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

SymfonyEventDispatcherDecorator::request()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 13
cts 15
cp 0.8667
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 4
nop 1
crap 4.0378
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\HttpClient\Decorator;
5
6
use Mikemirten\Component\JsonApi\HttpClient\Decorator\SymfonyEvent\ExceptionEvent;
7
use Mikemirten\Component\JsonApi\HttpClient\Decorator\SymfonyEvent\RequestEvent;
8
use Mikemirten\Component\JsonApi\HttpClient\Decorator\SymfonyEvent\ResponseEvent;
9
use Mikemirten\Component\JsonApi\HttpClient\Exception\RequestException;
10
use Mikemirten\Component\JsonApi\HttpClient\HttpClientInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Events handling decorator based on the Symfony EventDispatcher component.
17
 *
18
 * @package Mikemirten\Component\JsonApi\HttpClient\Decorator
19
 */
20
class SymfonyEventDispatcherDecorator implements HttpClientInterface
21
{
22
    /**
23
     * HTTP Client
24
     *
25
     * @var HttpClientInterface
26
     */
27
    protected $client;
28
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    protected $dispatcher;
33
34
    /**
35
     * @var string
36
     */
37
    protected $requestEvent;
38
39
    /**
40
     * @var string
41
     */
42
    protected $responseEvent;
43
44
    /**
45
     * @var string
46
     */
47
    protected $exceptionEvent;
48
49
    /**
50
     * SymfonyEventDispatcherDecorator constructor.
51
     *
52
     * @param HttpClientInterface      $client
53
     * @param EventDispatcherInterface $dispatcher
54
     *
55
     * @param string $requestEvent
56
     * @param string $responseEvent
57
     * @param string $exceptionEvent
58
     */
59 2
    public function __construct(
60
        HttpClientInterface      $client,
61
        EventDispatcherInterface $dispatcher,
62
63
        string $requestEvent,
64
        string $responseEvent,
65
        string $exceptionEvent
66
    ) {
67 2
        $this->client     = $client;
68 2
        $this->dispatcher = $dispatcher;
69
70 2
        $this->requestEvent   = $requestEvent;
71 2
        $this->responseEvent  = $responseEvent;
72 2
        $this->exceptionEvent = $exceptionEvent;
73 2
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2
    public function request(RequestInterface $request): ResponseInterface
79
    {
80 2
        $requestEvent = new RequestEvent($request);
81 2
        $this->dispatcher->dispatch($this->requestEvent, $requestEvent);
82
83
        try {
84 2
            $response = $this->client->request($requestEvent->getRequest());
85
        }
86 1
        catch (\Throwable $exception) {
87 1
            $exceptionEvent = new ExceptionEvent($request, $exception);
88 1
            $this->dispatcher->dispatch($this->exceptionEvent, $exceptionEvent);
89
90 1
            if (! $exceptionEvent->hasResponse()) {
91
                throw new RequestException($request, $exception);
0 ignored issues
show
Compatibility introduced by
$exception of type object<Throwable> is not a sub-type of object<Exception>. It seems like you assume a concrete implementation of the interface Throwable to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
92
            }
93
94 1
            $response = $exceptionEvent->getResponse();
95
96 1
            if (! $exceptionEvent->isResponseEventEnabled()) {
97
                return $response;
98
            }
99
        }
100
101 2
        $responseEvent = new ResponseEvent($response);
102 2
        $this->dispatcher->dispatch($this->responseEvent, $responseEvent);
103
104 2
        return $responseEvent->getResponse();
105
    }
106
}