InMemoryEventStoreRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 18.37 %

Importance

Changes 0
Metric Value
dl 18
loc 98
rs 10
c 0
b 0
f 0
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A buildEventAggregateAsArray() 17 17 2
A count() 0 3 2
A buildEventAggregateAsObject() 0 3 1
A save() 0 8 1
A __construct() 0 3 1
A buildEventAggregate() 0 7 2
A byUuid() 0 3 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * This file is part of the Simple EventStore Manager package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace SimpleEventStoreManager\Infrastructure\Persistence;
12
13
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface;
14
use SimpleEventStoreManager\Domain\Model\Contracts\EventStoreRepositoryInterface;
15
use SimpleEventStoreManager\Domain\Model\Event;
16
use SimpleEventStoreManager\Domain\Model\AggregateUuid;
17
18
class InMemoryEventStoreRepository implements EventStoreRepositoryInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    private $events;
24
25
    /**
26
     * InMemoryEventRepository constructor.
27
     */
28
    public function __construct()
29
    {
30
        $this->events = [];
31
    }
32
33
    /**
34
     * @param AggregateUuid $uuid
35
     * @param int $returnType
36
     *
37
     * @return array|mixed|null
38
     */
39
    public function byUuid(AggregateUuid $uuid, $returnType = self::RETURN_AS_ARRAY)
40
    {
41
        return (isset($this->events[(string) $uuid])) ? $this->buildEventAggregate($uuid, $returnType) : null;
42
    }
43
44
    /**
45
     * @param AggregateUuid $uuid
46
     * @param $returnType
47
     *
48
     * @return array|mixed
49
     */
50
    private function buildEventAggregate(AggregateUuid $uuid, $returnType)
51
    {
52
        if ($returnType === self::RETURN_AS_ARRAY) {
53
            return $this->buildEventAggregateAsArray($uuid);
54
        }
55
56
        return $this->buildEventAggregateAsObject($uuid);
57
    }
58
59
    /**
60
     * @param AggregateUuid $uuid
61
     *
62
     * @return array
63
     */
64 View Code Duplication
    private function buildEventAggregateAsArray(AggregateUuid $uuid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        $returnArray = [];
67
68
        /** @var Event $event */
69
        foreach ($this->events[(string) $uuid] as $event) {
70
            $returnArray[] = [
71
                'uuid' => $event->uuid(),
72
                'version' => $event->version(),
73
                'payload' => $event->payload(),
74
                'type' => $event->type(),
75
                'body' => $event->body(),
76
                'occurred_on' => $event->occurredOn(),
77
            ];
78
        }
79
80
        return $returnArray;
81
    }
82
83
    /**
84
     * @param AggregateUuid $uuid
85
     *
86
     * @return mixed
87
     */
88
    private function buildEventAggregateAsObject(AggregateUuid $uuid)
89
    {
90
        return $this->events[(string) $uuid];
91
    }
92
93
    /**
94
     * @param AggregateUuid $uuid
95
     *
96
     * @return int
97
     */
98
    public function count(AggregateUuid $uuid)
99
    {
100
        return (isset($this->events[(string) $uuid])) ? count($this->events[(string) $uuid]) : 0;
101
    }
102
103
    /**
104
     * @param EventInterface $event
105
     *
106
     * @return mixed
107
     */
108
    public function save(EventInterface $event)
109
    {
110
        $this->events[(string) $event->uuid()][] = new $event(
111
            $event->uuid(),
0 ignored issues
show
Unused Code introduced by
The call to SimpleEventStoreManager\...nterface::__construct() has too many arguments starting with $event->uuid(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
        $this->events[(string) $event->uuid()][] = /** @scrutinizer ignore-call */ new $event(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
112
            $event->type(),
113
            $event->body(),
114
            $this->count($event->uuid()),
115
            $event->occurredOn()->format('Y-m-d H:i:s.u')
116
        );
117
    }
118
}
119