Completed
Pull Request — final (#507)
by
unknown
03:32
created

Psr16Adapter::has()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Helper;
16
17
use phpFastCache\CacheManager;
18
use phpFastCache\Core\Item\ExtendedCacheItemInterface;
19
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
20
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
21
use phpFastCache\Exceptions\phpFastCacheInvalidArgumentException;
22
use phpFastCache\Exceptions\phpFastCacheRootException;
23
use phpFastCache\Exceptions\phpFastCacheSimpleCacheException;
24
use Psr\SimpleCache\CacheInterface;
25
26
/**
27
 * Class Psr16Adapter
28
 * @package phpFastCache\Helper
29
 */
30
class Psr16Adapter implements CacheInterface
31
{
32
    /**
33
     * @var ExtendedCacheItemPoolInterface
34
     */
35
    protected $internalCacheInstance;
36
37
    /**
38
     * Psr16Adapter constructor.
39
     * @param string $driver
40
     * @param array $config
41
     * @throws phpFastCacheDriverCheckException
42
     */
43
    public function __construct($driver, array $config = [])
44
    {
45
        $this->internalCacheInstance = CacheManager::getInstance($driver, $config);
46
    }
47
48
    /**
49
     * @param string $key
50
     * @param null $default
51
     * @return mixed|null
52
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
53
     */
54
    public function get($key, $default = null)
55
    {
56
        try {
57
            $cacheItemValue = $this->internalCacheInstance->getItem($key)->get();
58
            if ($cacheItemValue !== null) {
59
                return $cacheItemValue;
60
            } else {
61
                return $default;
62
            }
63
        } catch (phpFastCacheInvalidArgumentException $e) {
64
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
65
        }
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param mixed $value
71
     * @param null $ttl
72
     * @return bool
73
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
74
     */
75
    public function set($key, $value, $ttl = null)
76
    {
77
        try {
78
            $cacheItem = $this->internalCacheInstance
79
              ->getItem($key)
80
              ->set($value);
81 View Code Duplication
            if (is_int($ttl) && $ttl <= 0) {
82
                $cacheItem->expiresAt((new \DateTime('@0')));
83
            } elseif (is_int($ttl) || $ttl instanceof \DateInterval) {
84
                $cacheItem->expiresAfter($ttl);
85
            }
86
            return $this->internalCacheInstance->save($cacheItem);
87
        } catch (phpFastCacheInvalidArgumentException $e) {
88
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
89
        }
90
    }
91
92
    /**
93
     * @param string $key
94
     * @return bool
95
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
96
     */
97 View Code Duplication
    public function delete($key)
98
    {
99
        try {
100
            return $this->internalCacheInstance->deleteItem($key);
101
        } catch (phpFastCacheInvalidArgumentException $e) {
102
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
103
        }
104
    }
105
106
    /**
107
     * @return bool
108
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
109
     */
110
    public function clear()
111
    {
112
        try {
113
            return $this->internalCacheInstance->clear();
114
        } catch (phpFastCacheRootException $e) {
115
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
116
        }
117
    }
118
119
    /**
120
     * @param string[] $keys
121
     * @param null $default
122
     * @return \iterable
123
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
124
     */
125
    public function getMultiple($keys, $default = null)
126
    {
127
        try {
128
            return array_map(function (ExtendedCacheItemInterface $item) {
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array_map(functio...ance->getItems($keys)); (array) is incompatible with the return type declared by the interface Psr\SimpleCache\CacheInterface::getMultiple of type Psr\SimpleCache\iterable.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
129
                return $item->get();
130
            }, $this->internalCacheInstance->getItems($keys));
131
        } catch (phpFastCacheInvalidArgumentException $e) {
132
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
133
        }
134
    }
135
136
    /**
137
     * @param string[] $values
138
     * @param null|int|\DateInterval $ttl
139
     * @return bool
140
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
141
     */
142
    public function setMultiple($values, $ttl = null)
143
    {
144
        try {
145
            foreach ($values as $key => $value) {
146
                $cacheItem = $this->internalCacheInstance->getItem($key)->set($value);
147
148 View Code Duplication
                if (is_int($ttl) && $ttl <= 0) {
149
                    $cacheItem->expiresAt((new \DateTime('@0')));
150
                } elseif (is_int($ttl) || $ttl instanceof \DateInterval) {
151
                    $cacheItem->expiresAfter($ttl);
152
                }
153
                $this->internalCacheInstance->saveDeferred($cacheItem);
154
                unset($cacheItem);
155
            }
156
            return $this->internalCacheInstance->commit();
157
        } catch (phpFastCacheInvalidArgumentException $e) {
158
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
159
        }
160
    }
161
162
    /**
163
     * @param string[] $keys
164
     * @return bool
165
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
166
     */
167 View Code Duplication
    public function deleteMultiple($keys)
168
    {
169
        try {
170
            return $this->internalCacheInstance->deleteItems($keys);
171
        } catch (phpFastCacheInvalidArgumentException $e) {
172
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
173
        }
174
    }
175
176
    /**
177
     * @param string $key
178
     * @return bool
179
     * @throws \phpFastCache\Exceptions\phpFastCacheSimpleCacheException
180
     */
181
    public function has($key)
182
    {
183
        try {
184
            return $this->internalCacheInstance->getItem($key)->isHit();
185
        } catch (phpFastCacheInvalidArgumentException $e) {
186
            throw new phpFastCacheSimpleCacheException($e->getMessage(), null, $e);
187
        }
188
    }
189
}
190