|
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\Query\Project\Task; |
|
16
|
|
|
|
|
17
|
|
|
use Kreta\TaskManager\Application\DataTransformer\Project\Task\TaskDataTransformer; |
|
18
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\Organization; |
|
19
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository; |
|
20
|
|
|
use Kreta\TaskManager\Domain\Model\Organization\OrganizationSpecificationFactory; |
|
21
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Project; |
|
22
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectId; |
|
23
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository; |
|
24
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory; |
|
25
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\Task; |
|
26
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId; |
|
27
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskPriority; |
|
28
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskProgress; |
|
29
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository; |
|
30
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskSpecificationFactory; |
|
31
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskResourceException; |
|
32
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
|
33
|
|
|
|
|
34
|
|
|
class FilterTasksHandler |
|
35
|
|
|
{ |
|
36
|
|
|
private $repository; |
|
37
|
|
|
private $specificationFactory; |
|
38
|
|
|
private $dataTransformer; |
|
39
|
|
|
private $projectRepository; |
|
40
|
|
|
private $projectSpecificationFactory; |
|
41
|
|
|
private $organizationRepository; |
|
42
|
|
|
private $organizationSpecificationFactory; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct( |
|
45
|
|
|
OrganizationRepository $organizationRepository, |
|
46
|
|
|
OrganizationSpecificationFactory $organizationSpecificationFactory, |
|
47
|
|
|
ProjectRepository $projectRepository, |
|
48
|
|
|
ProjectSpecificationFactory $projectSpecificationFactory, |
|
49
|
|
|
TaskRepository $repository, |
|
50
|
|
|
TaskSpecificationFactory $specificationFactory, |
|
51
|
|
|
TaskDataTransformer $dataTransformer |
|
52
|
|
|
) { |
|
53
|
|
|
$this->repository = $repository; |
|
54
|
|
|
$this->specificationFactory = $specificationFactory; |
|
55
|
|
|
$this->dataTransformer = $dataTransformer; |
|
56
|
|
|
$this->organizationRepository = $organizationRepository; |
|
57
|
|
|
$this->organizationSpecificationFactory = $organizationSpecificationFactory; |
|
58
|
|
|
$this->projectRepository = $projectRepository; |
|
59
|
|
|
$this->projectSpecificationFactory = $projectSpecificationFactory; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function __invoke(FilterTasksQuery $query) |
|
63
|
|
|
{ |
|
64
|
|
|
$userId = UserId::generate($query->userId()); |
|
65
|
|
|
$projectIds = [ProjectId::generate($query->projectId())]; |
|
66
|
|
|
|
|
67
|
|
|
$project = $this->projectRepository->projectOfId($projectIds[0]); |
|
68
|
|
|
if ($project instanceof Project) { |
|
69
|
|
|
$organization = $this->organizationRepository->organizationOfId( |
|
70
|
|
|
$project->organizationId() |
|
71
|
|
|
); |
|
72
|
|
|
if (!$organization->isOrganizationMember($userId)) { |
|
73
|
|
|
throw new UnauthorizedTaskResourceException(); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
$organizations = $this->organizationRepository->query( |
|
77
|
|
|
$this->organizationSpecificationFactory->buildFilterableSpecification( |
|
78
|
|
|
null, |
|
79
|
|
|
$userId |
|
80
|
|
|
) |
|
81
|
|
|
); |
|
82
|
|
|
$organizationIds = array_map(function (Organization $organization) { |
|
83
|
|
|
return $organization->id(); |
|
84
|
|
|
}, $organizations); |
|
85
|
|
|
$projects = $this->projectRepository->query( |
|
86
|
|
|
$this->projectSpecificationFactory->buildFilterableSpecification( |
|
87
|
|
|
$organizationIds, |
|
88
|
|
|
null |
|
89
|
|
|
) |
|
90
|
|
|
); |
|
91
|
|
|
$projectIds = array_map(function (Project $project) { |
|
92
|
|
|
return $project->id(); |
|
93
|
|
|
}, $projects); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$tasks = $this->repository->query( |
|
97
|
|
|
$this->specificationFactory->buildFilterableSpecification( |
|
98
|
|
|
$projectIds, |
|
99
|
|
|
$query->title(), |
|
100
|
|
|
$this->parentTask($query->parentId(), $userId), |
|
101
|
|
|
null === $query->priority() ? null : new TaskPriority($query->priority()), |
|
102
|
|
|
null === $query->progress() ? null : new TaskProgress($query->progress()), |
|
103
|
|
|
$query->offset(), |
|
104
|
|
|
$query->limit() |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return array_map(function (Task $task) { |
|
109
|
|
|
$this->dataTransformer->write($task); |
|
110
|
|
|
|
|
111
|
|
|
return $this->dataTransformer->read(); |
|
112
|
|
|
}, $tasks); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private function parentTask(? string $parentId, UserId $userId) : ? TaskId |
|
|
|
|
|
|
116
|
|
|
{ |
|
117
|
|
|
if (null === $parentId) { |
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$parent = $this->repository->taskOfId( |
|
122
|
|
|
TaskId::generate($parentId) |
|
123
|
|
|
); |
|
124
|
|
|
if (null === $parent) { |
|
125
|
|
|
return TaskId::generate(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$project = $this->projectRepository->projectOfId( |
|
129
|
|
|
$parent->projectId() |
|
130
|
|
|
); |
|
131
|
|
|
$organization = $this->organizationRepository->organizationOfId( |
|
132
|
|
|
$project->organizationId() |
|
133
|
|
|
); |
|
134
|
|
|
if (!$organization->isOrganizationMember($userId)) { |
|
135
|
|
|
throw new UnauthorizedTaskResourceException(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $parent->id(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.