Completed
Pull Request — master (#296)
by Beñat
04:19
created

TaskDTODataTransformer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 64
Duplicated Lines 25 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 9
dl 16
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A write() 0 4 1
C read() 16 46 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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