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

AbstractFixture::getRandomUserByIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
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\SharedKernel\Infrastructure\Persistence\Doctrine\DataFixtures;
16
17
use Doctrine\Common\DataFixtures\AbstractFixture as BaseAbstractFixture;
18
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
19
use Kreta\SharedKernel\Application\QueryBus;
20
use Kreta\SharedKernel\Domain\Model\Exception;
21
use Symfony\Component\Yaml\Yaml;
22
23
abstract class AbstractFixture extends BaseAbstractFixture implements OrderedFixtureInterface
24
{
25
    protected $commandBus;
26
    protected $queryBus;
27
28
    abstract protected function type() : string;
29
30
    // It does not possible enable this return type because IdentityAccess,
31
    // uses the BenGorUser so, the command bus is not a shared kernel's
32
    // command bus instance.
33
    protected function commandBus() // : \Kreta\SharedKernel\Application\CommandBus
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...
34
    {
35
        return $this->commandBus;
36
    }
37
38
    protected function queryBus() : QueryBus
39
    {
40
        return $this->queryBus;
41
    }
42
43
    protected function fakeIds() : array
44
    {
45
        return Yaml::parse(
46
            file_get_contents(
47
                $this->fakeDataDir() . '/' . $this->type() . '_ids.yml'
48
            )
49
        );
50
    }
51
52
    protected function amount() : int
53
    {
54
        return count($this->fakeIds());
55
    }
56
57
    protected function getRandomByIndex($index, array $list = null) : string
58
    {
59
        $list = null === $list ? $this->fakeIds() : $list;
60
61
        $numberOfUserIds = count($list);
62
        $j = $this->mod($index, $numberOfUserIds - 1) > $numberOfUserIds
63
            ? 0
64
            : $this->mod(($numberOfUserIds - ($index - 1)), $numberOfUserIds);
65
66
        return $list[$j];
67
    }
68
69
    protected function getRandomUserByIndex($index) : string
70
    {
71
        $userIds = Yaml::parse(
72
            file_get_contents(
73
                __DIR__ . '/../DataFixtures/FakeData/user_ids.yml'
74
            )
75
        );
76
77
        return $this->getRandomByIndex($index, $userIds);
78
    }
79
80
    private function mod($first, $second)
81
    {
82
        $result = $first % $second;
83
84
        return $result < 0 ? $result + $second : $result;
85
    }
86
87
    private function fakeDataDir()
88
    {
89
        $baseDir = __DIR__ . '/../../../../../../../../../../src/Kreta' .
90
            '/%s/Infrastructure/Persistence/Doctrine/DataFixtures/FakeData/';
91
92
        $identityAccessDir = sprintf($baseDir, 'IdentityAccess');
93
        $taskManagerDir = sprintf($baseDir, 'TaskManager');
94
95
        if (is_dir($identityAccessDir)) {
96
            return $identityAccessDir;
97
        }
98
        if (is_dir($taskManagerDir)) {
99
            return $taskManagerDir;
100
        }
101
        if ($this->type() === 'user') {
102
            return __DIR__ . '/../DataFixtures/FakeData';
103
        }
104
        throw new Exception('The identity access and task manager fake data dirs are not valid');
105
    }
106
}
107