|
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\Command\Project\Task; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
18
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Project; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectId; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\Task; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskAlreadyExistsException; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskAndTaskParentCannotBeTheSameException; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskParentDoesNotExistException; |
|
27
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskPriority; |
|
28
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository; |
|
29
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskTitle; |
|
30
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException; |
|
31
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
32
|
|
|
|
|
33
|
|
|
class CreateTaskHandler |
|
34
|
|
|
{ |
|
35
|
|
|
private $repository; |
|
36
|
|
|
private $projectRepository; |
|
37
|
|
|
private $organizationRepository; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct( |
|
40
|
|
|
TaskRepository $repository, |
|
41
|
|
|
ProjectRepository $projectRepository, |
|
42
|
|
|
OrganizationRepository $organizationRepository |
|
43
|
|
|
) { |
|
44
|
|
|
$this->repository = $repository; |
|
45
|
|
|
$this->projectRepository = $projectRepository; |
|
46
|
|
|
$this->organizationRepository = $organizationRepository; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __invoke(CreateTaskCommand $command) |
|
50
|
|
|
{ |
|
51
|
|
|
if (null !== $id = $command->taskId()) { |
|
52
|
|
|
$task = $this->repository->taskOfId( |
|
53
|
|
|
TaskId::generate( |
|
54
|
|
|
$id |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
if ($command->parentId() === $command->taskId()) { |
|
58
|
|
|
throw new TaskAndTaskParentCannotBeTheSameException(); |
|
59
|
|
|
} |
|
60
|
|
|
if ($task instanceof Task) { |
|
61
|
|
|
throw new TaskAlreadyExistsException(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
$project = $this->projectRepository->projectOfId( |
|
65
|
|
|
ProjectId::generate( |
|
66
|
|
|
$command->projectId() |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
if (!$project instanceof Project) { |
|
70
|
|
|
throw new ProjectDoesNotExistException(); |
|
71
|
|
|
} |
|
72
|
|
|
$organization = $this->organizationRepository->organizationOfId( |
|
73
|
|
|
$project->organizationId() |
|
74
|
|
|
); |
|
75
|
|
|
$creatorId = UserId::generate($command->creatorId()); |
|
76
|
|
|
$assigneeId = UserId::generate($command->assigneeId()); |
|
77
|
|
|
if (!$organization->isOrganizationMember($creatorId) || !$organization->isOrganizationMember($assigneeId)) { |
|
78
|
|
|
throw new UnauthorizedTaskActionException(); |
|
79
|
|
|
} |
|
80
|
|
|
if (null !== $parentId = $command->parentId()) { |
|
81
|
|
|
$parent = $this->repository->taskOfId( |
|
82
|
|
|
TaskId::generate( |
|
83
|
|
|
$parentId |
|
84
|
|
|
) |
|
85
|
|
|
); |
|
86
|
|
|
if (!$parent instanceof Task) { |
|
87
|
|
|
throw new TaskParentDoesNotExistException(); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
$task = new Task( |
|
91
|
|
|
TaskId::generate($id), |
|
92
|
|
|
new TaskTitle( |
|
93
|
|
|
$command->title() |
|
94
|
|
|
), |
|
95
|
|
|
$command->description(), |
|
96
|
|
|
$organization->organizationMember($creatorId)->id(), |
|
97
|
|
|
$organization->organizationMember($assigneeId)->id(), |
|
98
|
|
|
new TaskPriority($command->priority()), |
|
99
|
|
|
$project->id(), |
|
100
|
|
|
TaskId::generate( |
|
101
|
|
|
$parentId |
|
102
|
|
|
) |
|
103
|
|
|
); |
|
104
|
|
|
$this->repository->persist($task); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|