Test Failed
Push — master ( 058028...9f33eb )
by Mikael
01:27
created

RemServer::saveDataset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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