Completed
Push — master ( 69c2b9...604983 )
by Beñat
16s
created

CreateTaskMutation::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 50
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.6315
c 0
b 0
f 0
cc 5
eloc 34
nc 5
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\Task;
16
17
use Kreta\SharedKernel\Application\CommandBus;
18
use Kreta\SharedKernel\Http\GraphQl\Relay\Mutation;
19
use Kreta\TaskManager\Application\Command\Project\Task\CreateTaskCommand;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationMemberDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
22
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentDoesNotExistException;
23
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
24
use Kreta\TaskManager\Infrastructure\Symfony\GraphQl\Query\Project\Task\TaskResolver;
25
use Overblog\GraphQLBundle\Error\UserError;
26
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
27
28
class CreateTaskMutation implements Mutation
29
{
30
    private $commandBus;
31
    private $currentUser;
32
    private $taskResolver;
33
34
    public function __construct(TokenStorageInterface $tokenStorage, CommandBus $commandBus, TaskResolver $taskResolver)
35
    {
36
        $this->commandBus = $commandBus;
37
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
38
        $this->taskResolver = $taskResolver;
39
    }
40
41
    public function execute(array $values) : array
42
    {
43
        $command = new CreateTaskCommand(
44
            $values['title'],
45
            $values['description'],
46
            $this->currentUser,
47
            $values['assigneeId'],
48
            $values['priority'],
49
            $values['projectId'],
50
            $values['parentId'] ?? null
51
        );
52
53
        try {
54
            $this->commandBus->handle($command);
55
        } catch (TaskParentDoesNotExistException $exception) {
56
            throw new UserError(
57
                sprintf(
58
                    'Does not exist any task with the given "%s" id',
59
                    $values['parentId']
60
                )
61
            );
62
        } catch (ProjectDoesNotExistException $exception) {
63
            throw new UserError(
64
                sprintf(
65
                    'Does not exist any project with the given "%s" id',
66
                    $values['projectId']
67
                )
68
            );
69
        } catch (UnauthorizedTaskActionException $exception) {
70
            throw new UserError(
71
                sprintf(
72
                    'The "%s" creator does not allow to perform the task creation',
73
                    $this->currentUser
74
                )
75
            );
76
        } catch (OrganizationMemberDoesNotExistException $exception) {
77
            throw new UserError(
78
                sprintf(
79
                    'The "%s" assignee is not an organization member',
80
                    $values['assigneeId']
81
                )
82
            );
83
        }
84
85
        return [
86
            'task' => $this->taskResolver->resolve([
87
                'id' => $command->taskId(),
88
            ]),
89
        ];
90
    }
91
}
92