GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 0f0f0f...bef6f6 )
by Vlad
02:22
created

HttpDataCollector   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 47
c 3
b 0
f 0
dl 0
loc 187
rs 10
wmc 23

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B doCollect() 0 30 7
A reset() 0 7 1
A addTotalTime() 0 3 1
A getMessages() 0 11 3
A getName() 0 3 1
A hasSlowResponses() 0 3 1
A getErrorCount() 0 3 1
A getCallCount() 0 3 1
A getTotalTime() 0 3 1
A getLogGroup() 0 7 2
A getErrorsByType() 0 6 1
A getLogs() 0 3 2
1
<?php
2
3
namespace EightPoints\Bundle\GuzzleBundle\DataCollector;
4
5
use EightPoints\Bundle\GuzzleBundle\Log\LogGroup;
6
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface;
7
use EightPoints\Bundle\GuzzleBundle\Log\LogMessage;
8
use Psr\Log\LogLevel;
9
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
/**
14
 * Collecting http data for Symfony profiler
15
 */
16
class HttpDataCollector extends DataCollector
17
{
18
    use DataCollectorSymfonyCompatibilityTrait;
19
20
    /** @var \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface[] */
21
    protected $loggers;
22
23
    /** @var float */
24
    private $slowResponseTime;
25
26
    /**
27
     * @param \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface[] $loggers
28
     * @param float|int $slowResponseTime Time in seconds
29
     */
30
    public function __construct(array $loggers, float $slowResponseTime)
31
    {
32
        $this->loggers = $loggers;
33
        $this->slowResponseTime = $slowResponseTime;
34
35
        $this->reset();
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    protected function doCollect(Request $request, Response $response, \Throwable $exception = null)
0 ignored issues
show
Unused Code introduced by
The parameter $response is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    protected function doCollect(Request $request, /** @scrutinizer ignore-unused */ Response $response, \Throwable $exception = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $exception is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

41
    protected function doCollect(Request $request, Response $response, /** @scrutinizer ignore-unused */ \Throwable $exception = null)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        $messages = [];
44
        foreach ($this->loggers as $logger) {
45
            $messages = array_merge($messages, $logger->getMessages());
46
        }
47
48
        if ($this->slowResponseTime > 0) {
49
            foreach ($messages as $message) {
50
                if (!$message instanceof LogMessage) {
51
                    continue;
52
                }
53
54
                if ($message->getTransferTime() >= $this->slowResponseTime) {
55
                    $this->data['hasSlowResponse'] = true;
56
                    break;
57
                }
58
            }
59
        }
60
61
        $requestId = $request->getUri();
62
63
        // clear log to have only messages related to Symfony request context
64
        foreach ($this->loggers as $logger) {
65
            $logger->clear();
66
        }
67
68
        $logGroup = $this->getLogGroup($requestId);
69
        $logGroup->setRequestName($request->getPathInfo());
70
        $logGroup->addMessages($messages);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getName() : string
77
    {
78
        return 'eight_points_guzzle';
79
    }
80
81
    /**
82
     * Resets this data collector to its initial state.
83
     *
84
     * @return void
85
     */
86
    public function reset() : void
87
    {
88
        $this->data = [
89
            'logs' => [],
90
            'callCount' => 0,
91
            'totalTime' => 0,
92
            'hasSlowResponse' => false,
93
        ];
94
    }
95
96
    /**
97
     * Returning log entries
98
     *
99
     * @return array
100
     */
101
    public function getLogs() : array
102
    {
103
        return array_key_exists('logs', $this->data) ? $this->data['logs'] : [];
0 ignored issues
show
Bug introduced by
It seems like $this->data can also be of type Symfony\Component\VarDumper\Cloner\Data; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

103
        return array_key_exists('logs', /** @scrutinizer ignore-type */ $this->data) ? $this->data['logs'] : [];
Loading history...
Bug Best Practice introduced by
The expression return array_key_exists(...>data['logs'] : array() could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
104
    }
105
106
    /**
107
     * Get all messages
108
     *
109
     * @return array
110
     */
111
    public function getMessages() : array
112
    {
113
        $messages = [];
114
115
        foreach ($this->getLogs() as $log) {
116
            foreach ($log->getMessages() as $message) {
117
                $messages[] = $message;
118
            }
119
        }
120
121
        return $messages;
122
    }
123
124
    /**
125
     * Return amount of http calls
126
     *
127
     * @return integer
128
     */
129
    public function getCallCount() : int
130
    {
131
        return count($this->getMessages());
132
    }
133
134
    /**
135
     * Get Error Count
136
     *
137
     * @return integer
138
     */
139
    public function getErrorCount() : int
140
    {
141
        return count($this->getErrorsByType(LogLevel::ERROR));
142
    }
143
144
    /**
145
     * @param string $type
146
     *
147
     * @return array
148
     */
149
    public function getErrorsByType(string $type) : array
150
    {
151
        return array_filter(
152
            $this->getMessages(),
153
            function (LogMessage $message) use ($type) {
154
                return $message->getLevel() === $type;
155
            }
156
        );
157
    }
158
159
    /**
160
     * Get total time of all requests
161
     *
162
     * @return float
163
     */
164
    public function getTotalTime() : float
165
    {
166
        return $this->data['totalTime'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['totalTime'] could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return double. Consider adding an additional type-check to rule them out.
Loading history...
167
    }
168
169
    /**
170
     * Check if there were any slow responses
171
     *
172
     * @return bool
173
     */
174
    public function hasSlowResponses() : bool
175
    {
176
        return $this->data['hasSlowResponse'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['hasSlowResponse'] could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
177
    }
178
179
    /**
180
     * @param float $time
181
     *
182
     * @return void
183
     */
184
    public function addTotalTime(float $time) : void
185
    {
186
        $this->data['totalTime'] += $time;
187
    }
188
189
    /**
190
     * Returns (new) LogGroup based on given id
191
     *
192
     * @param string $id
193
     *
194
     * @return \EightPoints\Bundle\GuzzleBundle\Log\LogGroup
195
     */
196
    protected function getLogGroup(string $id) : LogGroup
197
    {
198
        if (!isset($this->data['logs'][$id])) {
199
            $this->data['logs'][$id] = new LogGroup();
200
        }
201
202
        return $this->data['logs'][$id];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->data['logs'][$id] could return the type Symfony\Component\VarDumper\Cloner\Data|null which is incompatible with the type-hinted return EightPoints\Bundle\GuzzleBundle\Log\LogGroup. Consider adding an additional type-check to rule them out.
Loading history...
203
    }
204
}
205