AggregateRepository::remove()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 7
Ratio 41.18 %

Importance

Changes 0
Metric Value
dl 7
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Cubiche\Domain\EventSourcing;
13
14
use Cubiche\Domain\EventPublisher\DomainEventPublisher;
15
use Cubiche\Domain\EventSourcing\Event\PostPersistEvent;
16
use Cubiche\Domain\EventSourcing\Event\PostRemoveEvent;
17
use Cubiche\Domain\EventSourcing\Event\PrePersistEvent;
18
use Cubiche\Domain\EventSourcing\Event\PreRemoveEvent;
19
use Cubiche\Domain\EventSourcing\EventStore\EventStoreInterface;
20
use Cubiche\Domain\EventSourcing\EventStore\EventStream;
21
use Cubiche\Domain\EventSourcing\Utils\NameResolver;
22
use Cubiche\Domain\Model\IdInterface;
23
use Cubiche\Domain\Repository\RepositoryInterface;
24
25
/**
26
 * AggregateRepository class.
27
 *
28
 * @author Ivannis Suárez Jerez <[email protected]>
29
 */
30
class AggregateRepository implements RepositoryInterface
31
{
32
    /**
33
     * @var EventStoreInterface
34
     */
35
    protected $eventStore;
36
37
    /**
38
     * @var string
39
     */
40
    protected $aggregateClassName;
41
42
    /**
43
     * AggregateRepository constructor.
44
     *
45
     * @param EventStoreInterface $eventStore
46
     * @param string              $aggregateClassName
47
     */
48
    public function __construct(EventStoreInterface $eventStore, $aggregateClassName)
49
    {
50
        $this->eventStore = $eventStore;
51
        $this->aggregateClassName = $aggregateClassName;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function get(IdInterface $id)
58
    {
59
        $eventStream = $this->loadHistory($id);
60
61
        if ($eventStream !== null) {
62
            return call_user_func(
63
                array($this->aggregateClassName, 'loadFromHistory'),
64
                $eventStream
65
            );
66
        }
67
68
        return;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function persist($element)
75
    {
76 View Code Duplication
        if (!$element instanceof EventSourcedAggregateRootInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
            throw new \InvalidArgumentException(sprintf(
78
                'The object must be an instance of %s. Instance of %s given',
79
                EventSourcedAggregateRootInterface::class,
80
                is_object($element) ? get_class($element) : gettype($element)
81
            ));
82
        }
83
84
        $this->saveHistory($element);
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function persistAll($elements)
91
    {
92
        foreach ($elements as $element) {
93
            $this->persist($element);
94
        }
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function remove($element)
101
    {
102 View Code Duplication
        if (!$element instanceof EventSourcedAggregateRootInterface) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            throw new \InvalidArgumentException(sprintf(
104
                'The object must be an instance of %s. Instance of %s given',
105
                EventSourcedAggregateRootInterface::class,
106
                is_object($element) ? get_class($element) : gettype($element)
107
            ));
108
        }
109
110
        DomainEventPublisher::publish(new PreRemoveEvent($element));
111
112
        // remove the event stream
113
        $this->eventStore->remove($this->streamName($element->id()));
114
115
        DomainEventPublisher::publish(new PostRemoveEvent($element));
116
    }
117
118
    /**
119
     * Load a aggregate history from the storage.
120
     *
121
     * @param IdInterface $id
122
     *
123
     * @return EventStream|null
124
     */
125
    protected function loadHistory(IdInterface $id)
126
    {
127
        return $this->eventStore->load($this->streamName($id));
128
    }
129
130
    /**
131
     * Save the aggregate history.
132
     *
133
     * @param EventSourcedAggregateRootInterface $aggregateRoot
134
     */
135
    protected function saveHistory(EventSourcedAggregateRootInterface $aggregateRoot)
136
    {
137
        $recordedEvents = $aggregateRoot->recordedEvents();
138
        if (count($recordedEvents) > 0) {
139
            DomainEventPublisher::publish(new PrePersistEvent($aggregateRoot));
140
141
            // clear events
142
            $aggregateRoot->clearEvents();
143
144
            // create the eventStream and persist it
145
            $eventStream = new EventStream(
146
                $this->streamName($aggregateRoot->id()),
147
                $aggregateRoot->id(),
148
                $recordedEvents
149
            );
150
151
            $this->eventStore->persist($eventStream);
152
153
            DomainEventPublisher::publish(new PostPersistEvent($aggregateRoot, $eventStream));
154
        }
155
    }
156
157
    /**
158
     * @param IdInterface $id
159
     *
160
     * @return string
161
     */
162
    protected function streamName(IdInterface $id)
163
    {
164
        return NameResolver::resolve($this->aggregateClassName, $id);
165
    }
166
}
167