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 ( aad7d6...27e87e )
by Vlad
01:54
created

HttpDataCollector   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 44
dl 0
loc 182
rs 10
c 3
b 0
f 0
wmc 21

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getCallCount() 0 3 1
A reset() 0 7 1
A addTotalTime() 0 3 1
A getMessages() 0 11 3
A getName() 0 3 1
A __construct() 0 6 1
A hasSlowResponses() 0 3 1
A getErrorCount() 0 3 1
A getTotalTime() 0 3 1
A getLogGroup() 0 7 2
A doCollect() 0 25 5
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 $logger;
22
23
    /** @var float */
24
    private $slowResponseTime;
25
26
    /**
27
     * @param \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface $logger
28
     * @param float|int $slowResponseTime Time in seconds
29
     */
30
    public function __construct(LoggerInterface $logger, float $slowResponseTime)
31
    {
32
        $this->logger = $logger;
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 = $this->logger->getMessages();
44
45
        if ($this->slowResponseTime > 0) {
46
            foreach ($messages as $message) {
47
                if (!$message instanceof LogMessage) {
48
                    continue;
49
                }
50
51
                if ($message->getTransferTime() >= $this->slowResponseTime) {
52
                    $this->data['hasSlowResponse'] = true;
53
                    break;
54
                }
55
            }
56
        }
57
58
        $requestId = $request->getUri();
59
60
        // clear log to have only messages related to Symfony request context
61
        $this->logger->clear();
62
63
        $logGroup = $this->getLogGroup($requestId);
64
        $logGroup->setRequestName($request->getPathInfo());
65
        $logGroup->addMessages($messages);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getName() : string
72
    {
73
        return 'eight_points_guzzle';
74
    }
75
76
    /**
77
     * Resets this data collector to its initial state.
78
     *
79
     * @return void
80
     */
81
    public function reset() : void
82
    {
83
        $this->data = [
84
            'logs' => [],
85
            'callCount' => 0,
86
            'totalTime' => 0,
87
            'hasSlowResponse' => false,
88
        ];
89
    }
90
91
    /**
92
     * Returning log entries
93
     *
94
     * @return array
95
     */
96
    public function getLogs() : array
97
    {
98
        return array_key_exists('logs', $this->data) ? $this->data['logs'] : [];
0 ignored issues
show
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...
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

98
        return array_key_exists('logs', /** @scrutinizer ignore-type */ $this->data) ? $this->data['logs'] : [];
Loading history...
99
    }
100
101
    /**
102
     * Get all messages
103
     *
104
     * @return array
105
     */
106
    public function getMessages() : array
107
    {
108
        $messages = [];
109
110
        foreach ($this->getLogs() as $log) {
111
            foreach ($log->getMessages() as $message) {
112
                $messages[] = $message;
113
            }
114
        }
115
116
        return $messages;
117
    }
118
119
    /**
120
     * Return amount of http calls
121
     *
122
     * @return integer
123
     */
124
    public function getCallCount() : int
125
    {
126
        return count($this->getMessages());
127
    }
128
129
    /**
130
     * Get Error Count
131
     *
132
     * @return integer
133
     */
134
    public function getErrorCount() : int
135
    {
136
        return count($this->getErrorsByType(LogLevel::ERROR));
137
    }
138
139
    /**
140
     * @param string $type
141
     *
142
     * @return array
143
     */
144
    public function getErrorsByType(string $type) : array
145
    {
146
        return array_filter(
147
            $this->getMessages(),
148
            function (LogMessage $message) use ($type) {
149
                return $message->getLevel() === $type;
150
            }
151
        );
152
    }
153
154
    /**
155
     * Get total time of all requests
156
     *
157
     * @return float
158
     */
159
    public function getTotalTime() : float
160
    {
161
        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...
162
    }
163
164
    /**
165
     * Check if there were any slow responses
166
     *
167
     * @return bool
168
     */
169
    public function hasSlowResponses() : bool
170
    {
171
        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...
172
    }
173
174
    /**
175
     * @param float $time
176
     *
177
     * @return void
178
     */
179
    public function addTotalTime(float $time) : void
180
    {
181
        $this->data['totalTime'] += $time;
182
    }
183
184
    /**
185
     * Returns (new) LogGroup based on given id
186
     *
187
     * @param string $id
188
     *
189
     * @return \EightPoints\Bundle\GuzzleBundle\Log\LogGroup
190
     */
191
    protected function getLogGroup(string $id) : LogGroup
192
    {
193
        if (!isset($this->data['logs'][$id])) {
194
            $this->data['logs'][$id] = new LogGroup();
195
        }
196
197
        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...
198
    }
199
}
200