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 ( 2f9008...830571 )
by Bram
01:55
created

CacheListener::addDebugResponseHeader()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
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\Listener;
9
10
use StrokerCache\Options\ModuleOptions;
11
use StrokerCache\Service\CacheService;
12
use Zend\EventManager\AbstractListenerAggregate;
13
use Zend\EventManager\EventManagerInterface;
14
use Zend\Http\PhpEnvironment\Request as HttpRequest;
15
use Zend\Http\Response as HttpResponse;
16
use Zend\Mvc\MvcEvent;
17
18
class CacheListener extends AbstractListenerAggregate
19
{
20
    /**
21
     * @var array
22
     */
23
    protected $listeners = array();
24
25
    /**
26
     * @var CacheService
27
     */
28
    protected $cacheService;
29
30
    /**
31
     * @var ModuleOptions
32
     */
33
    protected $options;
34
35
    /**
36
     * @var bool
37
     */
38
    protected $loadedFromCache = false;
39
40
    /**
41
     * Default constructor
42
     *
43
     * @param CacheService $cacheService
44
     * @param ModuleOptions $options
45 8
     */
46
    public function __construct(CacheService $cacheService, ModuleOptions $options)
47 8
    {
48 8
        $this->cacheService = $cacheService;
49 8
        $this->options      = $options;
50
    }
51
52
    /**
53
     * {@inheritDoc}
54 1
     */
55
    public function attach(EventManagerInterface $events, $priority = 1)
56 1
    {
57 1
        $this->listeners[] = $events->attach('route', [$this, 'onRoute'], 100);
58 1
        $this->listeners[] = $events->attach('finish', [$this, 'onFinish'], -100);
59
    }
60
61
    /**
62
     * Load the page contents from the cache and set the response.
63
     *
64
     * @param  MvcEvent $e
65
     * @return HttpResponse
66 6
     */
67
    public function onRoute(MvcEvent $e)
68 6
    {
69 1
        if (!$e->getRequest() instanceof HttpRequest || !$e->getResponse() instanceof HttpResponse) {
70
            return null;
71
        }
72 5
73
        /** @var HttpResponse $response */
74 5
        $response = $e->getResponse();
75 3
76
        $data = $this->getCacheService()->load($e);
77 3
78 2
        if ($data !== null) {
79 2
            $this->loadedFromCache = true;
80 1
81 1
            if ($this->getOptions()->getCacheResponse() === true) {
82
                $response = unserialize($data);
83
            } else {
84 3
                $response = $e->getResponse();
85
                $response->setContent($data);
86 3
            }
87
88 2
            $this->addDebugResponseHeader($response, 'Hit');
89 2
90
            return $response;
91
        }
92
93
        $this->addDebugResponseHeader($response, 'Miss');
94
    }
95
96
    /**
97 4
     * @param HttpResponse $response
98
     * @param string $value
99 4
     */
100 3
    private function addDebugResponseHeader(HttpResponse $response, $value)
101
    {
102
        if ($this->getOptions()->isAddDebugHeaders()) {
103 1
            $response->getHeaders()->addHeaderLine('X-Stroker-Cache', $value);
104 1
        }
105
    }
106
107
    /**
108
     * Save page contents to the cache
109 5
     *
110
     * @param  MvcEvent $e
111 5
     * @return void
112
     */
113
    public function onFinish(MvcEvent $e)
114
    {
115
        if (!$e->getRequest() instanceof HttpRequest || $this->loadedFromCache) {
116
            return;
117 3
        }
118
119 3
        $this->getCacheService()->save($e);
120
    }
121
122
    /**
123
     * @return CacheService
124
     */
125
    public function getCacheService()
126
    {
127
        return $this->cacheService;
128
    }
129
130
    /**
131
     * @return ModuleOptions
132
     */
133
    public function getOptions()
134
    {
135
        return $this->options;
136
    }
137
}
138