DrawStrategy   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B injectResponse() 0 48 6
A resolveIsXml() 0 9 2
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino/WebinoDraw for the canonical source repository
6
 * @copyright   Copyright (c) 2012-2017 Webino, s. r. o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoDraw\View\Strategy;
12
13
use WebinoDraw\Exception;
14
use Zend\Http\PhpEnvironment\Response;
15
use Zend\View\ViewEvent;
16
17
/**
18
 * Draw XHTML with this view strategy
19
 */
20
class DrawStrategy extends AbstractDrawStrategy
21
{
22
    /**
23
     * Application service name
24
     */
25
    const SERVICE = 'WebinoDrawStrategy';
26
27
    /**
28
     * @param ViewEvent $event
29
     * @throws \Exception
30
     */
31
    public function injectResponse(ViewEvent $event)
32
    {
33
        if (!$this->shouldRespond($event)) {
34
            return;
35
        }
36
37
        /* @var $response Response */
38
        $options  = $this->draw->getOptions();
39
        $response = $event->getResponse();
40
        $body     = $response->getBody();
41
        $spec     = $options->getInstructions();
42
        $model    = $event->getModel();
43
        $isXml    = $this->resolveIsXml($response);
0 ignored issues
show
Documentation introduced by
$response is of type null|object<Zend\Stdlib\ResponseInterface>, but the function expects a object<Zend\Http\PhpEnvironment\Response>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
45
        try {
46
            $response->setContent(
47
                $this->draw->draw(
48
                    $body,
49
                    $spec,
50
                    $this->collectModelVariables($model),
0 ignored issues
show
Documentation introduced by
$model is of type null|object<Zend\View\Model\ModelInterface>, but the function expects a object<Zend\View\Model\ViewModel>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
                    $isXml
52
                )
53
            );
54
55
        } catch (Exception\DrawException $exc) {
56
            $_exc = $exc;
57
            while ($subExc = $_exc->getPrevious()) {
58
                $_exc = $subExc;
59
60
                if ($subExc instanceof Exception\ExceptionalDrawExceptionInterface) {
61
                    $model->setVariables($subExc->getDrawVariables());
62
                    $response->setContent(
63
                        $this->draw->draw(
64
                            $body,
65
                            $spec,
66
                            $this->collectModelVariables($model),
0 ignored issues
show
Documentation introduced by
$model is of type null|object<Zend\View\Model\ModelInterface>, but the function expects a object<Zend\View\Model\ViewModel>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
67
                            $isXml
68
                        )
69
                    );
70
                    return;
71
                }
72
            }
73
            throw $exc;
74
75
        } catch (\Exception $exc) {
76
            throw $exc;
77
        }
78
    }
79
80
    /**
81
     * @param Response $response
82
     * @return bool
83
     */
84
    private function resolveIsXml(Response $response)
85
    {
86
        $contentType = $response->getHeaders()->get('content-type');
87
        if (empty($contentType)) {
88
            return false;
89
        }
90
91
        return ('text/xml' === $contentType->getMediaType());
92
    }
93
}
94