Completed
Push — master ( 50c1b3...460dcb )
by Gorka
14s
created

TaskDTODataTransformer::read()   C

Complexity

Conditions 9
Paths 51

Size

Total Lines 46
Code Lines 32

Duplication

Lines 16
Ratio 34.78 %

Importance

Changes 0
Metric Value
dl 16
loc 46
rs 5.0942
c 0
b 0
f 0
cc 9
eloc 32
nc 51
nop 0
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\DataTransformer\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
21
class TaskDTODataTransformer implements TaskDataTransformer
22
{
23
    private $task;
24
    private $organizationRepository;
25
    private $projectRepository;
26
27
    public function __construct(OrganizationRepository $organizationRepository, ProjectRepository $projectRepository)
28
    {
29
        $this->organizationRepository = $organizationRepository;
30
        $this->projectRepository = $projectRepository;
31
    }
32
33
    public function write(Task $task)
34
    {
35
        $this->task = $task;
36
    }
37
38
    public function read()
39
    {
40
        if (!$this->task instanceof Task) {
41
            return [];
42
        }
43
44
        $project = $this->projectRepository->projectOfId(
45
            $this->task->projectId()
46
        );
47
        $organization = $this->organizationRepository->organizationOfId(
48
            $project->organizationId()
49
        );
50
        $assigneeId = null;
51
        $creatorId = null;
52 View Code Duplication
        foreach ($organization->organizationMembers() as $organizationMember) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            if ($organizationMember->id()->equals($this->task->assigneeId())) {
54
                $assigneeId = $organizationMember->userId()->id();
55
            }
56
            if ($organizationMember->id()->equals($this->task->creatorId())) {
57
                $creatorId = $organizationMember->userId()->id();
58
            }
59
        }
60 View Code Duplication
        foreach ($organization->owners() as $owner) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
61
            if ($owner->id()->equals($this->task->assigneeId())) {
62
                $assigneeId = $owner->userId()->id();
63
            }
64
            if ($owner->id()->equals($this->task->creatorId())) {
65
                $creatorId = $owner->userId()->id();
66
            }
67
        }
68
69
        return [
70
            'id'          => $this->task->id()->id(),
71
            'numeric_id'  => $this->task->numericId()->id(),
72
            'title'       => $this->task->title()->title(),
73
            'priority'    => $this->task->priority()->priority(),
74
            'progress'    => $this->task->progress()->progress(),
75
            'description' => $this->task->description(),
76
            'assignee_id' => $assigneeId,
77
            'creator_id'  => $creatorId,
78
            'created_on'  => $this->task->createdOn()->format('Y-m-d'),
79
            'updated_on'  => $this->task->updatedOn()->format('Y-m-d'),
80
            'project_id'  => $this->task->projectId()->id(),
81
            'parent_id'   => null === $this->task->parentId() ? null : $this->task->parentId()->id(),
82
        ];
83
    }
84
}
85