Passed
Push — develop ( bcf814...d07230 )
by Schlaefer
02:57
created

PhileToPsr16CacheAdapter::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

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 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Adapter to use PSR-16 compatible cache class with Phile
4
 */
5
6
namespace Phile\Plugin\Phile\PhpFastCache;
7
8
use Psr\SimpleCache\CacheInterface;
9
10
/**
11
 * Class PhpFastCache
12
 *
13
 * @author  PhileCMS
14
 * @link    https://philecms.com
15
 * @license http://opensource.org/licenses/MIT
16
 * @package Phile\Plugin\Phile\PhpFastCache
17
 */
18
class PhileToPsr16CacheAdapter implements \Phile\ServiceLocator\CacheInterface
19
{
20
    /** @var string slug */
21
    const SLUG_PREFIX = '-phile.phpFastCache.slug-';
22
    
23
    const SLUG = ['{', '}' , '(', ')', '/' , '\\' , '@', ':'];
24
25
    /**
26
     * @var \BasePhpFastCache the cache engine
27
     */
28
    protected $cacheEngine;
29
30
    /**
31
     * the constructor
32
     *
33
     * @param CacheInterface $cacheEngine
34
     */
35 27
    public function __construct(CacheInterface $cacheEngine)
36
    {
37 27
        $this->cacheEngine = $cacheEngine;
0 ignored issues
show
Documentation Bug introduced by
It seems like $cacheEngine of type object<Psr\SimpleCache\CacheInterface> is incompatible with the declared type object<BasePhpFastCache> of property $cacheEngine.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38 27
    }
39
40
    /**
41
     * method to check if cache has entry for given key
42
     *
43
     * @param $key
44
     *
45
     * @return bool|mixed
46
     */
47 19
    public function has($key)
48
    {
49 19
        $key = $this->slug($key);
50 19
        return $this->cacheEngine->has($key);
51
    }
52
53
    /**
54
     * method to get cache entry
55
     *
56
     * @param string $key
57
     *
58
     * @return mixed|null
59
     */
60 2
    public function get($key)
61
    {
62 2
        $key = $this->slug($key);
63 2
        return $this->cacheEngine->get($key);
64
    }
65
66
    /**
67
     * method to set cache entry
68
     *
69
     * @param string $key
70
     * @param string $value
71
     * @param int    $time
72
     * @param array  $options deprecated
73
     *
74
     * @return mixed|void
75
     */
76 20 View Code Duplication
    public function set($key, $value, $time = 300, array $options = array())
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...
77
    {
78 20
        if (!empty($options)) {
79
            // not longer supported by phpFastCache
80
            trigger_error('Argument $options is deprecated and ignored.', E_USER_WARNING);
81
        }
82 20
        $key = $this->slug($key);
83 20
        $this->cacheEngine->set($key, $value, $time);
84 20
    }
85
86
    /**
87
     * method to delete cache entry
88
     *
89
     * @param string $key
90
     * @param array  $options deprecated
91
     *
92
     * @return mixed|void
93
     */
94 View Code Duplication
    public function delete($key, array $options = array())
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...
95
    {
96
        if (!empty($options)) {
97
            // not longer supported by phpFastCache
98
            trigger_error('Argument $options is deprecated and ignored.', E_USER_WARNING);
99
        }
100
        $key = $this->slug($key);
101
        $this->cacheEngine->delete($key);
102
    }
103
104
    /**
105
     * clean complete cache and delete all cached entries
106
     */
107
    public function clean()
108
    {
109
        $this->cacheEngine->clear();
110
    }
111
112
    /**
113
     * replaces chars forbidden in PSR-16 cache-keys
114
     *
115
     * @param string $key key to slug
116
     * @return string $key slugged key
117
     */
118 20
    protected function slug($key)
119
    {
120 20
        $replacementTokens = array_map(
121 20
            function ($key) {
122 20
                return self::SLUG_PREFIX . $key;
123 20
            },
124 20
            array_keys(self::SLUG)
125
        );
126 20
        return str_replace(self::SLUG, $replacementTokens, $key);
127
    }
128
}
129