GenericStoreStream::fromAggregateData()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 17
rs 9.9666
1
<?php
2
3
/*
4
 * event-sourcing (https://github.com/phpgears/event-sourcing).
5
 * Event Sourcing base.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/event-sourcing
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\EventSourcing\Store;
15
16
use Gears\EventSourcing\Aggregate\AggregateRoot;
17
use Gears\EventSourcing\Aggregate\Exception\AggregateException;
18
use Gears\Identity\Identity;
19
use Gears\Immutability\ImmutabilityBehaviour;
20
21
/**
22
 * GenericStoreStream class.
23
 */
24
final class GenericStoreStream implements StoreStream
25
{
26
    use ImmutabilityBehaviour;
27
28
    /**
29
     * @var string
30
     */
31
    private $aggregateRootClass;
32
33
    /**
34
     * @var Identity
35
     */
36
    private $aggregateId;
37
38
    /**
39
     * SimpleStoreStream constructor.
40
     *
41
     * @param string   $aggregateRootClass
42
     * @param Identity $aggregateId
43
     */
44
    private function __construct(string $aggregateRootClass, Identity $aggregateId)
45
    {
46
        $this->assertImmutable();
47
48
        $this->aggregateRootClass = $aggregateRootClass;
49
        $this->aggregateId = $aggregateId;
50
    }
51
52
    /**
53
     * Create from aggregate data.
54
     *
55
     * @param string   $aggregateRootClass
56
     * @param Identity $aggregateId
57
     *
58
     * @throws AggregateException
59
     *
60
     * @return self
61
     */
62
    public static function fromAggregateData(string $aggregateRootClass, Identity $aggregateId): self
63
    {
64
        if (!\class_exists($aggregateRootClass)) {
65
            throw new AggregateException(
66
                \sprintf('Aggregate root class "%s" cannot be found', $aggregateRootClass)
67
            );
68
        }
69
70
        if (!\in_array(AggregateRoot::class, \class_implements($aggregateRootClass), true)) {
71
            throw new AggregateException(\sprintf(
72
                'Aggregate root class must implement "%s", "%s" given',
73
                AggregateRoot::class,
74
                $aggregateRootClass
75
            ));
76
        }
77
78
        return new self($aggregateRootClass, $aggregateId);
79
    }
80
81
    /**
82
     * Create from aggregate root.
83
     *
84
     * @param AggregateRoot $aggregateRoot
85
     *
86
     * @return GenericStoreStream
87
     */
88
    public static function fromAggregateRoot(AggregateRoot $aggregateRoot): self
89
    {
90
        return new self(\get_class($aggregateRoot), $aggregateRoot->getIdentity());
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getAggregateRootClass(): string
97
    {
98
        return $this->aggregateRootClass;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function getAggregateId(): Identity
105
    {
106
        return $this->aggregateId;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     *
112
     * @return string[]
113
     */
114
    protected function getAllowedInterfaces(): array
115
    {
116
        return [StoreStream::class];
117
    }
118
}
119