Completed
Pull Request — master (#170)
by Beñat
10:01 queued 04:43
created

EditProjectHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 11
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __invoke() 0 32 4
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\Project;
16
17
use Kreta\SharedKernel\Domain\Model\Identity\Slug;
18
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
19
use Kreta\TaskManager\Domain\Model\Organization\OwnerId;
20
use Kreta\TaskManager\Domain\Model\Project\Project;
21
use Kreta\TaskManager\Domain\Model\Project\ProjectDoesNotExistException;
22
use Kreta\TaskManager\Domain\Model\Project\ProjectId;
23
use Kreta\TaskManager\Domain\Model\Project\ProjectName;
24
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
25
use Kreta\TaskManager\Domain\Model\Project\UnauthorizedProjectActionException;
26
use Kreta\TaskManager\Domain\Model\User\UserId;
27
28
class EditProjectHandler
29
{
30
    private $repository;
31
    private $organizationRepository;
32
33
    public function __construct(ProjectRepository $repository, OrganizationRepository $organizationRepository)
34
    {
35
        $this->repository = $repository;
36
        $this->organizationRepository = $organizationRepository;
37
    }
38
39
    public function __invoke(EditProjectCommand $command)
40
    {
41
        $project = $this->repository->projectOfId(
42
            ProjectId::generate($command->id())
43
        );
44
        if (!$project instanceof Project) {
45
            throw new ProjectDoesNotExistException();
46
        }
47
48
        $organization = $this->organizationRepository->organizationOfId(
49
            $project->organizationId()
50
        );
51
52
        $ownerId = OwnerId::generate(
53
            UserId::generate(
54
                $command->editorId()
55
            ),
56
            $organization->id()
57
        );
58
        if (!$organization->isOwner($ownerId)) {
59
            throw new UnauthorizedProjectActionException();
60
        }
61
62
        $project->edit(
63
            new ProjectName($command->name()),
64
            new Slug(
65
                null === $command->slug() ? $command->name() : $command->slug()
66
            )
67
        );
68
69
        $this->repository->persist($project);
70
    }
71
}
72