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 |
|
|
|
|
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
|
|
|
|
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.