ChangeTaskProgressHandler::checkTaskExists()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
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\TaskProgress;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskRepository;
24
use Kreta\TaskManager\Domain\Model\Project\Task\UnauthorizedTaskActionException;
25
use Kreta\TaskManager\Domain\Model\User\UserId;
26
27
class ChangeTaskProgressHandler
28
{
29
    private $organizationRepository;
30
    private $projectRepository;
31
    private $taskRepository;
32
33
    public function __construct(
34
        OrganizationRepository $organizationRepository,
35
        ProjectRepository $projectRepository,
36
        TaskRepository $taskRepository
37
    ) {
38
        $this->organizationRepository = $organizationRepository;
39
        $this->projectRepository = $projectRepository;
40
        $this->taskRepository = $taskRepository;
41
    }
42
43
    public function __invoke(ChangeTaskProgressCommand $command)
44
    {
45
        $taskId = TaskId::generate($command->id());
46
        $editorId = UserId::generate($command->editorId());
47
        $progress = new TaskProgress($command->progress());
48
49
        $task = $this->taskRepository->taskOfId($taskId);
50
        $this->checkTaskExists($task);
51
        $this->checkEditorPrivileges($task, $editorId);
52
        $task->changeProgress($progress);
53
54
        $this->taskRepository->persist($task);
55
    }
56
57
    private function checkTaskExists(Task $task = null) : void
58
    {
59
        if (!$task instanceof Task) {
60
            throw new TaskDoesNotExistException();
61
        }
62
    }
63
64
    private function checkEditorPrivileges(Task $task, UserId $editorId) : void
65
    {
66
        $project = $this->projectRepository->projectOfId($task->projectId());
67
        $organization = $this->organizationRepository->organizationOfId($project->organizationId());
68
        if (!$organization->isOrganizationMember($editorId)) {
69
            throw new UnauthorizedTaskActionException();
70
        }
71
    }
72
}
73