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\Boot\Service\Provisioner\MessageBusProvisioner; |
12
|
|
|
use Daikon\EventSourcing\Aggregate\Command\CommandInterface; |
13
|
|
|
use Daikon\MessageBus\MessageBusInterface; |
14
|
|
|
use Daikon\Metadata\MetadataInterface; |
15
|
|
|
use ReflectionClass; |
16
|
|
|
|
17
|
|
|
abstract class Fixture implements FixtureInterface |
18
|
|
|
{ |
19
|
|
|
protected MessageBusInterface $messageBus; |
20
|
|
|
|
21
|
|
|
abstract protected function import(): void; |
22
|
|
|
|
23
|
|
|
public function __invoke(MessageBusInterface $messageBus): void |
24
|
|
|
{ |
25
|
|
|
$this->messageBus = $messageBus; |
26
|
|
|
$this->import(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getName(): string |
30
|
|
|
{ |
31
|
|
|
$shortName = (new ReflectionClass(static::class))->getShortName(); |
32
|
|
|
if (!preg_match('/^(?<name>.+?)\d+$/', $shortName, $matches)) { |
33
|
|
|
throw new FixtureException("Unexpected fixture name in '$shortName'."); |
34
|
|
|
} |
35
|
|
|
return $matches['name']; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getVersion(): int |
39
|
|
|
{ |
40
|
|
|
$shortName = (new ReflectionClass(static::class))->getShortName(); |
41
|
|
|
if (!preg_match('/(?<version>\d{14})$/', $shortName, $matches)) { |
42
|
|
|
throw new FixtureException("Unexpected fixture version in '$shortName'."); |
43
|
|
|
} |
44
|
|
|
return intval($matches['version']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function toNative(): array |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
|
|
'@type' => static::class, |
51
|
|
|
'name' => $this->getName(), |
52
|
|
|
'version' => $this->getVersion() |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function publish(CommandInterface $command, MetadataInterface $metadata = null): void |
57
|
|
|
{ |
58
|
|
|
$this->messageBus->publish($command, MessageBusProvisioner::COMMANDS_CHANNEL, $metadata); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|