RedisEventStoreRepository   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 14.52 %

Importance

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

7 Methods

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

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 Predis\Client;
14
use SimpleEventStoreManager\Domain\Model\Contracts\EventInterface;
15
use SimpleEventStoreManager\Domain\Model\Contracts\EventStoreRepositoryInterface;
16
use SimpleEventStoreManager\Domain\Model\Event;
17
use SimpleEventStoreManager\Domain\Model\AggregateUuid;
18
19
class RedisEventStoreRepository implements EventStoreRepositoryInterface
20
{
21
    /**
22
     * @var Client
23
     */
24
    private $client;
25
26
    /**
27
     * @var int
28
     */
29
    private $return;
30
31
    /**
32
     * RedisEventRepository constructor.
33
     *
34
     * @param Client $client
35
     */
36
    public function __construct(Client $client, $return = self::RETURN_AS_ARRAY)
37
    {
38
        $this->client = $client;
39
        $this->return = $return;
40
    }
41
42
    /**
43
     * @param AggregateUuid $uuid
44
     * @param int $returnType
45
     * @return array|null
46
     */
47
    public function byUuid(AggregateUuid $uuid, $returnType = self::RETURN_AS_ARRAY)
48
    {
49
        $events = array_map(function($event){
50
            return unserialize($event);
51
        }, $this->client->hgetall((string) $uuid));
52
53
        if (!empty($events)) {
54
            ksort($events);
55
56
            return $this->buildEventAggregate($events, $returnType);
57
        }
58
59
        return null;
60
    }
61
62
    /**
63
     * @param array $events
64
     * @param $returnType
65
     *
66
     * @return array
67
     */
68
    private function buildEventAggregate(array $events, $returnType)
69
    {
70
        if ($returnType === self::RETURN_AS_ARRAY) {
71
            return $this->buildEventAggregateAsArray($events);
72
        }
73
74
        return $this->buildEventAggregateAsObject($events);
75
    }
76
77
    /**
78
     * @param array $events
79
     *
80
     * @return array
81
     */
82 View Code Duplication
    private function buildEventAggregateAsArray(array $events)
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...
83
    {
84
        $returnArray = [];
85
86
        /** @var Event $event */
87
        foreach ($events as $event) {
88
            $returnArray[] = [
89
                'uuid' => (string) $event->uuid(),
90
                'version' => $event->version(),
91
                'payload' => $event->payload(),
92
                'type' => $event->type(),
93
                'body' => $event->body(),
94
                'occurred_on' => $event->occurredOn()->format('Y-m-d H:i:s.u')
95
            ];
96
        }
97
98
        return $returnArray;
99
    }
100
101
    /**
102
     * @param array $events
103
     *
104
     * @return array
105
     */
106
    private function buildEventAggregateAsObject(array $events)
107
    {
108
        $returnObject = [];
109
110
        /** @var Event $event */
111
        foreach ($events as $event) {
112
            $returnObject[] = $event;
113
        }
114
115
        return $returnObject;
116
    }
117
118
    /**
119
     * @param AggregateUuid $uuid
120
     *
121
     * @return int
122
     */
123
    public function count(AggregateUuid $uuid)
124
    {
125
        return count($this->client->hgetall((string) $uuid));
126
    }
127
128
    /**
129
     * @param EventInterface $event
130
     */
131
    public function save(EventInterface $event)
132
    {
133
        $this->client->hset(
134
            (string) $event->uuid(),
135
            $this->count($event->uuid()),
136
            serialize(
137
                new $event(
138
                    $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

138
                /** @scrutinizer ignore-call */ 
139
                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...
139
                    $event->type(),
140
                    $event->body(),
141
                    $this->count($event->uuid()),
142
                    $event->occurredOn()->format('Y-m-d H:i:s.u')
143
                )
144
            )
145
        );
146
    }
147
}
148