Completed
Pull Request — master (#318)
by Beñat
03:54
created

ChangeTaskProgressMutation::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 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\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\ChangeTaskProgressCommand;
20
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
22
use Kreta\TaskManager\Infrastructure\Symfony\GraphQl\Query\Project\Task\TaskResolver;
23
use Overblog\GraphQLBundle\Error\UserError;
24
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
25
26
class ChangeTaskProgressMutation implements Mutation
27
{
28
    private $commandBus;
29
    private $currentUser;
30
    private $taskResolver;
31
32
    public function __construct(TokenStorageInterface $tokenStorage, CommandBus $commandBus, TaskResolver $taskResolver)
33
    {
34
        $this->commandBus = $commandBus;
35
        $this->currentUser = $tokenStorage->getToken()->getUser()->getUsername();
36
        $this->taskResolver = $taskResolver;
37
    }
38
39
    public function execute(array $values) : array
40
    {
41
        $changeTaskProgressCommand = new ChangeTaskProgressCommand(
42
            $values['id'],
43
            $values['progress'],
44
            $this->currentUser
45
        );
46
47
        try {
48
            $this->commandBus->handle($changeTaskProgressCommand);
49
        } catch (TaskDoesNotExistException $exception) {
50
            throw new UserError(
51
                sprintf(
52
                    'The task with "%s" id does not exist',
53
                    $values['id']
54
                )
55
            );
56
        } catch (UnauthorizedTaskActionException $exception) {
57
            throw new UserError(
58
                sprintf(
59
                    'The "%s" user does not allow to change the progress of the task',
60
                    $this->currentUser
61
                )
62
            );
63
        }
64
65
        $task = $this->taskResolver->resolve([
66
            'id' => $changeTaskProgressCommand->id(),
67
        ]);
68
69
        return [
70
            'task' => $task,
71
        ];
72
    }
73
}
74