Completed
Push — master ( a672b4...82d417 )
by Beñat
04:13
created

EnableAction   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 4
lcom 0
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 23 3
1
<?php
2
3
/*
4
 * This file is part of the Kreta package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
declare(strict_types=1);
14
15
namespace Kreta\IdentityAccess\Infrastructure\Symfony\HttpAction;
16
17
use BenGorUser\User\Application\Command\Enable\EnableUserCommand;
18
use BenGorUser\User\Domain\Model\Exception\UserTokenNotFoundException;
19
use Kreta\SharedKernel\Application\CommandBus;
20
use Symfony\Component\HttpFoundation\JsonResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
23
class EnableAction
24
{
25
    private $commandBus;
26
27
    public function __construct(CommandBus $commandBus)
28
    {
29
        $this->commandBus = $commandBus;
30
    }
31
32
    public function __invoke(Request $request) : JsonResponse
33
    {
34
        $confirmationToken = $request->query->get('confirmation-token');
35
        if (null === $confirmationToken) {
36
            return new JsonResponse(null, 404);
37
        }
38
39
        try {
40
            $this->commandBus->handle(
41
                new EnableUserCommand($confirmationToken)
42
            );
43
44
            return new JsonResponse();
45
        } catch (UserTokenNotFoundException $exception) {
46
            return new JsonResponse(
47
                sprintf(
48
                    'The "%s" confirmation token does not exist',
49
                    $confirmationToken
50
                ),
51
                400
52
            );
53
        }
54
    }
55
}
56