CachedManager   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 105
ccs 0
cts 50
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCacheKey() 0 7 2
A setSetting() 0 14 3
A getSetting() 0 16 2
A flush() 0 4 1
A get() 0 6 1
A set() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the PhpMob package.
5
 *
6
 * (c) Ishmael Doss <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace PhpMob\Settings\Manager;
15
16
use Psr\Cache\CacheItemPoolInterface;
17
18
/**
19
 * @author Ishmael Doss <[email protected]>
20
 */
21
class CachedManager implements SettingManagerInterface
22
{
23
    /**
24
     * @var CacheItemPoolInterface
25
     */
26
    private $cache;
27
28
    /**
29
     * @var int|\DateInterval|null
30
     */
31
    private $ttl = 0;
32
33
    /**
34
     * @var SettingManagerInterface
35
     */
36
    private $decoratedManager;
37
38
    public function __construct(SettingManagerInterface $manager, CacheItemPoolInterface $cache, $ttl = 0)
39
    {
40
        $this->decoratedManager = $manager;
41
        $this->cache = $cache;
42
        $this->ttl = $ttl;
43
    }
44
45
    /**
46
     * @param $section
47
     * @param $key
48
     * @param string|null $owner
49
     *
50
     * @return string
51
     */
52
    private function getCacheKey($section, $key, ?string $owner = null)
53
    {
54
        return 'phpmob-settings.' . ($owner
55
                ? sprintf('%s.%s.%s', $section, $key, $owner)
56
                : sprintf('%s.%s', $section, $key)
57
            );
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function setSetting(string $section, string $key, $value, ?string $owner = null, $autoFlush = false): void
64
    {
65
        $cacheKey = $this->getCacheKey($section, $key, $owner);
66
67
        if ($this->cache->hasItem($cacheKey) && $value === $this->cache->getItem($cacheKey)->get()) {
68
            return;
69
        }
70
71
        $this->decoratedManager->setSetting($section, $key, $value, $owner, $autoFlush);
72
73
        $this->cache->save(
74
            $this->cache->getItem($cacheKey)->set($value)->expiresAfter($this->ttl)
75
        );
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getSetting(string $section, string $key, ?string $owner = null)
82
    {
83
        $cacheKey = $this->getCacheKey($section, $key, $owner);
84
85
        if ($this->cache->hasItem($cacheKey)) {
86
            return $this->cache->getItem($cacheKey)->get();
87
        }
88
89
        $value = $this->decoratedManager->getSetting($section, $key, $owner);
90
91
        $this->cache->save(
92
            $this->cache->getItem($cacheKey)->set($value)->expiresAfter($this->ttl)
93
        );
94
95
        return $value;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function flush(): void
102
    {
103
        $this->decoratedManager->flush();
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function get(string $path, ?string $owner = null)
110
    {
111
        @list($section, $key) = explode('.', $path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
112
113
        return $this->getSetting($section, $key, $owner);
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function set(string $path, $value, ?string $owner = null): void
120
    {
121
        @list($section, $key) = explode('.', $path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
122
123
        $this->setSetting($section, $key, $value, $owner, true);
124
    }
125
}
126