Completed
Push — master ( f571ad...af881e )
by Gorka
10s
created

CountTasksHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 5
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The property organizationRepository does not seem to exist. Did you mean repository?

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.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The property organizationRepository does not seem to exist. Did you mean repository?

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.

Loading history...
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,
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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 @return annotation as described here.

Loading history...
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(
0 ignored issues
show
Bug introduced by
The property organizationRepository does not seem to exist. Did you mean repository?

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.

Loading history...
103
            $project->organizationId()
104
        );
105
        if (!$organization->isOrganizationMember($userId)) {
106
            throw new UnauthorizedTaskResourceException();
107
        }
108
109
        return $parent->id();
110
    }
111
}
112