Completed
Push — master ( f0708c...690234 )
by Gorka
02:59
created

ChangeParentTaskHandler::__invoke()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 49
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 6.1403
c 0
b 0
f 0
cc 8
eloc 28
nc 8
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\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\TaskAndTaskParentCannotBeTheSameException;
22
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId;
24
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentCannotBelongToOtherProjectException;
25
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentDoesNotExistException;
26
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository;
27
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
28
use Kreta\TaskManager\Domain\Model\User\UserId;
29
30
class ChangeParentTaskHandler
31
{
32
    private $repository;
33
    private $projectRepository;
34
    private $organizationRepository;
35
36
    public function __construct(
37
        TaskRepository $repository,
38
        ProjectRepository $projectRepository,
39
        OrganizationRepository $organizationRepository
40
    ) {
41
        $this->repository = $repository;
42
        $this->projectRepository = $projectRepository;
43
        $this->organizationRepository = $organizationRepository;
44
    }
45
46
    public function __invoke(ChangeParentTaskCommand $command)
47
    {
48
        $task = $this->repository->taskOfId(
49
            TaskId::generate(
50
                $command->id()
51
            )
52
        );
53
        if (!$task instanceof Task) {
54
            throw new TaskDoesNotExistException();
55
        }
56
57
        if (null !== $parentId = $command->parentId()) {
58
            $parentId = TaskId::generate($parentId);
59
            if ($task->id()->equals($parentId)) {
60
                throw new TaskAndTaskParentCannotBeTheSameException();
61
            }
62
63
            $parent = $this->repository->taskOfId($parentId);
64
            if (!$parent instanceof Task) {
65
                throw new TaskParentDoesNotExistException();
66
            }
67
            if (!$task->projectId()->equals($parent->projectId())) {
68
                throw new TaskParentCannotBelongToOtherProjectException();
69
            }
70
        }
71
72
        $project = $this->projectRepository->projectOfId(
73
            $task->projectId()
74
        );
75
        $organization = $this->organizationRepository->organizationOfId(
76
            $project->organizationId()
77
        );
78
        $changerId = MemberId::generate(
79
            UserId::generate(
80
                $command->changerId()
81
            ),
82
            $organization->id()
83
        );
84
85
        if (!$organization->isMember($changerId)) {
86
            throw new UnauthorizedTaskActionException();
87
        }
88
89
        $task->changeParent(
90
            !isset($parent) ? null : $parent->id()
91
        );
92
93
        $this->repository->persist($task);
94
    }
95
}
96