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 ( 554d6d...4d8d0f )
by Bram
01:14
created

CacheService::save()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
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
            $cacheStorage->setTags($id, $this->getTags($mvcEvent));
111
        }
112
    }
113
114
    /**
115
     * Determine if we should cache the current request
116
     *
117
     * @param  MvcEvent $mvcEvent
118
     * @return bool
119
     */
120
    protected function shouldCacheRequest(MvcEvent $mvcEvent)
121
    {
122
        $event = $this->createCacheEvent(CacheEvent::EVENT_SHOULDCACHE, $mvcEvent);
123
124
        $results = $this->getEventManager()->triggerEventUntil(function ($result) {
125
            return $result;
126
        }, $event);
127
128
        if ($results->stopped()) {
129
            return $results->last();
130
        }
131
132
        return false;
133
    }
134
135
    /**
136
     * @param  array $tags
137
     * @param  bool|null $disjunction
138
     * @return bool
139
     * @throws UnsupportedAdapterException
140
     */
141
    public function clearByTags(array $tags = array(), $disjunction = null)
142
    {
143
        $cacheStorage = $this->getCacheStorage();
144
        if (!$cacheStorage instanceof TaggableInterface) {
145
            throw new UnsupportedAdapterException('purging by tags is only supported on adapters implementing the TaggableInterface');
146
        }
147
        $tags = array_map(
148
            function ($tag) { return CacheService::TAG_PREFIX . $tag; },
149
            $tags
150
        );
151
152
        return $cacheStorage->clearByTags($tags, $disjunction);
153
    }
154
155
    /**
156
     * Cache tags to use for this page
157
     *
158
     * @param  MvcEvent $event
159
     * @return array
160
     */
161
    public function getTags(MvcEvent $event)
162
    {
163
        $routeName = $event->getRouteMatch()->getMatchedRouteName();
164
        $tags = [
165
            self::TAG_PREFIX . 'route_' . $routeName
166
        ];
167
        foreach ($event->getRouteMatch()->getParams() as $key => $value) {
168
            if ($key == 'controller') {
169
                $tags[] = self::TAG_PREFIX . 'controller_' . $value;
170
            } else {
171
                $tags[] = self::TAG_PREFIX . 'param_' . $key . '_' . $value;
172
            }
173
        }
174
175
        return $tags;
176
    }
177
178
    /**
179
     * @param  string $eventName
180
     * @param  string $cacheKey
181
     * @param  MvcEvent|null $mvcEvent
182
     * @return CacheEvent
183
     */
184
    protected function createCacheEvent($eventName, MvcEvent $mvcEvent = null, $cacheKey = null)
185
    {
186
        $cacheEvent = new CacheEvent($eventName, $this);
187
        $cacheEvent->setCacheKey($cacheKey);
188
        if ($mvcEvent !== null) {
189
            $cacheEvent->setMvcEvent($mvcEvent);
190
        }
191
192
        return $cacheEvent;
193
    }
194
195
    /**
196
     * @return StorageInterface
197
     */
198
    public function getCacheStorage()
199
    {
200
        return $this->cacheStorage;
201
    }
202
203
    /**
204
     * @param  StorageInterface $cacheStorage
205
     * @return self
206
     */
207
    public function setCacheStorage($cacheStorage)
208
    {
209
        $this->cacheStorage = $cacheStorage;
210
211
        return $this;
212
    }
213
214
    /**
215
     * @return ModuleOptions
216
     */
217
    public function getOptions()
218
    {
219
        return $this->options;
220
    }
221
222
    /**
223
     * @param  ModuleOptions $options
224
     * @return self
225
     */
226
    public function setOptions($options)
227
    {
228
        $this->options = $options;
229
230
        return $this;
231
    }
232
233
    /**
234
     * Inject an EventManager instance
235
     *
236
     * @param  EventManagerInterface $eventManager
237
     * @return self
238
     */
239
    public function setEventManager(EventManagerInterface $eventManager)
240
    {
241
        $eventManager->setIdentifiers([__CLASS__, get_called_class()]);
242
243
        $this->eventManager = $eventManager;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Retrieve the event manager
250
     *
251
     * Lazy-loads an EventManager instance if none registered.
252
     *
253
     * @return EventManagerInterface
254
     */
255
    public function getEventManager()
256
    {
257
        if (!$this->eventManager instanceof EventManagerInterface) {
258
            $this->setEventManager(new EventManager());
259
        }
260
261
        return $this->eventManager;
262
    }
263
264
    /**
265
     * @return IdGeneratorInterface
266
     */
267
    public function getIdGenerator()
268
    {
269
        return $this->idGenerator;
270
    }
271
272
    /**
273
     * @param IdGeneratorInterface $idGenerator
274
     */
275
    public function setIdGenerator($idGenerator = null)
276
    {
277
        $this->idGenerator = $idGenerator;
278
    }
279
}
280