Collection   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 150
rs 10
c 0
b 0
f 0
wmc 24

8 Methods

Rating   Name   Duplication   Size   Complexity  
A update() 0 14 4
A delete() 0 14 4
A fetchOne() 0 4 2
A drop() 0 3 1
B fetchAll() 0 17 5
A insertBatch() 0 15 4
A __construct() 0 3 1
A insert() 0 13 3
1
<?php
2
3
namespace WebComplete\microDb;
4
5
use Closure;
6
7
class Collection
8
{
9
    /**
10
     * @var StorageInterface
11
     */
12
    protected $storage;
13
14
    /**
15
     * @param StorageInterface $storage
16
     */
17
    public function __construct(StorageInterface $storage)
18
    {
19
        $this->storage = $storage;
20
    }
21
22
    /**
23
     * @param Closure|null $filter
24
     * @param Closure|null $sort
25
     * @param int|null $limit
26
     * @param int $offset
27
     *
28
     * @return array
29
     * @throws \WebComplete\microDb\Exception
30
     */
31
    public function fetchAll(Closure $filter = null, Closure $sort = null, int $limit = null, int $offset = 0): array
32
    {
33
        $collectionData = $this->storage->load(false);
34
        if (!isset($collectionData['items'])) {
35
            $collectionData['items'] = [];
36
        }
37
        $items = &$collectionData['items'];
38
        if ($sort) {
39
            \uasort($items, $sort);
40
        }
41
        if ($filter) {
42
            $items = \array_filter($items, $filter);
43
        }
44
        if ((int)$limit > 0) {
45
            $items = \array_slice($items, $offset, $limit);
46
        }
47
        return \array_values($items);
48
    }
49
50
    /**
51
     * @param Closure|null $filter
52
     * @param Closure|null $sort
53
     *
54
     * @return array|null
55
     * @throws \WebComplete\microDb\Exception
56
     */
57
    public function fetchOne(Closure $filter = null, Closure $sort = null)
58
    {
59
        $result = $this->fetchAll($filter, $sort);
60
        return $result ? (array)\reset($result) : null;
0 ignored issues
show
introduced by
The condition $result can never be true.
Loading history...
61
    }
62
63
    /**
64
     * @param Closure $filter
65
     *
66
     * @throws \WebComplete\microDb\Exception
67
     */
68
    public function delete(Closure $filter)
69
    {
70
        $collectionData = $this->storage->load(true);
71
        if (!isset($collectionData['items'])) {
72
            $collectionData['items'] = [];
73
        }
74
        $items = &$collectionData['items'];
75
76
        foreach ($items as $k => $item) {
77
            if ($filter($item)) {
78
                unset($items[$k]);
79
            }
80
        }
81
        $this->storage->save($collectionData);
82
    }
83
84
    /**
85
     * @param Closure $filter
86
     * @param array $data
87
     * @param string $idField
88
     *
89
     * @throws \WebComplete\microDb\Exception
90
     */
91
    public function update(Closure $filter, array $data, string $idField = 'id')
92
    {
93
        unset($data[$idField]);
94
        $collectionData = $this->storage->load(true);
95
        if (!isset($collectionData['items'])) {
96
            $collectionData['items'] = [];
97
        }
98
        $items = &$collectionData['items'];
99
        foreach ($items as $k => $item) {
100
            if ($filter($item)) {
101
                $items[$k] = \array_merge($item, $data);
102
            }
103
        }
104
        $this->storage->save($collectionData);
105
    }
106
107
    /**
108
     * @param array $item
109
     * @param string $idField
110
     *
111
     * @return int last insert id
112
     * @throws \WebComplete\microDb\Exception
113
     */
114
    public function insert(array $item, string $idField = 'id'): int
115
    {
116
        $collectionData = $this->storage->load(true);
117
        if (!isset($collectionData['items'])) {
118
            $collectionData['items'] = [];
119
        }
120
        if (!isset($collectionData['inc'])) {
121
            $collectionData['inc'] = 1;
122
        }
123
        $item[$idField] = $collectionData['inc']++;
124
        $collectionData['items'][] = $item;
125
        $this->storage->save($collectionData);
126
        return $item[$idField];
127
    }
128
129
    /**
130
     * @param array $items
131
     * @param string $idField
132
     *
133
     * @throws \WebComplete\microDb\Exception
134
     */
135
    public function insertBatch(array $items, string $idField = 'id')
136
    {
137
        $collectionData = $this->storage->load(true);
138
        if (!isset($collectionData['items'])) {
139
            $collectionData['items'] = [];
140
        }
141
        if (!isset($collectionData['inc'])) {
142
            $collectionData['inc'] = 1;
143
        }
144
        foreach ($items as &$item) {
145
            $item[$idField] = $collectionData['inc']++;
146
        }
147
        unset($item);
148
        $collectionData['items'] = \array_merge($collectionData['items'], $items);
149
        $this->storage->save($collectionData);
150
    }
151
152
    /**
153
     */
154
    public function drop()
155
    {
156
        $this->storage->drop();
157
    }
158
}
159