Completed
Push — master ( cdb55b...3f24cf )
by Gorka
9s
created

CountTasksHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 25 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
dl 13
loc 52
c 0
b 0
f 0
rs 10
wmc 4
lcom 0
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
B __invoke() 0 30 3

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\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\TaskRepository;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskSpecificationFactory;
24
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
25
use Kreta\TaskManager\Domain\Model\User\UserId;
26
27
class CountTasksHandler
28
{
29
    private $repository;
30
    private $specificationFactory;
31
    private $projectRepository;
32
    private $projectSpecificationFactory;
33
34 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...
35
        ProjectRepository $projectRepository,
36
        ProjectSpecificationFactory $projectSpecificationFactory,
37
        OrganizationRepository $organizationRepository,
38
        TaskRepository $repository,
39
        TaskSpecificationFactory $specificationFactory
40
    ) {
41
        $this->projectRepository = $projectRepository;
42
        $this->projectSpecificationFactory = $projectSpecificationFactory;
43
        $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...
44
        $this->repository = $repository;
45
        $this->specificationFactory = $specificationFactory;
46
    }
47
48
    public function __invoke(CountTasksQuery $query): int
49
    {
50
        $projectIds = [ProjectId::generate($query->projectId())];
51
        $project = $this->projectRepository->projectOfId($projectIds[0]);
52
        if ($project instanceof Project) {
53
            $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...
54
                $project->organizationId()
55
            );
56
            if (!$organization->isOrganizationMember(UserId::generate($query->userId()))) {
57
                throw new UnauthorizedProjectResourceException();
58
            }
59
        } else {
60
            $projects = $this->projectRepository->query(
61
                $this->projectSpecificationFactory->buildFilterableSpecification(
62
                    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...
63
                    UserId::generate($query->userId())
64
                )
65
            );
66
            $projectIds = array_map(function (Project $project) {
67
                return $project->id();
68
            }, $projects);
69
        }
70
71
        return $this->repository->count(
72
            $this->specificationFactory->buildFilterableSpecification(
73
                $projectIds,
74
                $query->title()
75
            )
76
        );
77
    }
78
}
79