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\Domain\Model\Organization\OrganizationRepository; |
18
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Project; |
19
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectId; |
20
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository; |
21
|
|
|
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory; |
22
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId; |
23
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskPriority; |
24
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskProgress; |
25
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository; |
26
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\TaskSpecificationFactory; |
27
|
|
|
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskResourceException; |
28
|
|
|
use Kreta\TaskManager\Domain\Model\User\UserId; |
29
|
|
|
|
30
|
|
|
class CountTasksHandler |
31
|
|
|
{ |
32
|
|
|
private $repository; |
33
|
|
|
private $specificationFactory; |
34
|
|
|
private $projectRepository; |
35
|
|
|
private $projectSpecificationFactory; |
36
|
|
|
|
37
|
|
|
public function __construct( |
38
|
|
|
ProjectRepository $projectRepository, |
39
|
|
|
ProjectSpecificationFactory $projectSpecificationFactory, |
40
|
|
|
OrganizationRepository $organizationRepository, |
41
|
|
|
TaskRepository $repository, |
42
|
|
|
TaskSpecificationFactory $specificationFactory |
43
|
|
|
) { |
44
|
|
|
$this->projectRepository = $projectRepository; |
45
|
|
|
$this->projectSpecificationFactory = $projectSpecificationFactory; |
46
|
|
|
$this->organizationRepository = $organizationRepository; |
|
|
|
|
47
|
|
|
$this->repository = $repository; |
48
|
|
|
$this->specificationFactory = $specificationFactory; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __invoke(CountTasksQuery $query): int |
52
|
|
|
{ |
53
|
|
|
$userId = UserId::generate($query->userId()); |
54
|
|
|
$projectIds = [ProjectId::generate($query->projectId())]; |
55
|
|
|
$project = $this->projectRepository->projectOfId($projectIds[0]); |
56
|
|
|
if ($project instanceof Project) { |
57
|
|
|
$organization = $this->organizationRepository->organizationOfId( |
|
|
|
|
58
|
|
|
$project->organizationId() |
59
|
|
|
); |
60
|
|
|
if (!$organization->isOrganizationMember($userId)) { |
61
|
|
|
throw new UnauthorizedTaskResourceException(); |
62
|
|
|
} |
63
|
|
|
} else { |
64
|
|
|
$projects = $this->projectRepository->query( |
65
|
|
|
$this->projectSpecificationFactory->buildFilterableSpecification( |
66
|
|
|
null, |
|
|
|
|
67
|
|
|
$userId |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
$projectIds = array_map(function (Project $project) { |
71
|
|
|
return $project->id(); |
72
|
|
|
}, $projects); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->repository->count( |
76
|
|
|
$this->specificationFactory->buildFilterableSpecification( |
77
|
|
|
$projectIds, |
78
|
|
|
$query->title(), |
79
|
|
|
$this->parentTask($query->parentId(), $userId), |
80
|
|
|
null === $query->priority() ? null : new TaskPriority($query->priority()), |
81
|
|
|
null === $query->progress() ? null : new TaskProgress($query->progress()) |
82
|
|
|
) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function parentTask(? string $parentId, UserId $userId) : ? TaskId |
|
|
|
|
87
|
|
|
{ |
88
|
|
|
if (null === $parentId) { |
89
|
|
|
return null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$parent = $this->repository->taskOfId( |
93
|
|
|
TaskId::generate($parentId) |
94
|
|
|
); |
95
|
|
|
if (null === $parent) { |
96
|
|
|
return TaskId::generate(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$project = $this->projectRepository->projectOfId( |
100
|
|
|
$parent->projectId() |
101
|
|
|
); |
102
|
|
|
$organization = $this->organizationRepository->organizationOfId( |
|
|
|
|
103
|
|
|
$project->organizationId() |
104
|
|
|
); |
105
|
|
|
if (!$organization->isOrganizationMember($userId)) { |
106
|
|
|
throw new UnauthorizedTaskResourceException(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $parent->id(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.