|
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
|
|
View Code Duplication |
protected function getRandomByIndex(int $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(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
|
|
|
|
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
@returnannotation as described here.