Passed
Push — master ( 78234a...a33c53 )
by Michael
04:14
created

ExceptionEvent::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\HttpClient\Decorator\SymfonyEvent;
5
6
use Psr\Http\Message\RequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Symfony\Component\EventDispatcher\Event;
9
10
/**
11
 * On exception event
12
 *
13
 * @package Mikemirten\Component\JsonApi\HttpClient\Decorator\SymfonyEvent
14
 */
15
class ExceptionEvent extends Event
16
{
17
    /**
18
     * @var RequestInterface
19
     */
20
    protected $request;
21
22
    /**
23
     * @var ResponseInterface
24
     */
25
    protected $response;
26
27
    /**
28
     * @var \Throwable
29
     */
30
    protected $exception;
31
32
    /**
33
     * If the exception will be resolved to a response (through setResponse()),
34
     * response-event will be fired until explicitly disabled by this property.
35
     *
36
     * @var bool
37
     */
38
    protected $responseEventEnabled = true;
39
40
    /**
41
     * ExceptionEvent constructor.
42
     *
43
     * @param RequestInterface $request
44
     * @param \Throwable       $exception
45
     */
46 5
    public function __construct(RequestInterface $request, \Throwable $exception)
47
    {
48 5
        $this->request   = $request;
49 5
        $this->exception = $exception;
50 5
    }
51
52
    /**
53
     * Get request
54
     *
55
     * @return RequestInterface
56
     */
57 2
    public function getRequest(): RequestInterface
58
    {
59 2
        return $this->request;
60
    }
61
62
    /**
63
     * Get exception
64
     *
65
     * @return \Throwable
66
     */
67 2
    public function getException(): \Throwable
68
    {
69 2
        return $this->exception;
70
    }
71
72
    /**
73
     * Set response
74
     *
75
     * @param ResponseInterface $response
76
     */
77 2
    public function setResponse(ResponseInterface $response)
78
    {
79 2
        $this->response = $response;
80 2
    }
81
82
    /**
83
     * Has a response set by previous listeners ?
84
     *
85
     * @return bool
86
     */
87 3
    public function hasResponse(): bool
88
    {
89 3
        return $this->response !== null;
90
    }
91
92
    /**
93
     * Get response
94
     *
95
     * @return ResponseInterface
96
     * @throws \OutOfBoundsException
97
     */
98 2
    public function getResponse(): ResponseInterface
99
    {
100 2
        if ($this->response === null) {
101
            throw new \OutOfBoundsException('Response has never been set.');
102
        }
103
104 2
        return $this->response;
105
    }
106
107
    /**
108
     * Disable dispatching of response-event.
109
     * Enabled by default.
110
     */
111 1
    public function disableResponseEvent()
112
    {
113 1
        $this->responseEventEnabled = false;
114 1
    }
115
116
    /**
117
     * Enable dispatching of response-event.
118
     * Can be useful to reenable after has been disabled by another event-listener.
119
     */
120
    public function enableResponseEvent()
121
    {
122
        $this->responseEventEnabled = false;
123
    }
124
125
    /**
126
     * Is response event enabled to dispatch in the case if the exception
127
     * will be resolved to a response (through setResponse()) ?
128
     *
129
     * @return bool
130
     */
131 2
    public function isResponseEventEnabled(): bool
132
    {
133 2
        return $this->responseEventEnabled;
134
    }
135
}