Completed
Pull Request — master (#308)
by Gorka
04:59
created

EditTaskMutation::execute()   B

Complexity

Conditions 6
Paths 21

Size

Total Lines 69
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 8.56
c 0
b 0
f 0
cc 6
eloc 49
nc 21
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Task;
16
17
use Kreta\SharedKernel\Application\CommandBus;
18
use Kreta\SharedKernel\Http\GraphQl\Relay\Mutation;
19
use Kreta\TaskManager\Application\Command\Project\Task\ChangeParentTaskCommand;
20
use Kreta\TaskManager\Application\Command\Project\Task\ChangeTaskPriorityCommand;
21
use Kreta\TaskManager\Application\Command\Project\Task\EditTaskCommand;
22
use Kreta\TaskManager\Application\Command\Project\Task\ReassignTaskCommand;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskAndTaskParentCannotBeTheSameException;
24
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
25
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentCannotBelongToOtherProjectException;
26
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentDoesNotExistException;
27
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
28
use Kreta\TaskManager\Infrastructure\Symfony\GraphQl\Query\Project\Task\TaskResolver;
29
use Overblog\GraphQLBundle\Error\UserError;
30
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
31
32
class EditTaskMutation implements Mutation
33
{
34
    private $commandBus;
35
    private $currentUser;
36
    private $taskResolver;
37
38
    public function __construct(TokenStorageInterface $tokenStorage, CommandBus $commandBus, TaskResolver $taskResolver)
39
    {
40
        $this->commandBus = $commandBus;
41
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
42
        $this->taskResolver = $taskResolver;
43
    }
44
45
    public function execute(array $values) : array
46
    {
47
        $editTaskCommand = new EditTaskCommand(
48
            $values['id'],
49
            $values['title'],
50
            $values['description'],
51
            $this->currentUser
52
        );
53
        $changePriorityCommand = new ChangeTaskPriorityCommand(
54
            $values['id'],
55
            $values['priority'],
56
            $this->currentUser
57
        );
58
        $reassignTaskCommand = new ReassignTaskCommand(
59
            $values['id'],
60
            $values['assigneeId'],
61
            $this->currentUser
62
        );
63
        $changeParentTaskCommand = new ChangeParentTaskCommand(
64
            $values['id'],
65
            $this->currentUser,
66
            $values['parentId'] ?? null
67
        );
68
69
        try {
70
            $this->commandBus->handle($editTaskCommand);
71
            $this->commandBus->handle($changePriorityCommand);
72
            $this->commandBus->handle($reassignTaskCommand);
73
            $this->commandBus->handle($changeParentTaskCommand);
74
        } catch (TaskDoesNotExistException $exception) {
75
            throw new UserError(
76
                sprintf(
77
                    'The task with "%s" id does not exist',
78
                    $values['id']
79
                )
80
            );
81
        } catch (UnauthorizedTaskActionException $exception) {
82
            throw new UserError(
83
                sprintf(
84
                    'The "%s" user does not allow to edit the task',
85
                    $this->currentUser
86
                )
87
            );
88
        } catch (TaskAndTaskParentCannotBeTheSameException $exception) {
89
            throw new UserError('The task and its parent are the same');
90
        } catch (TaskParentDoesNotExistException $exception) {
91
            throw new UserError(
92
                sprintf(
93
                    'The task parent with "%s" id does not exist',
94
                    $values['parentId']
95
                )
96
            );
97
        } catch (TaskParentCannotBelongToOtherProjectException $exception) {
98
            throw new UserError(
99
                sprintf(
100
                    'The task parent with "%s" id does not belong to the task\'s project',
101
                    $values['parentId']
102
                )
103
            );
104
        }
105
106
        $task = $this->taskResolver->resolve([
107
            'id' => $editTaskCommand->id(),
108
        ]);
109
110
        return [
111
            'task' => $task,
112
        ];
113
    }
114
}
115