Completed
Push — prototype ( 3bfdd7...aec713 )
by Peter
07:47
created

DispatchEvent::resetResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Webino (http://webino.sk)
4
 *
5
 * @link        https://github.com/webino for the canonical source repository
6
 * @copyright   Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk)
7
 * @author      Peter Bačinský <[email protected]>
8
 * @license     BSD-3-Clause
9
 */
10
11
namespace WebinoAppLib\Event;
12
13
use WebinoAppLib\Application\AbstractApplication;
14
use WebinoAppLib\Application\ConfiguredApplicationInterface;
15
use WebinoBaseLib\Util\ToString;
16
use Zend\Http\AbstractMessage;
17
use Zend\Stdlib\RequestInterface;
18
use Zend\Stdlib\ResponseInterface;
19
20
/**
21
 * Class DispatchEvent
22
 */
23
class DispatchEvent extends AppEvent implements
24
    DispatchEventInterface
25
{
26
    /**
27
     * @param ConfiguredApplicationInterface|AbstractApplication $app
28
     */
29
    public function __construct(ConfiguredApplicationInterface $app)
30
    {
31
        parent::__construct(AppEvent::DISPATCH);
32
        $this->setApp($app);
0 ignored issues
show
Documentation introduced by
$app is of type object<WebinoAppLib\Appl...edApplicationInterface>, but the function expects a object<WebinoAppLib\Appl...on\AbstractApplication>.

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...
33
    }
34
35
    /**
36
     * @return RequestInterface
37
     */
38
    public function getRequest()
39
    {
40
        return $this->getEventParam($this::REQUEST);
41
    }
42
43
    /**
44
     * @param RequestInterface $request
45
     * @return $this
46
     */
47
    public function setRequest(RequestInterface $request)
48
    {
49
        $this->setEventParam($this::REQUEST, $request);
50
        return $this;
51
    }
52
53
    /**
54
     * @return ResponseInterface|AbstractMessage
55
     */
56
    public function getResponse()
57
    {
58
        return $this->getEventParam($this::RESPONSE);
59
    }
60
61
    /**
62
     * Set response
63
     *
64
     * It sets response content when response is a string.
65
     * It joins values to a string when response is an array.
66
     * It converts to string when response is an object.
67
     * It sets response object when response is an interface.
68
     *
69
     * @param string|array|ResponseInterface $response
70
     * @return $this
71
     */
72
    public function setResponse($response)
73
    {
74
        if ($response instanceof ResponseInterface) {
75
            $this->setEventParam($this::RESPONSE, $response);
76
            return $this;
77
        }
78
79
        $_response = $this->getResponse();
80
        $_response->setContent($_response->getContent() . $this->normalizeResponseContent($response));
81
        return $this;
82
    }
83
84
    /**
85
     * Reset response
86
     *
87
     * @param string|array|ResponseInterface $response
88
     * @return $this
89
     */
90
    public function resetResponse($response = null)
91
    {
92
        if ($response instanceof ResponseInterface) {
93
            // TODO implement??
94
            return $this;
95
        }
96
        $this->getResponse()->setContent($this->normalizeResponseContent($response));
0 ignored issues
show
Bug introduced by
It seems like $response defined by parameter $response on line 90 can also be of type null; however, WebinoAppLib\Event\Dispa...malizeResponseContent() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97
        return $this;
98
    }
99
100
    /**
101
     * @param string|array $content
102
     * @return string
103
     */
104
    private function normalizeResponseContent($content)
105
    {
106
        return ToString::value($content);
107
    }
108
}
109