Fixture   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 42
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 7 2
A toNative() 0 6 1
A publish() 0 3 1
A getVersion() 0 7 2
A __invoke() 0 4 1
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