Passed
Branch 1.0 (690a53)
by Vladimir
07:08
created

FilesystemCache::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Cache;
6
7
use JsonSerializable;
8
use FondBot\Contracts\Cache;
9
use League\Flysystem\Filesystem;
10
11
class FilesystemCache implements Cache
12
{
13
    private $filesystem;
14
15 3
    public function __construct(Filesystem $filesystem)
16
    {
17 3
        $this->filesystem = $filesystem;
18 3
    }
19
20
    /**
21
     * Retrieve an item from the cache by key.
22
     *
23
     * @param string $key
24
     * @param mixed  $default
25
     *
26
     * @return mixed
27
     */
28 1
    public function get(string $key, $default = null)
29
    {
30 1
        return $this->filesystem->get($this->key($key)) ?? $default;
31
    }
32
33
    /**
34
     * Store an item in the cache.
35
     *
36
     * @param string $key
37
     * @param mixed  $value
38
     */
39 1
    public function store(string $key, $value): void
40
    {
41 1
        if (is_array($value)) {
42 1
            $value = json_encode($value);
43
        }
44 1
        if ($value instanceof JsonSerializable) {
0 ignored issues
show
Bug introduced by
The class JsonSerializable does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
45 1
            $value = json_encode($value->jsonSerialize());
46
        }
47
48 1
        $this->filesystem->put($this->key($key), $value);
49 1
    }
50
51
    /**
52
     * Remove an item from the cache.
53
     *
54
     * @param string $key
55
     */
56 1
    public function forget(string $key): void
57
    {
58 1
        $this->filesystem->delete($this->key($key));
59 1
    }
60
61 3
    private function key(string $key): string
62
    {
63 3
        return md5($key);
64
    }
65
}
66