Completed
Push — master ( f7103c...fb6e23 )
by Beñat
12s
created

FilterProjectsHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 7
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __invoke() 0 18 3
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\OrganizationId;
19
use Kreta\TaskManager\Domain\Model\Project\Project;
20
use Kreta\TaskManager\Domain\Model\Project\ProjectName;
21
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
22
use Kreta\TaskManager\Domain\Model\Project\ProjectSpecificationFactory;
23
use Kreta\TaskManager\Domain\Model\User\UserId;
24
25
class FilterProjectsHandler
26
{
27
    private $repository;
28
    private $dataTransformer;
29
    private $specificationFactory;
30
31
    public function __construct(
32
        ProjectRepository $repository,
33
        ProjectSpecificationFactory $specificationFactory,
34
        ProjectDataTransformer $dataTransformer
35
    ) {
36
        $this->repository = $repository;
37
        $this->specificationFactory = $specificationFactory;
38
        $this->dataTransformer = $dataTransformer;
39
    }
40
41
    public function __invoke(FilterProjectsQuery $query)
42
    {
43
        $organizations = $this->repository->query(
44
            $this->specificationFactory->buildFilterableSpecification(
45
                UserId::generate($query->userId()),
46
                null === $query->organizationId() ? null : OrganizationId::generate($query->organizationId()),
47
                null === $query->name() ? null : new ProjectName($query->name()),
48
                $query->offset(),
49
                $query->limit()
50
            )
51
        );
52
53
        return array_map(function (Project $organization) {
54
            $this->dataTransformer->write($organization);
55
56
            return $this->dataTransformer->read();
57
        }, $organizations);
58
    }
59
}
60