Passed
Pull Request — master (#67)
by Daniel
05:57
created

PasswordUpdateAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 3
dl 0
loc 5
c 0
b 0
f 0
cc 1
ccs 0
cts 4
cp 0
crap 2
rs 10
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 Silverback\ApiComponentsBundle\Exception\UnexpectedValueException;
17
use Silverback\ApiComponentsBundle\Helper\User\PasswordManager;
18
use Silverback\ApiComponentsBundle\Serializer\SerializeFormatResolver;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
22
use Symfony\Component\Serializer\Encoder\DecoderInterface;
23
24
/**
25
 * @author Daniel West <[email protected]>
26
 */
27
class PasswordUpdateAction
28
{
29
    private PasswordManager $passwordManager;
30
    private SerializeFormatResolver $serializeFormatResolver;
31
    private DecoderInterface $decoder;
32
33
    public function __construct(PasswordManager $passwordManager, SerializeFormatResolver $serializeFormatResolver, DecoderInterface $decoder)
34
    {
35
        $this->passwordManager = $passwordManager;
36
        $this->serializeFormatResolver = $serializeFormatResolver;
37
        $this->decoder = $decoder;
38
    }
39
40
    public function __invoke(Request $request): Response
41
    {
42
        $format = $this->serializeFormatResolver->getFormatFromRequest($request);
43
        $data = $this->decoder->decode($request->getContent(), $format);
44
        $requiredKeys = ['username', 'token', 'password'];
45
        foreach ($requiredKeys as $requiredKey) {
46
            if (!isset($data[$requiredKey])) {
47
                throw new BadRequestHttpException(sprintf('the key `%s` was not found in POST data', $requiredKey));
48
            }
49
        }
50
        try {
51
            $this->passwordManager->passwordReset($data['username'], $data['token'], $data['password']);
52
53
            return new Response(null, Response::HTTP_OK);
54
        } catch (UnexpectedValueException $exception) {
55
            return new Response(null, Response::HTTP_NOT_FOUND);
56
        }
57
    }
58
}
59