CacheSubscriber::onRequestErrored()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Http Adapter package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\HttpAdapter\Event\Subscriber;
13
14
use Ivory\HttpAdapter\Event\Cache\CacheInterface;
15
use Ivory\HttpAdapter\Event\Events;
16
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent;
17
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent;
18
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
19
use Ivory\HttpAdapter\Event\RequestCreatedEvent;
20
use Ivory\HttpAdapter\Event\RequestErroredEvent;
21
use Ivory\HttpAdapter\Event\RequestSentEvent;
22
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
23
24
/**
25
 * @author GeLo <[email protected]>
26
 */
27
class CacheSubscriber implements EventSubscriberInterface
28
{
29
    /**
30
     * @var CacheInterface
31
     */
32
    private $cache;
33
34
    /**
35
     * @param CacheInterface $cache
36
     */
37 108
    public function __construct(CacheInterface $cache)
38
    {
39 108
        $this->setCache($cache);
40 108
    }
41
42
    /**
43
     * @return CacheInterface
44
     */
45 9
    public function getCache()
46
    {
47 9
        return $this->cache;
48
    }
49
50
    /**
51
     * @param CacheInterface $cache
52
     */
53 108
    public function setCache(CacheInterface $cache)
54
    {
55 108
        $this->cache = $cache;
56 108
    }
57
58
    /**
59
     * @param RequestCreatedEvent $event
60
     */
61 27
    public function onRequestCreated(RequestCreatedEvent $event)
62
    {
63 27
        $request = $event->getRequest();
64 27
        $messageFactory = $event->getHttpAdapter()->getConfiguration()->getMessageFactory();
65
66 27
        if (($response = $this->cache->getResponse($request, $messageFactory)) !== null) {
67 9
            $event->setResponse($response);
68 25
        } elseif (($exception = $this->cache->getException($request, $messageFactory)) !== null) {
69 9
            $event->setException($exception);
70 7
        }
71 27
    }
72
73
    /**
74
     * @param RequestSentEvent $event
75
     */
76 9
    public function onRequestSent(RequestSentEvent $event)
77
    {
78 9
        $this->cache->saveResponse($event->getResponse(), $event->getRequest());
0 ignored issues
show
Bug introduced by
It seems like $event->getResponse() can be null; however, saveResponse() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
79 9
    }
80
81
    /**
82
     * @param RequestErroredEvent $event
83
     */
84 9
    public function onRequestErrored(RequestErroredEvent $event)
85
    {
86 9
        $this->cache->saveException($event->getException(), $event->getException()->getRequest());
0 ignored issues
show
Bug introduced by
It seems like $event->getException()->getRequest() can be null; however, saveException() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
87 9
    }
88
89
    /**
90
     * @param MultiRequestCreatedEvent $event
91
     */
92 27
    public function onMultiRequestCreated(MultiRequestCreatedEvent $event)
93
    {
94 27
        $messageFactory = $event->getHttpAdapter()->getConfiguration()->getMessageFactory();
95
96 27
        foreach ($event->getRequests() as $request) {
97 27
            if (($response = $this->cache->getResponse($request, $messageFactory)) !== null) {
98 9
                $event->addResponse($response);
99 9
                $event->removeRequest($request);
100 25
            } elseif (($exception = $this->cache->getException($request, $messageFactory)) !== null) {
101 9
                $event->addException($exception);
102 13
                $event->removeRequest($request);
103 7
            }
104 21
        }
105 27
    }
106
107
    /**
108
     * @param MultiRequestSentEvent $event
109
     */
110 9
    public function onMultiRequestSent(MultiRequestSentEvent $event)
111
    {
112 9
        foreach ($event->getResponses() as $response) {
113 9
            $this->cache->saveResponse($response, $response->getParameter('request'));
114 7
        }
115 9
    }
116
117
    /**
118
     * @param MultiRequestErroredEvent $event
119
     */
120 9
    public function onMultiRequestErrored(MultiRequestErroredEvent $event)
121
    {
122 9
        foreach ($event->getResponses() as $response) {
123 9
            $this->cache->saveResponse($response, $response->getParameter('request'));
124 7
        }
125
126 9
        foreach ($event->getExceptions() as $exception) {
127 9
            $this->cache->saveException($exception, $exception->getRequest());
0 ignored issues
show
Bug introduced by
It seems like $exception->getRequest() can be null; however, saveException() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
128 7
        }
129 9
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134 9 View Code Duplication
    public static function getSubscribedEvents()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
    {
136
        return [
137 9
            Events::REQUEST_CREATED       => ['onRequestCreated', -100],
138 7
            Events::REQUEST_SENT          => ['onRequestSent', -100],
139 7
            Events::REQUEST_ERRORED       => ['onRequestErrored', -100],
140 7
            Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', -100],
141 7
            Events::MULTI_REQUEST_SENT    => ['onMultiRequestSent', -100],
142 7
            Events::MULTI_REQUEST_ERRORED => ['onMultiRequestErrored', -100],
143 7
        ];
144
    }
145
}
146