Completed
Pull Request — master (#169)
by Beñat
05:56
created

ReassignTaskHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 10
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B __invoke() 0 31 4
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\Application\Project\Task;
16
17
use Kreta\TaskManager\Domain\Model\Organization\MemberId;
18
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
19
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
20
use Kreta\TaskManager\Domain\Model\Project\Task\Task;
21
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
22
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository;
24
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
25
use Kreta\TaskManager\Domain\Model\User\UserId;
26
27
class ReassignTaskHandler
28
{
29
    private $organizationRepository;
30
    private $projectRepository;
31
    private $taskRepository;
32
33
    public function __construct(
34
        OrganizationRepository $organizationRepository,
35
        ProjectRepository $projectRepository,
36
        TaskRepository $taskRepository
37
    ) {
38
        $this->organizationRepository = $organizationRepository;
39
        $this->projectRepository = $projectRepository;
40
        $this->taskRepository = $taskRepository;
41
    }
42
43
    public function __invoke(ReassignTaskCommand $command)
44
    {
45
        $task = $this->taskRepository->taskOfId(TaskId::generate($command->id()));
46
        if (!$task instanceof Task) {
47
            throw new TaskDoesNotExistException();
48
        }
49
50
        $project = $this->projectRepository->projectOfId(
51
            $task->projectId()
52
        );
53
        $organization = $this->organizationRepository->organizationOfId(
54
            $project->organizationId()
55
        );
56
57
        $editorId = MemberId::generate(
58
            UserId::generate($command->editorId()),
59
            $organization->id()
60
        );
61
62
        $newAssigneeId = MemberId::generate(
63
            UserId::generate($command->assigneeId()),
64
            $organization->id()
65
        );
66
67
        if (!$organization->isMember($editorId) || !$organization->isMember($newAssigneeId)) {
68
            throw new UnauthorizedTaskActionException();
69
        }
70
71
        $task->reassign($newAssigneeId);
72
        $this->taskRepository->persist($task);
73
    }
74
}
75