1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/boot project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Boot\Fixture; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\MessageBus\MessageBusInterface; |
13
|
|
|
|
14
|
|
|
final class FixtureTarget implements FixtureTargetInterface |
15
|
|
|
{ |
16
|
|
|
private string $key; |
17
|
|
|
|
18
|
|
|
private bool $enabled; |
19
|
|
|
|
20
|
|
|
private FixtureLoaderInterface $fixtureLoader; |
21
|
|
|
|
22
|
|
|
private MessageBusInterface $messageBus; |
23
|
|
|
|
24
|
|
|
private ?FixtureList $fixtureList; |
25
|
|
|
|
26
|
|
|
public function __construct( |
27
|
|
|
string $key, |
28
|
|
|
bool $enabled, |
29
|
|
|
FixtureLoaderInterface $fixtureLoader, |
30
|
|
|
MessageBusInterface $messageBus |
31
|
|
|
) { |
32
|
|
|
$this->key = $key; |
33
|
|
|
$this->enabled = $enabled; |
34
|
|
|
$this->fixtureLoader = $fixtureLoader; |
35
|
|
|
$this->messageBus = $messageBus; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function import(FixtureInterface $fixture): bool |
39
|
|
|
{ |
40
|
|
|
Assertion::true($this->isEnabled(), sprintf("Fixture '%s' is not enabled.", $fixture->getName())); |
41
|
|
|
|
42
|
|
|
$index = $this->getFixtureList()->find($fixture); |
43
|
|
|
|
44
|
|
|
if ($index !== false) { |
45
|
|
|
$fixture($this->messageBus); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $index !== false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getKey(): string |
52
|
|
|
{ |
53
|
|
|
return $this->key; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function isEnabled(): bool |
57
|
|
|
{ |
58
|
|
|
return $this->enabled === true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getFixtureList(): FixtureList |
62
|
|
|
{ |
63
|
|
|
if (!isset($this->fixtureList)) { |
64
|
|
|
//@todo fix fixture class loading to avoid this |
65
|
|
|
$this->fixtureList = $this->fixtureLoader->load(); |
66
|
|
|
} |
67
|
|
|
return $this->fixtureList; |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|