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.

ImageStrategy::detach()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 5
cp 0
cc 3
eloc 4
nc 3
nop 1
crap 12
1
<?php
2
namespace HtImgModule\View\Strategy;
3
4
use HtImgModule\View\Model\ImageModel;
5
use Zend\EventManager\EventManagerInterface;
6
use Zend\EventManager\ListenerAggregateInterface;
7
use Zend\View\ViewEvent;
8
use Imagine\Image\ImagineInterface;
9
use Imagine\Image\ImageInterface;
10
use HtImgModule\Exception;
11
use HtImgModule\View\Renderer\ImageRenderer;
12
13
class ImageStrategy implements ListenerAggregateInterface
14
{
15
    /**
16
     * @var \Zend\Stdlib\CallbackHandler[]
17
     */
18
    protected $listeners = [];
19
20
    /**
21
     * @var ImagineInterface
22
     */
23
    protected $imagine;
24
25
    /**
26
     * Constructor
27
     *
28
     * @param ImagineInterface $imagine
29
     */
30 3
    public function __construct(ImagineInterface $imagine)
31
    {
32 3
        $this->imagine = $imagine;
33 3
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    public function attach(EventManagerInterface $events, $priority = 1)
39
    {
40
        $this->listeners[] = $events->attach(ViewEvent::EVENT_RENDERER, [$this, 'selectRenderer'], $priority);
41
        $this->listeners[] = $events->attach(ViewEvent::EVENT_RESPONSE, [$this, 'injectResponse'], $priority);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function detach(EventManagerInterface $events)
48
    {
49
        foreach ($this->listeners as $index => $listener) {
50
            if ($events->detach($listener)) {
51
                unset($this->listeners[$index]);
52
            }
53
        }
54
    }
55
56
    /**
57
     * Sets ImageRenderer as Renderer when ImageModel is used
58
     *
59
     * @param  ViewEvent                  $e
60
     * @return ImageRenderer|null
61
     * @throws Exception\RuntimeException
62
     */
63 2
    public function selectRenderer(ViewEvent $e)
64
    {
65 2
        $model = $e->getModel();
66 2
        if ($model instanceof ImageModel) {
67 1
            if (!$model->getImage() instanceof ImageInterface) {
68 1
                if (!$model->getImagePath()) {
69 1
                    throw new Exception\RuntimeException(
70 1
                        'You must provide Imagine\Image\ImageInterface or path of image'
71
                    );
72
                }
73
                $model->setImage($this->imagine->open($model->getImagePath()));
74
            }
75
76
            return new ImageRenderer();
77
        }
78 1
    }
79
80
    /**
81
     * Sets the response based on image returned by the renderer
82
     *
83
     * @param  ViewEvent $e
84
     * @return void
85
     */
86
    public function injectResponse(ViewEvent $e)
87
    {
88
        $model = $e->getModel();
89
        if ($model instanceof ImageModel) {
90
            $result   = $e->getResult();
91
92
            $response = $e->getResponse();
93
            $response->setContent($result);
94
95
            $response->getHeaders()->addHeaderLine('Content-type', $this->getMimeType($model->getFormat()));
96
        }
97
    }
98
99
    /**
100
     * Internal
101
     *
102
     * Get the mime type based on format.
103
     *
104
     * @param string $format
105
     *
106
     * @return string mime-type
107
     *
108
     * @throws RuntimeException
109
     */
110
    protected function getMimeType($format)
111
    {
112
        static $mimeTypes = array(
113
            'jpeg'  => 'image/jpeg',
114
            'jpg'   => 'image/jpeg',
115
            'pjpeg' => 'image/jpeg',
116
            'gif'   => 'image/gif',
117
            'png'   => 'image/png',
118
            'wbmp'  => 'image/vnd.wap.wbmp',
119
            'xbm'   => 'image/xbm',
120
        );
121
        $format = strtolower($format);
122
        return $mimeTypes[$format];
123
    }
124
}
125