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
Pull Request — master (#156)
by
unknown
01:44
created

HttpDataCollector   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 189
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
dl 0
loc 189
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessages() 0 11 3
A getErrorCount() 0 4 1
A getCallCount() 0 3 1
A getTotalTime() 0 3 1
A getLogs() 0 3 2
A reset() 0 6 1
A addTotalTime() 0 3 1
A getName() 0 3 1
A __construct() 0 5 1
A getIconColor() 0 7 2
A getLogGroup() 0 7 2
A getSymfonyVersion() 0 7 1
A collect() 0 11 1
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
use Symfony\Component\HttpKernel\Kernel;
13
14
/**
15
 * Collecting http data for Symfony profiler
16
 *
17
 * @version 2.1
18
 * @since   2014-11
19
 */
20
class HttpDataCollector extends DataCollector
21
{
22
23
    /*** @var \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface $logger */
24
    protected $logger;
25
26
    /**
27
     * @version 2.1
28
     * @since   2014-11
29
     *
30
     * @param   \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface $logger
31
     */
32
    public function __construct(LoggerInterface $logger)
33
    {
34
        $this->logger = $logger;
35
36
        $this->reset();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @version 2.1
43
     * @since   2014-11
44
     */
45
    public function collect(Request $request, Response $response, \Exception $exception = null)
46
    {
47
        $messages = $this->logger->getMessages();
48
        $requestId = $request->getUri();
49
50
        // clear log to have only messages related to Symfony request context
51
        $this->logger->clear();
52
53
        $logGroup = $this->getLogGroup($requestId);
54
        $logGroup->setRequestName($request->getPathInfo());
55
        $logGroup->addMessages($messages);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     *
61
     * @version 2.1
62
     * @since   2014-11
63
     */
64
    public function getName() : string
65
    {
66
        return 'eight_points_guzzle';
67
    }
68
69
    /**
70
     * Resets this data collector to its initial state.
71
     */
72
    public function reset()
73
    {
74
        $this->data = [
75
            'logs' => [],
76
            'callCount' => 0,
77
            'totalTime' => 0,
78
        ];
79
    }
80
81
    /**
82
     * Returning log entries
83
     *
84
     * @version 2.1
85
     * @since   2014-11
86
     *
87
     * @return  array $logs
88
     */
89
    public function getLogs() : array
90
    {
91
        return array_key_exists('logs', $this->data) ? $this->data['logs'] : [];
92
    }
93
94
    /**
95
     * Get all messages
96
     *
97
     * @version 2.1
98
     * @since   2015-05
99
     *
100
     * @return  array
101
     */
102
    public function getMessages() : array
103
    {
104
        $messages = [];
105
106
        foreach ($this->getLogs() as $log) {
107
            foreach ($log->getMessages() as $message) {
108
                $messages[] = $message;
109
            }
110
        }
111
112
        return $messages;
113
    }
114
115
    /**
116
     * Return amount of http calls
117
     *
118
     * @version 2.1
119
     * @since   2015-05
120
     *
121
     * @return  integer
122
     */
123
    public function getCallCount() : int
124
    {
125
        return count($this->getMessages());
126
    }
127
128
    /**
129
     * Get Error Count
130
     *
131
     * @version 2.2
132
     * @since   2015-05
133
     *
134
     * @return  integer
135
     */
136
    public function getErrorCount() : int
137
    {
138
        return count(array_filter($this->getMessages(), function (LogMessage $message) {
139
            return $message->getLevel() === LogLevel::ERROR;
140
        }));
141
    }
142
143
    /**
144
     * Get total time of all requests
145
     *
146
     * @since  2015-05
147
     *
148
     * @return float
149
     */
150
    public function getTotalTime() : float
151
    {
152
        return $this->data['totalTime'];
153
    }
154
155
    /**
156
     * @param float $time
157
     */
158
    public function addTotalTime(float $time)
159
    {
160
        $this->data['totalTime'] += $time;
161
    }
162
163
    /**
164
     * Returns (new) LogGroup based on given id
165
     *
166
     * @version 2.1
167
     * @since   2015-05
168
     *
169
     * @param   string $id
170
     * @return  LogGroup
171
     */
172
    protected function getLogGroup(string $id) : LogGroup
173
    {
174
        if (!isset($this->data['logs'][$id])) {
175
            $this->data['logs'][$id] = new LogGroup();
176
        }
177
178
        return $this->data['logs'][$id];
179
    }
180
181
    /**
182
     * Return the color used version
183
     * @since 2016-06
184
     *
185
     * @return string
186
     */
187
    public final function getIconColor() : string
188
    {
189
        if ((float)$this->getSymfonyVersion() >= 2.8) {
190
            return $this->data['iconColor'] = '#AAAAAA';
191
        }
192
193
        return $this->data['iconColor'] = '#3F3F3F';
194
    }
195
196
    /**
197
     * Returns current version symfony
198
     * @since  2016-06
199
     *
200
     * @return string
201
     */
202
    private function getSymfonyVersion() : string
203
    {
204
        $symfonyVersion = Kernel::VERSION;
205
        $symfonyVersion = explode('.', $symfonyVersion, -1);
206
        $symfonyMajorMinorVersion = implode('.', $symfonyVersion);
207
208
        return $symfonyMajorMinorVersion;
209
    }
210
}
211