Cache   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 28
c 1
b 0
f 0
dl 0
loc 63
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete() 0 3 1
A clear() 0 3 1
A __construct() 0 21 4
A get() 0 8 2
A set() 0 11 2
1
<?php
2
/**
3
 * This file is the main class of the keinos/mastodon-streaming-api-cache package.
4
 *
5
 * - Reference of the public methods of this class to use:
6
 *   - See: ./CacheInterface.php
7
 * - Authors, copyright, license, usage and etc.:
8
 *   - See: https://github.com/KEINOS/Mastodon_StreamingAPI_Cache/
9
 *
10
 * Debug:
11
 * - To test and analyze the scripts:
12
 *   - Run: `$ composer test all verbose` (Composer is required)
13
 */
14
15
declare(strict_types=1);
16
17
namespace KEINOS\MSTDN_TOOLS\Cache;
18
19
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Cache\Adapter\FilesystemAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
22
 * Simple Caching Class.
23
 */
24
class Cache implements CacheInterface
25
{
26
    /** @var FilesystemAdapter */
27
    protected $cache_pool;
28
    /** @var string */
29
    protected $namespace;
30
    /** @var int */
31
    protected $ttl;
32
33
    public function __construct(array $settings=[])
34
    {
35
        $ttl = self::CACHE_TTL_DEFAULT;
36
        if (isset($settings['ttl'])) {
37
            $ttl = intval($settings['ttl']);
38
        }
39
        $this->ttl = $ttl;
40
41
        $prefix = self::CACHE_PREFIX_DEFAULT;
42
        if (isset($settings['prefix'])) {
43
            $prefix = $settings['prefix'];
44
        }
45
46
        $id_hash = strval(hash_file('md5', __FILE__));
47
        if (isset($settings['namespace'])) {
48
            $id_hash = $settings['namespace'];
49
        }
50
        $this->namespace = $prefix . $id_hash;
51
52
        // Set file system adapter object
53
        $this->cache_pool = new FilesystemAdapter($this->namespace, $this->ttl);
54
    }
55
56
    public function clear(): bool
57
    {
58
        return $this->cache_pool->clear();
59
    }
60
61
    public function delete(string $key): bool
62
    {
63
        return $this->cache_pool->deleteItem($key);
64
    }
65
66
    public function get(string $key)
67
    {
68
        $cache_item = $this->cache_pool->getItem($key);
69
70
        if (! $cache_item->isHit()) {
71
            return null;
72
        }
73
        return $cache_item->get();
74
    }
75
76
    public function set(string $key, $value): void
77
    {
78
        if (null === $value) {
79
            $msg = 'Null value given. "null" value is NOT allowed to cache.';
80
            throw new \Exception($msg);
81
        }
82
83
        $cache_item = $this->cache_pool->getItem($key);
84
85
        $cache_item->set($value);
86
        $this->cache_pool->save($cache_item);
87
    }
88
}
89