AbstractRepository::getIndexInRangeDate()   B
last analyzed

Complexity

Conditions 9
Paths 2

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 12
nc 2
nop 2
dl 0
loc 20
rs 8.0555
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the InMemoryList 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 InMemoryList\Infrastructure\Persistance;
12
13
use InMemoryList\Infrastructure\Persistance\Exceptions\ListElementDoesNotExistsException;
14
15
abstract class AbstractRepository
16
{
17
    /**
18
     * @param $listUuid
19
     *
20
     * @return mixed
21
     */
22
    public function delete($listUuid)
23
    {
24
        $list = $this->findListByUuid($listUuid);
0 ignored issues
show
Bug introduced by
The method findListByUuid() does not exist on InMemoryList\Infrastruct...ance\AbstractRepository. Since it exists in all sub-types, consider adding an abstract or default implementation to InMemoryList\Infrastruct...ance\AbstractRepository. ( Ignorable by Annotation )

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

24
        /** @scrutinizer ignore-call */ 
25
        $list = $this->findListByUuid($listUuid);
Loading history...
25
26
        foreach (array_keys($list) as $elementUuid) {
27
            $this->deleteElement($listUuid, $elementUuid);
0 ignored issues
show
Bug introduced by
The method deleteElement() does not exist on InMemoryList\Infrastruct...ance\AbstractRepository. Did you maybe mean delete()? ( Ignorable by Annotation )

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

27
            $this->/** @scrutinizer ignore-call */ 
28
                   deleteElement($listUuid, $elementUuid);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        }
29
    }
30
31
    /**
32
     * @param $listUuid
33
     * @param $elementUuid
34
     *
35
     * @return mixed
36
     */
37
    public function existsElement($listUuid, $elementUuid)
38
    {
39
        return isset($this->findListByUuid($listUuid)[$elementUuid]);
40
    }
41
42
    /**
43
     * @param $listUuid
44
     * @param $elementUuid
45
     *
46
     * @return mixed
47
     *
48
     * @throws ListElementDoesNotExistsException
49
     */
50
    public function findElement($listUuid, $elementUuid)
51
    {
52
        if (!$this->existsElement($listUuid, $elementUuid)) {
53
            throw new ListElementDoesNotExistsException('Cannot retrieve the element '.$elementUuid.' from the collection in memory.');
54
        }
55
56
        return $this->findListByUuid($listUuid)[$elementUuid];
57
    }
58
59
    /**
60
     * @param $listUuid
61
     *
62
     * @return mixed
63
     */
64
    public function getChunkSize($listUuid)
65
    {
66
        if ($this->existsListInIndex($listUuid)) {
67
            $index = $this->getIndex($listUuid);
0 ignored issues
show
Bug introduced by
The method getIndex() does not exist on InMemoryList\Infrastruct...ance\AbstractRepository. Did you maybe mean getIndexInRangeDate()? ( Ignorable by Annotation )

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

67
            /** @scrutinizer ignore-call */ 
68
            $index = $this->getIndex($listUuid);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
69
            return $index['chunk-size'];
70
        }
71
72
        return 0;
73
    }
74
75
    /**
76
     * @param $listUuid
77
     *
78
     * @return mixed
79
     */
80
    public function getCounter($listUuid)
81
    {
82
        if ($this->existsListInIndex($listUuid)) {
83
            $index = $this->getIndex($listUuid);
84
85
            return $index['size'];
86
        }
87
88
        return 0;
89
    }
90
91
    /**
92
     * @param $listUuid
93
     *
94
     * @return bool
95
     */
96
    public function existsListInIndex($listUuid)
97
    {
98
        return ($this->getIndex($listUuid)) ? true : false;
99
    }
100
101
    /**
102
     * @param $listUuid
103
     *
104
     * @return float
105
     */
106
    public function getNumberOfChunks($listUuid)
107
    {
108
        if ($this->existsListInIndex($listUuid)) {
109
            $index = $this->getIndex($listUuid);
110
111
            return $index['chunks'];
112
        }
113
114
        return 0;
115
    }
116
117
    /**
118
     * @param \Datetime|null $from
119
     * @param \Datetime|null $to
120
     *
121
     * @return array
122
     */
123
    public function getIndexInRangeDate(\Datetime $from = null, \Datetime $to = null)
124
    {
125
        $results = [];
126
127
        if ($index = $this->getIndex()) {
128
            foreach ($index as $key => $item) {
129
                $unserializedItem = $item;
130
                $createdOn = $unserializedItem['created_on']->format('Y-m-d');
131
                $from = $from ? $from->format('Y-m-d') : null;
132
                $to = $to ? $to->format('Y-m-d') : null;
133
                $firstStatement = ($from) ? $createdOn >= $from : true;
134
                $secondStatement = ($to) ? $createdOn <= $to : true;
135
136
                if ($firstStatement && $secondStatement) {
137
                    $results[$key] = $item;
138
                }
139
            }
140
        }
141
142
        return $results;
143
    }
144
145
    /**
146
     * @param $listUuid
147
     *
148
     * @return mixed
149
     */
150
    public function getTtl($listUuid)
151
    {
152
        $index = $this->getIndex($listUuid);
153
        if ($index['ttl'] && $index['ttl'] > 0) {
154
            $now = new \DateTime('NOW');
155
            $expire_date = $index['created_on']->add(new \DateInterval('PT'.$index['ttl'].'S'));
156
            $diffSeconds = $expire_date->getTimestamp() - $now->getTimestamp();
157
158
            return $diffSeconds;
159
        }
160
161
        return -1;
162
    }
163
164
    /**
165
     * @param array $index
166
     */
167
    protected function removeExpiredListsFromIndex($index)
168
    {
169
        if (is_array($index)) {
0 ignored issues
show
introduced by
The condition is_array($index) is always true.
Loading history...
170
            foreach (array_keys($index) as $key) {
171
                if (false === $this->exists($key) && isset($index[$key])) {
0 ignored issues
show
Bug introduced by
The method exists() does not exist on InMemoryList\Infrastruct...ance\AbstractRepository. Did you maybe mean existsListInIndex()? ( Ignorable by Annotation )

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

171
                if (false === $this->/** @scrutinizer ignore-call */ exists($key) && isset($index[$key])) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
172
                    $this->removeListFromIndex($key);
0 ignored issues
show
Bug introduced by
The method removeListFromIndex() does not exist on InMemoryList\Infrastruct...ance\AbstractRepository. Since it exists in all sub-types, consider adding an abstract or default implementation to InMemoryList\Infrastruct...ance\AbstractRepository. ( Ignorable by Annotation )

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

172
                    $this->/** @scrutinizer ignore-call */ 
173
                           removeListFromIndex($key);
Loading history...
173
                }
174
            }
175
        }
176
    }
177
178
    /**
179
     * @param $listElement
180
     * @param $data
181
     *
182
     * @return array|object
183
     */
184
    protected function updateListElementBody($listElement, $data)
185
    {
186
        $listElement = $listElement;
187
188
        if (is_string($listElement)) {
189
            return $data;
190
        }
191
192
        if (is_array($listElement)) {
193
            return array_merge((array) $listElement, (array) $data);
194
        }
195
196
        return (object) array_merge((array) $listElement, (array) $data);
197
    }
198
}
199