Completed
Pull Request — master (#309)
by Beñat
04:06
created

EditProjectMutation::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 3
nop 1
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\TaskManager\Infrastructure\Symfony\GraphQl\Mutation\Project;
16
17
use Kreta\SharedKernel\Application\CommandBus;
18
use Kreta\SharedKernel\Http\GraphQl\Relay\Mutation;
19
use Kreta\TaskManager\Application\Command\Project\EditProjectCommand;
20
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectActionException;
22
use Kreta\TaskManager\Infrastructure\Symfony\GraphQl\Query\Project\ProjectResolver;
23
use Overblog\GraphQLBundle\Error\UserError;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
26
class EditProjectMutation implements Mutation
27
{
28
    private $commandBus;
29
    private $currentUser;
30
    private $projectResolver;
31
32 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
        TokenStorageInterface $tokenStorage,
34
        CommandBus $commandBus,
35
        ProjectResolver $projectResolver
36
    ) {
37
        $this->commandBus = $commandBus;
38
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
39
        $this->projectResolver = $projectResolver;
40
    }
41
42
    public function execute(array $values) : array
43
    {
44
        $command = new EditProjectCommand(
45
            $values['id'],
46
            $values['name'],
47
            $this->currentUser,
48
            $values['slug'] ?? null
49
        );
50
51
        try {
52
            $this->commandBus->handle($command);
53
        } catch (ProjectDoesNotExistException $exception) {
54
            throw new UserError(
55
                sprintf(
56
                    'The project with "%s" id does not exist',
57
                    $values['id']
58
                )
59
            );
60
        } catch (UnauthorizedProjectActionException $exception) {
61
            throw new UserError(
62
                sprintf(
63
                    'The "%s" user does not allow to edit the project',
64
                    $this->currentUser
65
                )
66
            );
67
        }
68
69
        $project = $this->projectResolver->resolve([
70
            'id' => $values['id'],
71
        ]);
72
73
        return [
74
            'project' => $project,
75
        ];
76
    }
77
}
78