Completed
Pull Request — master (#180)
by Gorka
05:29
created

EditTaskHandler::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 17

Duplication

Lines 31
Ratio 100 %

Importance

Changes 0
Metric Value
dl 31
loc 31
c 0
b 0
f 0
rs 8.8571
cc 3
eloc 17
nc 3
nop 1
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\Command\Project\Task;
16
17
use Kreta\TaskManager\Domain\Model\Organization\OrganizationRepository;
18
use Kreta\TaskManager\Domain\Model\Project\ProjectRepository;
19
use Kreta\TaskManager\Domain\Model\Project\Task\Task;
20
use Kreta\TaskManager\Domain\Model\Project\Task\TaskDoesNotExistException;
21
use Kreta\TaskManager\Domain\Model\Project\Task\TaskId;
22
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskTitle;
24
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
25
use Kreta\TaskManager\Domain\Model\User\UserId;
26
27 View Code Duplication
class EditTaskHandler
0 ignored issues
show
Duplication introduced by
This class 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...
28
{
29
    private $repository;
30
    private $projectRepository;
31
    private $organizationRepository;
32
33
    public function __construct(
34
        TaskRepository $repository,
35
        ProjectRepository $projectRepository,
36
        OrganizationRepository $organizationRepository
37
    ) {
38
        $this->repository = $repository;
39
        $this->projectRepository = $projectRepository;
40
        $this->organizationRepository = $organizationRepository;
41
    }
42
43
    public function __invoke(EditTaskCommand $command)
44
    {
45
        $task = $this->repository->taskOfId(
46
            TaskId::generate(
47
                $command->id()
48
            )
49
        );
50
        if (!$task instanceof Task) {
51
            throw new TaskDoesNotExistException();
52
        }
53
54
        $project = $this->projectRepository->projectOfId(
55
            $task->projectId()
56
        );
57
        $organization = $this->organizationRepository->organizationOfId(
58
            $project->organizationId()
59
        );
60
61
        if (!$organization->isOrganizationMember(UserId::generate($command->editorId()))) {
62
            throw new UnauthorizedTaskActionException();
63
        }
64
65
        $task->edit(
66
            new TaskTitle(
67
                $command->title()
68
            ),
69
            $command->description()
70
        );
71
72
        $this->repository->persist($task);
73
    }
74
}
75