1 | <?php |
||
38 | class CreateTaskHandler |
||
39 | { |
||
40 | private $repository; |
||
41 | private $specificationFactory; |
||
42 | private $projectRepository; |
||
43 | private $organizationRepository; |
||
44 | |||
45 | public function __construct( |
||
46 | TaskRepository $repository, |
||
47 | TaskSpecificationFactory $specificationFactory, |
||
48 | ProjectRepository $projectRepository, |
||
49 | OrganizationRepository $organizationRepository |
||
50 | ) { |
||
51 | $this->repository = $repository; |
||
52 | $this->specificationFactory = $specificationFactory; |
||
53 | $this->projectRepository = $projectRepository; |
||
54 | $this->organizationRepository = $organizationRepository; |
||
55 | } |
||
56 | |||
57 | public function __invoke(CreateTaskCommand $command) |
||
58 | { |
||
59 | $taskId = $command->taskId(); |
||
60 | $parentTaskId = $command->parentId(); |
||
61 | |||
62 | if (!$this->areTaskAndParentIdsDifferent($taskId, $parentTaskId)) { |
||
63 | throw new TaskAndTaskParentCannotBeTheSameException(); |
||
64 | } |
||
65 | |||
66 | $task = $this->repository->taskOfId(TaskId::generate($taskId)); |
||
67 | if ($this->doesTaskExist($task)) { |
||
68 | throw new TaskAlreadyExistsException(); |
||
69 | } |
||
70 | |||
71 | $parentTask = $this->repository->taskOfId(TaskId::generate($parentTaskId)); |
||
72 | if (!$this->doesParentTaskExist($parentTaskId, $parentTask)) { |
||
73 | throw new TaskParentDoesNotExistException(); |
||
74 | } |
||
75 | |||
76 | $projectId = $command->projectId(); |
||
77 | $project = $this->projectRepository->projectOfId(ProjectId::generate($projectId)); |
||
78 | if (!$this->doesProjectExist($project)) { |
||
79 | throw new ProjectDoesNotExistException(); |
||
80 | } |
||
81 | |||
82 | $organizationId = $project->organizationId(); |
||
83 | $reporterId = UserId::generate($command->reporterId()); |
||
84 | $assigneeId = UserId::generate($command->assigneeId()); |
||
85 | |||
86 | $organization = $this->organizationRepository->organizationOfId($organizationId); |
||
87 | |||
88 | $this->checkUsersAreOrganizationMembers($organization, $reporterId, $assigneeId); |
||
89 | |||
90 | $taskId = TaskId::generate($taskId); |
||
91 | $title = new TaskTitle($command->title()); |
||
92 | $description = $command->description(); |
||
93 | $reporter = $this->becomeUserIdToDomainMemberId($organization, $reporterId); |
||
94 | $assignee = $this->becomeUserIdToDomainMemberId($organization, $assigneeId); |
||
95 | $priority = new TaskPriority($command->priority()); |
||
96 | $projectId = ProjectId::generate($projectId); |
||
97 | $parentTaskId = $this->parentTaskId($parentTaskId); |
||
98 | |||
99 | $tasksNumber = $this->repository->count( |
||
100 | $this->specificationFactory->buildByProjectSpecification($projectId) |
||
101 | ); |
||
102 | $numericId = NumericId::fromPrevious($tasksNumber); |
||
103 | |||
104 | $task = new Task( |
||
105 | $taskId, |
||
106 | $numericId, |
||
107 | $title, |
||
108 | $description, |
||
109 | $reporter, |
||
110 | $assignee, |
||
111 | $priority, |
||
112 | $projectId, |
||
113 | $parentTaskId |
||
114 | ); |
||
115 | $this->repository->persist($task); |
||
116 | } |
||
117 | |||
118 | private function areTaskAndParentIdsDifferent(?string $taskId, ?string $parentTaskId) : bool |
||
122 | |||
123 | private function doesTaskExist(?Task $task) : bool |
||
127 | |||
128 | private function doesParentTaskExist(?string $parentTaskId, Task $parentTask = null) : bool |
||
132 | |||
133 | private function doesProjectExist(?Project $project) : bool |
||
137 | |||
138 | private function checkUsersAreOrganizationMembers( |
||
139 | Organization $organization, |
||
146 | |||
147 | private function checkAssigneeIsOrganizationMember(Organization $organization, UserId $assigneeId) : void |
||
153 | |||
154 | private function checkReporterIsOrganizationMember(Organization $organization, UserId $reporterId) : void |
||
155 | { |
||
156 | if (!$organization->isOrganizationMember($reporterId)) { |
||
157 | throw new UnauthorizedTaskActionException(); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | private function parentTaskId(?string $parentTaskId) : ?TaskId |
||
165 | |||
166 | private function becomeUserIdToDomainMemberId(Organization $organization, UserId $userId) : MemberId |
||
170 | } |
||
171 |