FilterProjectsHandler   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 45.9 %

Coupling/Cohesion

Components 0
Dependencies 10

Importance

Changes 0
Metric Value
dl 28
loc 61
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
B __invoke() 15 38 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Application\DataTransformer\Project\ProjectDataTransformer;
18
use Kreta\TaskManager\Domain\Model\Organization\Organization;
19
use Kreta\TaskManager\Domain\Model\Organization\OrganizationId;
20
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
21
use Kreta\TaskManager\Domain\Model\Organization\OrganizationSpecificationFactory;
22
use Kreta\TaskManager\Domain\Model\Project\Project;
23
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
24
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory;
25
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
26
use Kreta\TaskManager\Domain\Model\User\UserId;
27
28
class FilterProjectsHandler
29
{
30
    private $repository;
31
    private $specificationFactory;
32
    private $dataTransformer;
33
    private $organizationRepository;
34
    private $organizationSpecificationFactory;
35
36 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...
37
        OrganizationRepository $organizationRepository,
38
        OrganizationSpecificationFactory $organizationSpecificationFactory,
39
        ProjectRepository $repository,
40
        ProjectSpecificationFactory $specificationFactory,
41
        ProjectDataTransformer $dataTransformer
42
    ) {
43
        $this->repository = $repository;
44
        $this->specificationFactory = $specificationFactory;
45
        $this->dataTransformer = $dataTransformer;
46
        $this->organizationRepository = $organizationRepository;
47
        $this->organizationSpecificationFactory = $organizationSpecificationFactory;
48
    }
49
50
    public function __invoke(FilterProjectsQuery $query)
51
    {
52
        $organizationIds = [OrganizationId::generate($query->organizationId())];
53
        $organization = $this->organizationRepository->organizationOfId($organizationIds[0]);
54 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...
55
            if (!$organization->isOrganizationMember(UserId::generate($query->userId()))) {
56
                throw new UnauthorizedProjectResourceException();
57
            }
58
        } else {
59
            $organizations = $this->organizationRepository->query(
60
                $this->organizationSpecificationFactory->buildFilterableSpecification(
61
                    null,
62
                    UserId::generate($query->userId())
63
                )
64
            );
65
            $organizationIds = array_map(function (Organization $organization) {
66
                return $organization->id();
67
            }, $organizations);
68
        }
69
        if (empty($organizationIds)) {
70
            return [];
71
        }
72
73
        $projects = $this->repository->query(
74
            $this->specificationFactory->buildFilterableSpecification(
75
                $organizationIds,
76
                $query->name(),
77
                $query->offset(),
78
                $query->limit()
79
            )
80
        );
81
82
        return array_map(function (Project $project) {
83
            $this->dataTransformer->write($project);
84
85
            return $this->dataTransformer->read();
86
        }, $projects);
87
    }
88
}
89