CacheSubscriber   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 119
Duplicated Lines 9.24 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 11
dl 11
loc 119
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCache() 0 4 1
A setCache() 0 4 1
A onRequestCreated() 0 11 3
A onRequestSent() 0 4 1
A onRequestErrored() 0 4 1
A onMultiRequestCreated() 0 14 4
A onMultiRequestSent() 0 6 2
A onMultiRequestErrored() 0 10 3
A getSubscribedEvents() 11 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 GeLo
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 GeLo
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 GeLo
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 GeLo
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