Passed
Push — feature/uploadable ( 7c6d25...a7ed20 )
by Daniel
11:07
created

PasswordUpdateAction::__invoke()   A

Complexity

Conditions 5
Paths 11

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 13
nc 11
nop 1
dl 0
loc 20
ccs 0
cts 13
cp 0
crap 30
rs 9.5222
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\Action\User;
15
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Response;
18
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
use Symfony\Component\Security\Core\Exception\AuthenticationException;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 */
25
class PasswordUpdateAction extends AbstractPasswordAction
26
{
27
    public function __invoke(Request $request)
28
    {
29
        $data = $this->serializer->decode($request->getContent(), $this->requestFormatResolver->getFormatFromRequest($request), []);
30
        $requiredKeys = ['username', 'token', 'password'];
31
        foreach ($requiredKeys as $requiredKey) {
32
            if (!isset($data[$requiredKey])) {
33
                throw new BadRequestHttpException(sprintf('the key `%s` was not found in POST data', $requiredKey));
34
            }
35
        }
36
37
        try {
38
            $this->passwordManager->passwordReset($data['username'], $data['token'], $data['password']);
39
40
            return $this->responseFactory->create($request);
41
        } catch (AuthenticationException $exception) {
42
            $errors = $exception->getMessageData();
43
44
            return $this->responseFactory->create($request, $errors, Response::HTTP_BAD_REQUEST);
45
        } catch (NotFoundHttpException $exception) {
46
            return $this->responseFactory->create($request, $exception->getMessage(), Response::HTTP_NOT_FOUND);
47
        }
48
    }
49
}
50