StopwatchSubscriber   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 115
Duplicated Lines 9.57 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
lcom 1
cbo 11
dl 11
loc 115
ccs 50
cts 50
cp 1
rs 10
c 1
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getStopwatch() 0 4 1
A onRequestCreated() 0 4 1
A onRequestSent() 0 6 2
A onRequestErrored() 0 4 1
A onMultiRequestCreated() 0 6 2
A onMultiRequestSent() 0 8 2
A onMultiResponseErrored() 0 14 3
A getSubscribedEvents() 11 11 1
A getStopwatchName() 0 4 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\Events;
15
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent;
16
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent;
17
use Ivory\HttpAdapter\Event\MultiRequestSentEvent;
18
use Ivory\HttpAdapter\Event\RequestCreatedEvent;
19
use Ivory\HttpAdapter\Event\RequestErroredEvent;
20
use Ivory\HttpAdapter\Event\RequestSentEvent;
21
use Ivory\HttpAdapter\HttpAdapterInterface;
22
use Ivory\HttpAdapter\Message\InternalRequestInterface;
23
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24
use Symfony\Component\Stopwatch\Stopwatch;
25
26
/**
27
 * @author GeLo <[email protected]>
28
 */
29
class StopwatchSubscriber implements EventSubscriberInterface
30
{
31
    /**
32
     * @var Stopwatch
33
     */
34
    private $stopwatch;
35
36
    /**
37
     * @param Stopwatch $stopwatch
38
     */
39 72
    public function __construct(Stopwatch $stopwatch)
40
    {
41 72
        $this->stopwatch = $stopwatch;
42 72
    }
43
44
    /**
45
     * @return Stopwatch
46
     */
47 9
    public function getStopwatch()
48
    {
49 9
        return $this->stopwatch;
50
    }
51
52
    /**
53
     * @param RequestCreatedEvent $event
54
     */
55 9
    public function onRequestCreated(RequestCreatedEvent $event)
56
    {
57 9
        $this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest()));
58 9
    }
59
60
    /**
61
     * @param RequestSentEvent $event
62
     */
63 9
    public function onRequestSent(RequestSentEvent $event)
64
    {
65 9
        if (!$event->hasException()) {
66 9
            $this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest()));
67 7
        }
68 9
    }
69
70
    /**
71
     * @param RequestErroredEvent $event
72
     */
73 9
    public function onRequestErrored(RequestErroredEvent $event)
74
    {
75 9
        $this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getException()->getRequest()));
0 ignored issues
show
Bug introduced by
It seems like $event->getException()->getRequest() can be null; however, getStopwatchName() 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...
76 9
    }
77
78
    /**
79
     * @param MultiRequestCreatedEvent $event
80
     */
81 9
    public function onMultiRequestCreated(MultiRequestCreatedEvent $event)
82
    {
83 9
        foreach ($event->getRequests() as $request) {
84 9
            $this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $request));
85 7
        }
86 9
    }
87
88
    /**
89
     * @param MultiRequestSentEvent $event
90
     */
91 9
    public function onMultiRequestSent(MultiRequestSentEvent $event)
92
    {
93 9
        foreach ($event->getResponses() as $response) {
94 9
            $this->stopwatch->stop(
95 9
                $this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request'))
96 7
            );
97 7
        }
98 9
    }
99
100
    /**
101
     * @param MultiRequestErroredEvent $event
102
     */
103 9
    public function onMultiResponseErrored(MultiRequestErroredEvent $event)
104
    {
105 9
        foreach ($event->getResponses() as $response) {
106 9
            $this->stopwatch->stop(
107 9
                $this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request'))
108 7
            );
109 7
        }
110
111 9
        foreach ($event->getExceptions() as $exception) {
112 9
            $this->stopwatch->stop(
113 9
                $this->getStopwatchName($event->getHttpAdapter(), $exception->getRequest())
0 ignored issues
show
Bug introduced by
It seems like $exception->getRequest() can be null; however, getStopwatchName() 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...
114 7
            );
115 7
        }
116 9
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 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...
122
    {
123
        return [
124 9
            Events::REQUEST_CREATED       => ['onRequestCreated', 10000],
125 7
            Events::REQUEST_SENT          => ['onRequestSent', -10000],
126 7
            Events::REQUEST_ERRORED       => ['onRequestErrored', -10000],
127 7
            Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 10000],
128 7
            Events::MULTI_REQUEST_SENT    => ['onMultiRequestSent', -10000],
129 7
            Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', -10000],
130 7
        ];
131
    }
132
133
    /**
134
     * @param HttpAdapterInterface     $httpAdapter
135
     * @param InternalRequestInterface $internalRequest
136
     *
137
     * @return string
138
     */
139 54
    private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest)
140
    {
141 54
        return sprintf('ivory.http_adapter.%s (%s)', $httpAdapter->getName(), (string) $internalRequest->getUri());
142
    }
143
}
144