GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( d32462...af31de )
by Hilari
05:14 queued 02:44
created

LoggerCache::call()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
ccs 10
cts 10
cp 1
cc 4
eloc 11
nc 5
nop 3
crap 4
1
<?php
2
3
namespace Cmp\Cache\Decorator;
4
5
use Cmp\Cache\Cache;
6
use Cmp\Cache\Exceptions\BackendOperationFailedException;
7
use Cmp\Cache\Exceptions\CacheException;
8
use Cmp\Cache\Traits\LoggerCacheTrait;
9
use Exception;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\LogLevel;
12
13
/**
14
 * Class LoggerCache
15
 *
16
 * @package Cmp\Cache\Decorator
17
 */
18
class LoggerCache implements Cache, CacheDecorator
19
{
20
    use CacheDecoratorTrait, LoggerCacheTrait;
21
22
    /**
23
     * @var bool
24
     */
25
    private $withExceptions;
26
27
    /**
28
     * @var string
29
     */
30
    private $logLevel;
31
32
    /**
33
     * SilentCacheDecorator constructor.
34
     *
35
     * @param Cache           $cache
36
     * @param bool            $withExceptions
37
     * @param LoggerInterface $logger
38
     * @param string          $logLevel
39
     */
40 1
    public function __construct(
41
        Cache $cache,
42
        $withExceptions = true,
43
        LoggerInterface $logger = null,
44
        $logLevel = LogLevel::ALERT
45
    ) {
46 1
        $this->cache          = $cache;
47 1
        $this->withExceptions = $withExceptions;
48 1
        $this->logger         = $logger;
49 1
        $this->logLevel       = $logLevel;
50 1
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 1
    public function set($key, $value, $timeToLive = null)
56
    {
57 1
        return $this->call(
58
            function() use ($key, $value, $timeToLive) {
59 1
                return $this->cache->set($key, $value, $timeToLive);
60 1
            },
61 1
            __METHOD__,
62 1
            [$key, $value, $timeToLive]
63
        );
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 1
    public function has($key)
70
    {
71 1
        return $this->call(
72
            function() use ($key) {
73 1
                return $this->cache->has($key);
74 1
            },
75 1
            __METHOD__,
76 1
            [$key]
77
        );
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 1
    public function get($key, $default = null)
84
    {
85 1
        return $this->call(
86
            function() use ($key, $default) {
87 1
                return $this->cache->get($key, $default);
88 1
            },
89 1
            __METHOD__,
90 1
            [$key, $default]
91
        );
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 1
    public function demand($key)
98
    {
99 1
        return $this->call(
100
            function() use ($key) {
101 1
                return $this->cache->demand($key);
102 1
            },
103 1
            __METHOD__,
104 1
            [$key]
105
        );
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 1
    public function delete($key)
112
    {
113 1
        return $this->call(
114
            function() use ($key) {
115 1
                return $this->cache->delete($key);
116 1
            },
117 1
            __METHOD__,
118 1
            [$key]
119
        );
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 1
    public function flush()
126
    {
127 1
        return $this->call(
128
            function() {
129 1
                return $this->cache->flush();
130 1
            },
131 1
            __METHOD__
132
        );
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 1
    public function setItems(array $items, $timeToLive = null)
139
    {
140 1
        return $this->call(
141
            function() use ($items, $timeToLive) {
142 1
                return $this->cache->setItems($items, $timeToLive);
143 1
            },
144 1
            __METHOD__,
145 1
            [$items, $timeToLive]
146
        );
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 1
    public function getItems(array $keys)
153
    {
154 1
        return $this->call(
155
            function() use ($keys) {
156 1
                return $this->cache->getItems($keys);
157 1
            },
158 1
            __METHOD__,
159 1
            [$keys]
160
        );
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166 1
    public function deleteItems(array $keys)
167
    {
168 1
        return $this->call(
169
            function() use ($keys) {
170 1
                return $this->cache->deleteItems($keys);
171 1
            },
172 1
            __METHOD__,
173 1
            [$keys]
174
        );
175
    }
176
177
    /**
178
     * Gets the remaining time to live for an item
179
     *
180
     * @param $key
181
     *
182
     * @return int|null
183
     */
184 1
    public function getTimeToLive($key)
185
    {
186 1
        return $this->call(
187 1
            function() use($key) {
188 1
                return $this->cache->getTimeToLive($key);
189 1
            },
190 1
            __METHOD__
191
        );
192
    }
193
194
    /**
195
     * @param callable $callable
196
     *
197
     * @param string   $method
198
     * @param array    $arguments
199
     *
200
     * @return mixed
201
     * @throws BackendOperationFailedException
202
     * @throws CacheException
203
     */
204 1
    protected function call(callable $callable, $method, $arguments = [])
205
    {
206
        try {
207 1
            return $callable();
208 1
        } catch (CacheException $exception) {
209 1
            $this->logException($exception, $method, $arguments);
210 1
        } catch (Exception $exception) {
211 1
            $this->logException($exception, $method, $arguments);
212 1
            $exception = new BackendOperationFailedException($this->getDecoratedCache(), $method, $exception);
213
        }
214
215 1
        if ($this->withExceptions) {
216 1
            throw $exception;
217
        }
218
219 1
        return false;
220
    }
221
222
    /**
223
     * @param Exception $exception
224
     * @param string    $method
225
     * @param array     $arguments
226
     *
227
     * @return bool
228
     */
229 1
    protected function logException(Exception $exception, $method, array $arguments)
230
    {
231 1
        $this->log($this->logLevel, "Cache $method on cache operation failed: ".$exception->getMessage(), [
232 1
            'cache'     => get_class($this->cache),
233 1
            'decorated' => get_class($this->getDecoratedCache()),
234 1
            'exception' => $exception,
235 1
            'method'    => $method,
236 1
            'arguments' => $arguments,
237
        ]);
238 1
    }
239
}
240