LoadTaskData::load()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 53
rs 7.5251
c 0
b 0
f 0
cc 7
eloc 34
nc 10
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Infrastructure\Persistence\Doctrine\DataFixtures;
16
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Kreta\SharedKernel\Infrastructure\Persistence\Doctrine\DataFixtures\AbstractFixture;
19
use Kreta\TaskManager\Application\Command\Project\Task\ChangeTaskProgressCommand;
20
use Kreta\TaskManager\Application\Command\Project\Task\CreateTaskCommand;
21
use Kreta\TaskManager\Application\Query\Organization\OrganizationOfIdQuery;
22
use Kreta\TaskManager\Application\Query\Project\FilterProjectsQuery;
23
use Kreta\TaskManager\Domain\Model\Project\Task\TaskPriority;
24
25
class LoadTaskData extends AbstractFixture
26
{
27
    const TASK_PRIORITIES = [TaskPriority::LOW, TaskPriority::MEDIUM, TaskPriority::HIGH];
28
29
    protected function type() : string
30
    {
31
        return 'task';
32
    }
33
34
    public function getOrder() : int
35
    {
36
        return 4;
37
    }
38
39
    public function load(ObjectManager $manager) : void
40
    {
41
        $i = 0;
42
        while ($i < $this->amount()) {
43
            $userId = $this->getRandomUserByIndex($i);
44
45
            $this->queryBus()->handle(
46
                new FilterProjectsQuery(
47
                    $userId,
48
                    0,
49
                    1
50
                ),
51
                $projects
52
            );
53
54
            foreach ($projects as $project) {
55
                $this->queryBus()->handle(
56
                    new OrganizationOfIdQuery(
57
                        $project['organization_id'],
58
                        $userId
59
                    ),
60
                    $organization
61
                );
62
63
                $parent = 40 === $i || 70 === $i ? $this->fakeIds()[10] : null;
64
65
                $this->commandBus()->handle(
66
                    new CreateTaskCommand(
67
                        'Task ' . $i,
68
                        'The description of the task ' . $i,
69
                        $userId,
70
                        $organization['owners'][0]['id'],
71
                        $this->taskPriority($i),
72
                        $project['id'],
73
                        $parent,
74
                        $this->fakeIds()[$i]
75
                    )
76
                );
77
78
                if (130 === $i || 340 === $i) {
79
                    $this->commandBus()->handle(
80
                        new ChangeTaskProgressCommand(
81
                            $this->fakeIds()[$i],
82
                            'doing',
83
                            $userId
84
                        )
85
                    );
86
                }
87
88
                ++$i;
89
            }
90
        }
91
    }
92
93
    private function taskPriority($index)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
94
    {
95
        $priorityIndex = $index % 2 > 3 ? 0 : 2 - $index % 3;
96
97
        return self::TASK_PRIORITIES[$priorityIndex];
98
    }
99
}
100