Test Failed
Push — master ( 599853...c631b6 )
by Mikael
02:12
created

RemServer::getDefaultDataset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Anax\RemServer;
4
5
use Anax\Session\SessionInterface;
6
7
/**
8
 * REM Server using session to store information.
9
 */
10
class RemServer
11
{
12
    /**
13
     * @var array  $dataset the dataset to add as default dataset.
14
     * @var object $session inject a reference to the session.
15
     * @var string $key to use when storing in session.
16
     */
17
    private $dataset = [];
18
    protected $session;
19
    const KEY = "remserver";
20
21
22
23
    /**
24
     * Inject dependency to $session.
25
     *
26
     * @param SessionInterface $session object representing session.
27
     *
28
     * @return self
29
     */
30 1
    public function injectSession(SessionInterface $session) : object
31
    {
32 1
        $this->session = $session;
33 1
        return $this;
34
    }
35
36
37
38
    /**
39
     * Set the default dataset to use.
40
     *
41
     * @param array $dataset array with absolute paths to json files to load.
42
     *
43
     * @return self
44
     */
45
    public function setDefaultDataset(array $dataset) : object
46
    {
47
        $this->dataset = $dataset;
48
        return $this;
49
    }
50
51
52
53
    /**
54
     * Get the default dataset that is used.
55
     *
56
     * @return self
57
     */
58
    public function getDefaultDataset() : array
59
    {
60
        return $this->dataset;
61
    }
62
63
64
65
    /**
66
     * Fill the session with default data that are read from files.
67
     *
68
     * @throws Exception when bad configuration.
69
     *
70
     * @return self
71
     */
72
    public function init()
73
    {
74
        $json = [];
75
        foreach ($this->dataset as $file) {
76
            $content = file_get_contents($file);
77
            $key = pathinfo($file, PATHINFO_FILENAME);
78
            $json[$key] = json_decode($content, true);
79
        }
80
81
        $this->session->set(self::KEY, $json);
82
        return $this;
83
    }
84
85
86
87
    /**
88
     * Check if there is a dataset stored.
89
     *
90
     * @return boolean true if dataset exists in session, else false
91
     */
92
    public function hasDataset()
93
    {
94
        return($this->session->has(self::KEY));
95
    }
96
97
98
99
    /**
100
     * Get (or create) a subset of data.
101
     *
102
     * @param string $key for data subset.
103
     *
104
     * @return array with the dataset
105
     */
106
    public function getDataset($key)
107
    {
108
        $data = $this->session->get(self::KEY);
109
        $set = isset($data[$key])
110
            ? $data[$key]
111
            : [];
112
        return $set;
113
    }
114
115
116
117
    /**
118
     * Save (the modified) dataset.
119
     *
120
     * @param string $key     for data subset.
121
     * @param array  $dataset the data to save.
122
     *
123
     * @return self
124
     */
125
    public function saveDataset($key, $dataset)
126
    {
127
        $data = $this->session->get(self::KEY);
128
        $data[$key] = $dataset;
129
        $this->session->set(self::KEY, $data);
130
        return $this;
131
    }
132
133
134
135
    /**
136
     * Get an item from a dataset.
137
     *
138
     * @param string $key    for the dataset
139
     * @param string $itemId for the item to get
140
     *
141
     * @return array|null array with item if found, else null
142
     */
143
    public function getItem($key, $itemId)
144
    {
145
        $dataset = $this->getDataset($key);
146
147
        foreach ($dataset as $item) {
148
            if ($item["id"] === $itemId) {
149
                return $item;
150
            }
151
        }
152
        return null;
153
    }
154
155
156
157
    /**
158
     * Add an item to a dataset.
159
     *
160
     * @param string $key  for the dataset
161
     * @param array  $item to add
162
     *
163
     * @return array as new item inserted
164
     */
165
    public function addItem($key, $item)
166
    {
167
        $dataset = $this->getDataset($key);
168
169
        // Get max value for the id
170
        $max = 0;
171
        foreach ($dataset as $val) {
172
            if ($max < $val["id"]) {
173
                $max = $val["id"];
174
            }
175
        }
176
        $item["id"] = $max + 1;
177
        $dataset[] = $item;
178
        $this->saveDataset($key, $dataset);
179
        return $item;
180
    }
181
182
183
184
    /**
185
     * Upsert/replace an item to a dataset.
186
     *
187
     * @param string $keyDataset for the dataset
188
     * @param string $itemId     where to store it
189
     * @param string $entry      to add
190
     *
191
     * @return array as item upserted
192
     */
193
    public function upsertItem($keyDataset, $itemId, $entry)
194
    {
195
        $dataset = $this->getDataset($keyDataset);
196
        $entry["id"] = $itemId;
197
198
        // Find the item if it exists
199
        $found = false;
200
        foreach ($dataset as $key => $val) {
201
            if ($itemId === $val["id"]) {
202
                $dataset[$key] = $entry;
203
                $found = true;
204
                break;
205
            }
206
        }
207
208
        if (!$found) {
209
            $dataset[] = $entry;
210
        }
211
212
        $this->saveDataset($keyDataset, $dataset);
213
        return $entry;
214
    }
215
216
217
218
    /**
219
     * Delete an item from the dataset.
220
     *
221
     * @param string $keyDataset for the dataset
222
     * @param string $itemId     to delete
223
     *
224
     * @return void
225
     */
226
    public function deleteItem($keyDataset, $itemId)
227
    {
228
        $dataset = $this->getDataset($keyDataset);
229
230
        // Find the item if it exists
231
        foreach ($dataset as $key => $val) {
232
            if ($itemId === $val["id"]) {
233
                unset($dataset[$key]);
234
                break;
235
            }
236
        }
237
        $this->saveDataset($keyDataset, $dataset);
238
    }
239
}
240