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
Pull Request — master (#54)
by Vytautas
01:24
created

CacheService::save()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 14
nc 5
nop 1
1
<?php
2
/**
3
 * @author Bram Gerritsen [email protected]
4
 * @copyright (c) Bram Gerritsen 2013
5
 * @license http://opensource.org/licenses/mit-license.php
6
 */
7
8
namespace StrokerCache\Service;
9
10
use StrokerCache\Exception\UnsupportedAdapterException;
11
use StrokerCache\IdGenerator\IdGeneratorInterface;
12
use Zend\Mvc\MvcEvent;
13
use StrokerCache\Event\CacheEvent;
14
use Zend\EventManager\EventManager;
15
use Zend\EventManager\EventManagerInterface;
16
use StrokerCache\Options\ModuleOptions;
17
use Zend\Cache\Storage\TaggableInterface;
18
use Zend\Cache\Storage\StorageInterface;
19
20
class CacheService
21
{
22
    /**
23
     * Prefix to use for the tag key
24
     * @var string
25
     */
26
    const TAG_PREFIX = 'strokercache_';
27
28
    /**
29
     * @var EventManagerInterface
30
     */
31
    protected $eventManager;
32
33
    /**
34
     * @var StorageInterface
35
     */
36
    protected $cacheStorage;
37
38
    /**
39
     * @var IdGeneratorInterface
40
     */
41
    protected $idGenerator;
42
43
    /**
44
     * @var ModuleOptions
45
     */
46
    protected $options;
47
48
    /**
49
     * Default constructor
50
     *
51
     * @param StorageInterface $cacheStorage
52
     * @param ModuleOptions $options
53
     * @param IdGeneratorInterface $idGenerator
54
     */
55
    public function __construct(StorageInterface $cacheStorage, ModuleOptions $options, IdGeneratorInterface $idGenerator = null)
56
    {
57
        $this->setCacheStorage($cacheStorage);
58
        $this->setOptions($options);
59
        $this->setIdGenerator($idGenerator);
60
    }
61
62
    /**
63
     * Check if a page is saved in the cache and return contents. Return null when no item is found.
64
     *
65
     * @param MvcEvent $mvcEvent
66
     * @return mixed|null
67
     */
68
    public function load(MvcEvent $mvcEvent)
69
    {
70
        $id = $this->getIdGenerator()->generate();
71
        if (!$this->getCacheStorage()->hasItem($id)) {
72
            return null;
73
        };
74
75
        $event = $this->createCacheEvent(CacheEvent::EVENT_LOAD, $mvcEvent, $id);
76
77
        $results = $this->getEventManager()->triggerEventUntil(function ($result) {
78
            return ($result === false);
79
        }, $event);
80
81
        if ($results->stopped()) {
82
            return null;
83
        }
84
85
        return $this->getCacheStorage()->getItem($id);
86
    }
87
88
    /**
89
     * Save the page contents to the cache storage.
90
     *
91
     * @param MvcEvent $mvcEvent
92
     */
93
    public function save(MvcEvent $mvcEvent)
94
    {
95
        if (!$this->shouldCacheRequest($mvcEvent)) {
96
            return;
97
        }
98
99
        $id = $this->getIdGenerator()->generate();
100
101
        $item = ($this->getOptions()->getCacheResponse() === true) ? serialize($mvcEvent->getResponse()) : $mvcEvent->getResponse()->getContent();
102
103
        $this->getCacheStorage()->setItem($id, $item);
104
105
        $cacheEvent = $this->createCacheEvent(CacheEvent::EVENT_SAVE, $mvcEvent, $id);
106
        $this->getEventManager()->triggerEvent($cacheEvent);
107
108
        $cacheStorage = $this->getCacheStorage();
109
        if ($cacheStorage instanceof TaggableInterface) {
110
            $tags = array_map(
111
                function ($tag) { return CacheService::TAG_PREFIX . $tag; },
112
                array_unique(array_merge($this->getTags($mvcEvent), $cacheEvent->getTags()))
113
            );
114
            $cacheStorage->setTags($id, $tags);
115
        }
116
    }
117
118
    /**
119
     * Determine if we should cache the current request
120
     *
121
     * @param  MvcEvent $mvcEvent
122
     * @return bool
123
     */
124
    protected function shouldCacheRequest(MvcEvent $mvcEvent)
125
    {
126
        $event = $this->createCacheEvent(CacheEvent::EVENT_SHOULDCACHE, $mvcEvent);
127
128
        $results = $this->getEventManager()->triggerEventUntil(function ($result) {
129
            return $result;
130
        }, $event);
131
132
        if ($results->stopped()) {
133
            return $results->last();
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * @param  array $tags
141
     * @param  bool|null $disjunction
142
     * @return bool
143
     * @throws UnsupportedAdapterException
144
     */
145
    public function clearByTags(array $tags = array(), $disjunction = null)
146
    {
147
        $cacheStorage = $this->getCacheStorage();
148
        if (!$cacheStorage instanceof TaggableInterface) {
149
            throw new UnsupportedAdapterException('purging by tags is only supported on adapters implementing the TaggableInterface');
150
        }
151
        $tags = array_map(
152
            function ($tag) { return CacheService::TAG_PREFIX . $tag; },
153
            $tags
154
        );
155
156
        return $cacheStorage->clearByTags($tags, $disjunction);
157
    }
158
159
    /**
160
     * Cache tags to use for this page
161
     *
162
     * @param  MvcEvent $event
163
     * @return array
164
     */
165
    public function getTags(MvcEvent $event)
166
    {
167
        $routeName = $event->getRouteMatch()->getMatchedRouteName();
168
        $tags = [
169
            'route_' . $routeName
170
        ];
171
        foreach ($event->getRouteMatch()->getParams() as $key => $value) {
172
            if ($key == 'controller') {
173
                $tags[] = 'controller_' . $value;
174
            } else {
175
                $tags[] = 'param_' . $key . '_' . $value;
176
            }
177
        }
178
179
        return $tags;
180
    }
181
182
    /**
183
     * @param  string $eventName
184
     * @param  string $cacheKey
185
     * @param  MvcEvent|null $mvcEvent
186
     * @return CacheEvent
187
     */
188
    protected function createCacheEvent($eventName, MvcEvent $mvcEvent = null, $cacheKey = null)
189
    {
190
        $cacheEvent = new CacheEvent($eventName, $this);
191
        $cacheEvent->setCacheKey($cacheKey);
192
        if ($mvcEvent !== null) {
193
            $cacheEvent->setMvcEvent($mvcEvent);
194
        }
195
196
        return $cacheEvent;
197
    }
198
199
    /**
200
     * @return StorageInterface
201
     */
202
    public function getCacheStorage()
203
    {
204
        return $this->cacheStorage;
205
    }
206
207
    /**
208
     * @param  StorageInterface $cacheStorage
209
     * @return self
210
     */
211
    public function setCacheStorage($cacheStorage)
212
    {
213
        $this->cacheStorage = $cacheStorage;
214
215
        return $this;
216
    }
217
218
    /**
219
     * @return ModuleOptions
220
     */
221
    public function getOptions()
222
    {
223
        return $this->options;
224
    }
225
226
    /**
227
     * @param  ModuleOptions $options
228
     * @return self
229
     */
230
    public function setOptions($options)
231
    {
232
        $this->options = $options;
233
234
        return $this;
235
    }
236
237
    /**
238
     * Inject an EventManager instance
239
     *
240
     * @param  EventManagerInterface $eventManager
241
     * @return self
242
     */
243
    public function setEventManager(EventManagerInterface $eventManager)
244
    {
245
        $eventManager->setIdentifiers([__CLASS__, get_called_class()]);
246
247
        $this->eventManager = $eventManager;
248
249
        return $this;
250
    }
251
252
    /**
253
     * Retrieve the event manager
254
     *
255
     * Lazy-loads an EventManager instance if none registered.
256
     *
257
     * @return EventManagerInterface
258
     */
259
    public function getEventManager()
260
    {
261
        if (!$this->eventManager instanceof EventManagerInterface) {
262
            $this->setEventManager(new EventManager());
263
        }
264
265
        return $this->eventManager;
266
    }
267
268
    /**
269
     * @return IdGeneratorInterface
270
     */
271
    public function getIdGenerator()
272
    {
273
        return $this->idGenerator;
274
    }
275
276
    /**
277
     * @param IdGeneratorInterface $idGenerator
278
     */
279
    public function setIdGenerator($idGenerator = null)
280
    {
281
        $this->idGenerator = $idGenerator;
282
    }
283
}
284