Completed
Push — master ( ff3370...c7a062 )
by Beñat
38s queued 33s
created

LoadOrganizationData::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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\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\Organization\AddOrganizationMemberToOrganizationCommand;
20
use Kreta\TaskManager\Application\Command\Organization\CreateOrganizationCommand;
21
22
class LoadOrganizationData extends AbstractFixture
23
{
24
    protected function type() : string
25
    {
26
        return 'organization';
27
    }
28
29
    public function getOrder() : int
30
    {
31
        return 2;
32
    }
33
34
    public function load(ObjectManager $manager) : void
35
    {
36
        $i = 0;
37
        $memberIds = [];
38
        while ($i < $this->amount()) {
39
            $ownerId = $this->getRandomUserByIndex($i);
40
            $command = new CreateOrganizationCommand(
41
                $ownerId,
42
                'Organization ' . $i,
43
                $this->fakeIds()[$i]
44
            );
45
            $memberIds[] = $ownerId;
46
47
            $this->commandBus()->handle($command);
48
49
            $iterations = $i % 5 > 6 ? 0 : 5 - $i % 6;
50
            $j = 0;
51
            while ($j < $iterations) {
52
                $memberId = $this->getRandomUserByIndex($j);
53
                if (in_array($memberId, $memberIds, true)) {
54
                    ++$j;
55
                    continue;
56
                }
57
58
                $this->commandBus()->handle(
59
                    new AddOrganizationMemberToOrganizationCommand(
60
                        $memberId,
61
                        $command->id(),
62
                        $ownerId
63
                    )
64
                );
65
                ++$j;
66
                $memberIds[] = $memberId;
67
            }
68
            $memberIds = [];
69
            ++$i;
70
        }
71
    }
72
}
73