CollectionRepository   B
last analyzed

Complexity

Total Complexity 50

Size/Duplication

Total Lines 206
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 206
ccs 0
cts 171
cp 0
rs 8.6206
c 0
b 0
f 0
wmc 50

27 Methods

Rating   Name   Duplication   Size   Complexity  
A find() 0 7 2
A remove() 0 8 2
A isConsumed() 0 3 1
A offsetUnset() 0 3 1
A valid() 0 3 1
A filter() 0 6 1
A offsetGet() 0 7 2
A search() 0 6 1
A next() 0 3 1
A append() 0 5 1
B sort() 0 12 8
A __construct() 0 7 2
A offsetExists() 0 3 1
A isModified() 0 3 1
A key() 0 3 1
A offsetSet() 0 6 2
A limit() 0 4 1
A reset() 0 4 1
A current() 0 7 2
B reject() 0 11 9
A rewind() 0 3 1
A toArray() 0 3 1
A __clone() 0 3 1
A count() 0 3 1
A change() 0 4 1
A any() 0 10 2
A all() 0 10 2

How to fix   Complexity   

Complex Class

Complex classes like CollectionRepository often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CollectionRepository, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace vakata\orm;
3
4
use \vakata\collection\Collection;
5
6
class CollectionRepository implements Repository
7
{
8
    /**
9
     * @var Collection
10
     */
11
    protected $collection;
12
    /**
13
     * @var Collection
14
     */
15
    protected $original;
16
    /**
17
     * @var array
18
     */
19
    protected $id = false;
20
    /**
21
     * @var callable
22
     */
23
    protected $searching = null;
24
    /**
25
     * @var bool
26
     */
27
    protected $consumed = false;
28
    /**
29
     * @var bool
30
     */
31
    protected $modified = false;
32
33
    public function __construct(Collection $collection, $id, callable $searching = null)
34
    {
35
        $this->collection = $collection;
36
        $this->original = clone $collection;
37
        $this->id = is_array($id) ? $id : [$id];
38
        $this->searching = $searching ?? function () {
39
            return true;
40
        };
41
    }
42
    public function __clone()
43
    {
44
        $this->reset();
45
    }
46
    public function find($key)
47
    {
48
        $where = [];
49
        foreach ($this->id as $field) {
50
            $where[$field] = $key[$field] ?? array_shift($key) ?? null;
51
        }
52
        return $this->collection->where($where)[0];
53
    }
54
    public function filter(string $column, $value) : Repository
55
    {
56
        $where = [];
57
        $where[$column] = $value;
58
        $this->collection = $this->collection->where($where);
59
        return $this;
60
    }
61
    public function reject(string $column, $value) : Repository
62
    {
63
        $this->collection = $this->collection->reject(function ($v) use ($column, $value) {
64
            $strict = false;
65
            $vv = is_object($v) ? (isset($v->{$column}) ? $v->{$column} : null) : (isset($v[$column]) ? $v[$column] : null);
66
            if (!$vv || ($strict && $vv !== $value) || (!$strict && $vv != $value)) {
67
                return false;
68
            }
69
            return true;
70
        });
71
        return $this;
72
    }
73
    public function sort(string $column, bool $desc = false) : Repository
74
    {
75
        $this->collection = $this->collection->sortBy(function ($a, $b) use ($column, $desc) {
76
            $v1 = is_object($a) ?
77
                (isset($a->{$column}) ? $a->{$column} : null) :
78
                (isset($a[$column]) ? $a[$column] : null);
79
            $v2 = is_object($b) ?
80
                (isset($b->{$column}) ? $b->{$column} : null) :
81
                (isset($b[$column]) ? $b[$column] : null);
82
            return $desc ? $v2 <=> $v1 : $v1 <=> $v2;
83
        });
84
        return $this;
85
    }
86
    public function limit(int $limit, int $offset = 0) : Repository
87
    {
88
        $this->collection = $this->collection->rest($offset)->first($limit);
89
        return $this;
90
    }
91
    public function count() : int
92
    {
93
        return $this->collection->count();
94
    }
95
    public function reset() : Repository
96
    {
97
        $this->collection = $this->original;
98
        return $this;
99
    }
100
    public function current()
101
    {
102
        if(!($data = $this->collection->current())) {
103
            return null;
104
        }
105
        $this->consumed = true;
106
        return $data;
107
    }
108
    public function offsetGet($offset)
109
    {
110
        if(!($data = $this->collection->offsetGet($offset))) {
111
            return null;
112
        }
113
        $this->consumed = true;
114
        return $data;
115
    }
116
117
    public function append($entity) : Repository
118
    {
119
        $this->modified = true;
120
        $this->collection[] = $entity;
121
        return $this;
122
    }
123
    public function change($entity) : Repository
124
    {
125
        $this->modified = true;
126
        return $this;
127
    }
128
    public function remove($entity) : Repository
129
    {
130
        $k = $this->collection->indexOf($entity);
131
        if ($k !== false) {
132
            $this->modified = true;
133
            unset($this->collection[$k]);
134
        }
135
        return $this;
136
    }
137
138
    public function key()
139
    {
140
        return $this->collection->key();
141
    }
142
    public function rewind()
143
    {
144
        $this->collection->rewind();
145
    }
146
    public function next()
147
    {
148
        $this->collection->next();
149
    }
150
    public function valid()
151
    {
152
        return $this->collection->valid();
153
    }
154
    public function offsetExists($offset)
155
    {
156
        return $this->collection->offsetExists($offset);
157
    }
158
    public function offsetUnset($offset)
159
    {
160
        $this->collection->offsetUnset($offset);
161
    }
162
    public function offsetSet($offset, $value)
163
    {
164
        if ($offset !== null) {
165
            throw new \BadMethodCallException();
166
        }
167
        $this->append($value);
168
    }
169
170
    public function isConsumed() : bool
171
    {
172
        return $this->consumed;
173
    }
174
    public function isModified() : bool
175
    {
176
        return $this->modified;
177
    }
178
    public function toArray($entity) : array
179
    {
180
        return [];
181
    }
182
183
    public function search(string $q) : Repository
184
    {
185
        $this->collection = $this->collection->filter(function ($v) use ($q) {
186
            return call_user_func($this->searching, $v, $q);
187
        });
188
        return $this;
189
    }
190
191
    public function any(array $criteria) : Repository
192
    {
193
        $where = [];
194
        foreach ($criteria as $row) {
195
            $temp = [];
196
            $temp[$row[0]] = $row[1];
197
            $where[] = $temp;
198
        }
199
        $this->collection = $this->collection->whereAny($where);
200
        return $this;
201
    }
202
    public function all(array $criteria) : Repository
203
    {
204
        $where = [];
205
        foreach ($criteria as $row) {
206
            $temp = [];
207
            $temp[$row[0]] = $row[1];
208
            $where[] = $temp;
209
        }
210
        $this->collection = $this->collection->whereAll($where);
211
        return $this;
212
    }
213
}
214