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.
Completed
Pull Request — master (#7)
by Bas
03:22
created

HttpLog::process()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 8
Ratio 50 %

Importance

Changes 0
Metric Value
dl 8
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 4
nop 2
1
<?php
2
3
namespace LosMiddleware\LosLog;
4
5
use Psr\Http\Message\ResponseInterface as Response;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Http\Message\ServerRequestInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\LogLevel;
12
use Zend\Diactoros\Request\Serializer as RequestSerializer;
13
use Zend\Diactoros\Response\Serializer as ResponseSerializer;
14
use Psr\Http\Server\MiddlewareInterface;
15
16
class HttpLog implements MiddlewareInterface
17
{
18
    private $logger;
19
    private $options;
20
21
    public function __construct(LoggerInterface $logger, $options = [])
22
    {
23
        $this->logger = $logger;
24
        $this->options = array_merge([
25
            'level' => LogLevel::INFO,
26
            'log_request' => true,
27
            'log_response' => true,
28
            'full' => false,
29
        ], $options);
30
    }
31
32
    private function generateRequestLog(Request $request, Response $response)
33
    {
34
        if ($this->options['full']) {
35
            return RequestSerializer::toString($request);
36
        }
37
38
        $msg = sprintf(
39
            "%s %s",
40
            $request->getMethod(),
41
            $request->getRequestTarget()
42
        );
43
44 View Code Duplication
        if ($request->hasHeader('X-Request-Id')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
            $msg .= ' RequestId: '. $request->getHeader('X-Request-Id')[0];
46
        } elseif ($response->hasHeader('X-Request-Id')) {
47
            $msg .= ' RequestId: '. $response->getHeader('X-Request-Id')[0];
48
        }
49
50
        return $msg;
51
    }
52
53
    private function generateResponseLog(Request $request, Response $response)
54
    {
55
        if ($this->options['full']) {
56
            return ResponseSerializer::toString($response);
57
        }
58
59
        $reasonPhrase = $response->getReasonPhrase();
60
        $msg = sprintf(
61
            "%s %s",
62
            $response->getStatusCode(),
63
            ($reasonPhrase ? $reasonPhrase : '')
64
        );
65
66 View Code Duplication
        if ($response->hasHeader('X-Request-Id')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
67
            $msg .= ' RequestId: '. $response->getHeader('X-Request-Id')[0];
68
        } elseif ($request->hasHeader('X-Request-Id')) {
69
            $msg .= ' RequestId: '. $request->getHeader('X-Request-Id')[0];
70
        }
71
        if ($response->hasHeader('X-Response-Time')) {
72
            $msg .= ' ResponseTime: '. $response->getHeader('X-Response-Time')[0];
73
        }
74
75
        return $msg;
76
    }
77
78
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
79
    {
80
        $response = $handler->handle($request);
81
82 View Code Duplication
        if ($this->options['log_request']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
83
            $requestMessage = $this->generateRequestLog($request, $response);
84
            $this->logger->log($this->options['level'], sprintf("Request: %s", $requestMessage));
85
        }
86
87 View Code Duplication
        if ($this->options['log_response']) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
            $responseMessage = $this->generateResponseLog($request, $response);
89
            $this->logger->log($this->options['level'], sprintf("Response: %s", $responseMessage));
90
        }
91
92
        return $response;
93
    }
94
}
95