Completed
Push — master ( c631b6...ab9e5a )
by Mikael
01:55
created

RemServer::injectSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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