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.

RequestTimeMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
namespace EightPoints\Bundle\GuzzleBundle\Middleware;
4
5
use EightPoints\Bundle\GuzzleBundle\Log\Logger;
6
use EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface;
7
use EightPoints\Bundle\GuzzleBundle\DataCollector\HttpDataCollector;
8
use Psr\Http\Message\RequestInterface;
9
use GuzzleHttp\TransferStats;
10
11
class RequestTimeMiddleware
12
{
13
    /** @var \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface */
14
    protected $logger;
15
16
    /** @var \EightPoints\Bundle\GuzzleBundle\DataCollector\HttpDataCollector */
17
    private $dataCollector;
18
19
    /**
20
     * @param \EightPoints\Bundle\GuzzleBundle\Log\LoggerInterface $logger
21
     * @param \EightPoints\Bundle\GuzzleBundle\DataCollector\HttpDataCollector $dataCollector
22
     */
23
    public function __construct(LoggerInterface $logger, HttpDataCollector $dataCollector)
24
    {
25
        $this->logger = $logger;
26
        $this->dataCollector = $dataCollector;
27
    }
28
29
    /**
30
     * @param callable $handler
31
     *
32
     * @return \Closure
33
     */
34
    public function __invoke(callable $handler) : \Closure
35
    {
36
        return function (RequestInterface $request, array $options) use ($handler) {
37
            $options['on_stats'] = $this->getOnStatsCallback(
38
                isset($options['on_stats']) ? $options['on_stats'] : null,
39
                isset($options['request_id']) ? $options['request_id'] : null
40
            );
41
42
            // Continue the handler chain.
43
            return $handler($request, $options);
44
        };
45
    }
46
47
    /**
48
     * Create callback for on_stats options.
49
     * If request has on_stats option, it will be called inside of this callback.
50
     *
51
     * @param null|callable $initialOnStats
52
     * @param null|string $requestId
53
     *
54
     * @return \Closure
55
     */
56
    protected function getOnStatsCallback(?callable $initialOnStats, ?string $requestId) : \Closure
57
    {
58
        return function (TransferStats $stats) use ($initialOnStats, $requestId) {
59
            if (is_callable($initialOnStats)) {
60
                call_user_func($initialOnStats, $stats);
61
            }
62
63
            $this->dataCollector->addTotalTime((float)$stats->getTransferTime());
64
65
            if (($this->logger instanceof Logger) && $requestId) {
66
                $this->logger->addTransferTimeByRequestId($requestId, (float)$stats->getTransferTime());
67
            }
68
        };
69
    }
70
}
71