Completed
Pull Request — master (#20)
by
unknown
01:16
created

Valuestore::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Spatie\Valuestore;
4
5
use Countable;
6
use ArrayAccess;
7
8
class Valuestore implements ArrayAccess, Countable
9
{
10
    /** @var string */
11
    protected $fileName;
12
13
    /**
14
     * @param string $fileName
15
     * @param array|null $values
16
     *
17
     * @return $this
18
     */
19
    public static function make(string $fileName, array $values = null)
20
    {
21
        $valuestore = (new static())->setFileName($fileName);
22
23
        if (! is_null($values)) {
24
            $valuestore->put($values);
25
        }
26
27
        return $valuestore;
28
    }
29
30
    protected function __construct()
31
    {
32
    }
33
34
    /**
35
     * Set the filename where all values will be stored.
36
     *
37
     * @param string $fileName
38
     *
39
     * @return $this
40
     */
41
    protected function setFileName(string $fileName)
42
    {
43
        $this->fileName = $fileName;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Put a value in the store.
50
     *
51
     * @param string|array    $name
52
     * @param string|int|null $value
53
     *
54
     * @return $this
55
     */
56
    public function put($name, $value = null)
57
    {
58
        if ($name == []) {
59
            return $this;
60
        }
61
62
        $newValues = $name;
63
64
        if (! is_array($name)) {
65
            $newValues = [$name => $value];
66
        }
67
68
        $newContent = array_merge($this->all(), $newValues);
69
70
        $this->setContent($newContent);
71
72
        return $this;
73
    }
74
75
    /**
76
     * set a value in the store.
77
     *
78
     * @param string|array    $name
79
     * @param string|int|null $value
80
     *
81
     * @return $this
82
     */
83
    public function set($name, $value = null)
0 ignored issues
show
Unused Code introduced by
The parameter $name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $value is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
    {
85
        return $this->put(...func_get_args());
86
    }
87
88
    /**
89
     * Push a new value into an array.
90
     *
91
     * @param string $name
92
     * @param $pushValue
93
     *
94
     * @return $this
95
     */
96 View Code Duplication
    public function push(string $name, $pushValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        if (! is_array($pushValue)) {
99
            $pushValue = [$pushValue];
100
        }
101
102
        if (! $this->has($name)) {
103
            $this->put($name, $pushValue);
0 ignored issues
show
Documentation introduced by
$pushValue is of type array, but the function expects a string|integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
105
            return $this;
106
        }
107
108
        $oldValue = $this->get($name);
109
110
        if (! is_array($oldValue)) {
111
            $oldValue = [$oldValue];
112
        }
113
114
        $newValue = array_merge($oldValue, $pushValue);
115
116
        $this->put($name, $newValue);
0 ignored issues
show
Documentation introduced by
$newValue is of type array, but the function expects a string|integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
118
        return $this;
119
    }
120
121
    /**
122
     * Prepend a new value in an array.
123
     *
124
     * @param string $name
125
     * @param $prependValue
126
     *
127
     * @return $this
128
     */
129 View Code Duplication
    public function prepend(string $name, $prependValue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        if (! is_array($prependValue)) {
132
            $prependValue = [$prependValue];
133
        }
134
135
        if (! $this->has($name)) {
136
            $this->put($name, $prependValue);
0 ignored issues
show
Documentation introduced by
$prependValue is of type array, but the function expects a string|integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
138
            return $this;
139
        }
140
141
        $oldValue = $this->get($name);
142
143
        if (! is_array($oldValue)) {
144
            $oldValue = [$oldValue];
145
        }
146
147
        $newValue = array_merge($prependValue, $oldValue);
148
149
        $this->put($name, $newValue);
0 ignored issues
show
Documentation introduced by
$newValue is of type array, but the function expects a string|integer|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
150
151
        return $this;
152
    }
153
154
    /**
155
     * Get a value from the store.
156
     *
157
     * @param string $name
158
     * @param $default
159
     *
160
     * @return null|string
161
     */
162
    public function get(string $name, $default = null)
163
    {
164
        $all = $this->all();
165
166
        if (! array_key_exists($name, $all)) {
167
            return $default;
168
        }
169
170
        return $all[$name];
171
    }
172
173
    /*
174
     * Determine if the store has a value for the given name.
175
     */
176
    public function has(string $name) : bool
177
    {
178
        return array_key_exists($name, $this->all());
179
    }
180
181
    /**
182
     * Get all values from the store.
183
     *
184
     * @return array
185
     */
186
    public function all() : array
187
    {
188
        if (! file_exists($this->fileName)) {
189
            return [];
190
        }
191
192
        return json_decode(file_get_contents($this->fileName), true);
193
    }
194
195
    /**
196
     * Get all keys starting with a given string from the store.
197
     *
198
     * @param string $startingWith
199
     *
200
     * @return array
201
     */
202
    public function allStartingWith(string $startingWith = '') : array
203
    {
204
        $values = $this->all();
205
206
        if ($startingWith === '') {
207
            return $values;
208
        }
209
210
        return $this->filterKeysStartingWith($values, $startingWith);
211
    }
212
213
    /**
214
     * Forget a value from the store.
215
     *
216
     * @param string $key
217
     *
218
     * @return $this
219
     */
220
    public function forget(string $key)
221
    {
222
        $newContent = $this->all();
223
224
        unset($newContent[$key]);
225
226
        $this->setContent($newContent);
227
228
        return $this;
229
    }
230
231
    /**
232
     * Flush all values from the store.
233
     *
234
     * @return $this
235
     */
236
    public function flush()
237
    {
238
        return $this->setContent([]);
239
    }
240
241
    /**
242
     * Flush all values which keys start with a given string.
243
     *
244
     * @param string $startingWith
245
     *
246
     * @return $this
247
     */
248
    public function flushStartingWith(string $startingWith = '')
249
    {
250
        $newContent = [];
251
252
        if ($startingWith !== '') {
253
            $newContent = $this->filterKeysNotStartingWith($this->all(), $startingWith);
254
        }
255
256
        return $this->setContent($newContent);
257
    }
258
259
    /**
260
     * Get and forget a value from the store.
261
     *
262
     * @param string $name
263
     *
264
     * @return null|string
265
     */
266
    public function pull(string $name)
267
    {
268
        $value = $this->get($name);
269
270
        $this->forget($name);
271
272
        return $value;
273
    }
274
275
    /**
276
     * Increment a value from the store.
277
     *
278
     * @param string $name
279
     * @param int    $by
280
     *
281
     * @return int|null|string
282
     */
283
    public function increment(string $name, int $by = 1)
284
    {
285
        $currentValue = $this->get($name) ?? 0;
286
287
        $newValue = $currentValue + $by;
288
289
        $this->put($name, $newValue);
290
291
        return $newValue;
292
    }
293
294
    /**
295
     * Decrement a value from the store.
296
     *
297
     * @param string $name
298
     * @param int    $by
299
     *
300
     * @return int|null|string
301
     */
302
    public function decrement(string $name, int $by = 1)
303
    {
304
        return $this->increment($name, $by * -1);
305
    }
306
307
    /**
308
     * Whether a offset exists.
309
     *
310
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
311
     *
312
     * @param mixed $offset
313
     *
314
     * @return bool
315
     */
316
    public function offsetExists($offset)
317
    {
318
        return $this->has($offset);
319
    }
320
321
    /**
322
     * Offset to retrieve.
323
     *
324
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
325
     *
326
     * @param mixed $offset
327
     *
328
     * @return mixed
329
     */
330
    public function offsetGet($offset)
331
    {
332
        return $this->get($offset);
333
    }
334
335
    /**
336
     * Offset to set.
337
     *
338
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
339
     *
340
     * @param mixed $offset
341
     * @param mixed $value
342
     */
343
    public function offsetSet($offset, $value)
344
    {
345
        $this->put($offset, $value);
346
    }
347
348
    /**
349
     * Offset to unset.
350
     *
351
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
352
     *
353
     * @param mixed $offset
354
     */
355
    public function offsetUnset($offset)
356
    {
357
        $this->forget($offset);
358
    }
359
360
    /**
361
     * Count elements.
362
     *
363
     * @link http://php.net/manual/en/countable.count.php
364
     *
365
     * @return int
366
     */
367
    public function count()
368
    {
369
        return count($this->all());
370
    }
371
372
    protected function filterKeysStartingWith(array $values, string $startsWith) : array
373
    {
374
        return array_filter($values, function ($key) use ($startsWith) {
375
            return $this->startsWith($key, $startsWith);
376
        }, ARRAY_FILTER_USE_KEY);
377
    }
378
379
    protected function filterKeysNotStartingWith(array $values, string $startsWith) : array
380
    {
381
        return array_filter($values, function ($key) use ($startsWith) {
382
            return ! $this->startsWith($key, $startsWith);
383
        }, ARRAY_FILTER_USE_KEY);
384
    }
385
386
    protected function startsWith(string $haystack, string $needle) : bool
387
    {
388
        return substr($haystack, 0, strlen($needle)) === $needle;
389
    }
390
391
    /**
392
     * @param array $values
393
     *
394
     * @return $this
395
     */
396
    protected function setContent(array $values)
397
    {
398
        file_put_contents($this->fileName, json_encode($values));
399
400
        if (! count($values)) {
401
            unlink($this->fileName);
402
        }
403
404
        return $this;
405
    }
406
}
407