Completed
Pull Request — master (#212)
by Beñat
06:38 queued 02:56
created

CountProjectsHandler::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 18

Duplication

Lines 15
Ratio 55.56 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
c 0
b 0
f 0
nc 3
nop 1
dl 15
loc 27
rs 8.8571
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;
16
17
use Kreta\TaskManager\Domain\Model\Organization\Organization;
18
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationSpecificationFactory;
21
use Kreta\TaskManager\Domain\Model\Organization\UnauthorizedOrganizationActionException;
22
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
23
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory;
24
use Kreta\TaskManager\Domain\Model\User\UserId;
25
26
class CountProjectsHandler
27
{
28
    private $repository;
29
    private $specificationFactory;
30
    private $organizationRepository;
31
    private $organizationSpecificationFactory;
32
33 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
        OrganizationRepository $organizationRepository,
35
        OrganizationSpecificationFactory $organizationSpecificationFactory,
36
        ProjectRepository $repository,
37
        ProjectSpecificationFactory $specificationFactory
38
    ) {
39
        $this->repository = $repository;
40
        $this->specificationFactory = $specificationFactory;
41
        $this->organizationRepository = $organizationRepository;
42
        $this->organizationSpecificationFactory = $organizationSpecificationFactory;
43
    }
44
45
    public function __invoke(CountProjectsQuery $query): int
46
    {
47
        $organizationIds = [OrganizationId::generate($query->organizationId())];
48
        $organization = $this->organizationRepository->organizationOfId($organizationIds[0]);
49 View Code Duplication
        if ($organization instanceof Organization) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            if (!$organization->isOrganizationMember(UserId::generate($query->userId()))) {
51
                throw new UnauthorizedOrganizationActionException();
52
            }
53
        } else {
54
            $organizations = $this->organizationRepository->query(
55
                $this->organizationSpecificationFactory->buildFilterableSpecification(
56
                    null,
57
                    UserId::generate($query->userId())
58
                )
59
            );
60
            $organizationIds = array_map(function (Organization $organization) {
61
                return $organization->id();
62
            }, $organizations);
63
        }
64
65
        return $this->repository->count(
66
            $this->specificationFactory->buildFilterableSpecification(
67
                $organizationIds,
68
                $query->name()
69
            )
70
        );
71
    }
72
}
73