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

ProjectOfIdHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 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\OrganizationRepository;
19
use Kreta\TaskManager\Domain\Model\Project\Project;
20
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Project\ProjectId;
22
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
23
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectResourceException;
24
use Kreta\TaskManager\Domain\Model\User\UserId;
25
26
class ProjectOfIdHandler
27
{
28
    private $repository;
29
    private $organizationRepository;
30
    private $dataTransformer;
31
32
    public function __construct(
33
        OrganizationRepository $organizationRepository,
34
        ProjectRepository $repository,
35
        ProjectDataTransformer $dataTransformer
36
    ) {
37
        $this->repository = $repository;
38
        $this->dataTransformer = $dataTransformer;
39
        $this->organizationRepository = $organizationRepository;
40
    }
41
42
    public function __invoke(ProjectOfIdQuery $query)
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...
43
    {
44
        $project = $this->repository->projectOfId(
45
            ProjectId::generate(
46
                $query->projectId()
47
            )
48
        );
49
        if (!$project instanceof Project) {
50
            throw new ProjectDoesNotExistException();
51
        }
52
        $organization = $this->organizationRepository->organizationOfId(
53
            $project->organizationId()
54
        );
55
        if (!$organization->isOrganizationMember(UserId::generate($query->userId()))) {
56
            throw new UnauthorizedProjectResourceException();
57
        }
58
59
        $this->dataTransformer->write($project);
60
61
        return $this->dataTransformer->read();
62
    }
63
}
64