Completed
Push — master ( 7fe5eb...9bad48 )
by Beñat
08:54 queued 04:27
created

AbstractFixture::fakeDataDir()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 15
nc 5
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\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 View Code Duplication
    protected function getRandomByIndex(int $index, array $list = null) : string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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(int $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(int $first, int $second) : int
81
    {
82
        $result = $first % $second;
83
84
        return $result < 0 ? $result + $second : $result;
85
    }
86
87
    private function fakeDataDir() : string
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
        $notifierDir = sprintf($baseDir, 'Notifier');
95
96
        if ($this->type() === 'user') {
97
            return __DIR__ . '/../DataFixtures/FakeData';
98
        }
99
        if (is_dir($identityAccessDir)) {
100
            return $identityAccessDir;
101
        }
102
        if (is_dir($taskManagerDir)) {
103
            return $taskManagerDir;
104
        }
105
        if (is_dir($notifierDir)) {
106
            return $notifierDir;
107
        }
108
        throw new Exception('The identity access and task manager fake data dirs are not valid');
109
    }
110
}
111