Completed
Push — master ( 4f3de5...9b9e7a )
by Pavel
05:51
created

Logger::__invoke()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 39
Code Lines 31

Duplication

Lines 28
Ratio 71.79 %

Importance

Changes 0
Metric Value
cc 4
eloc 31
nc 4
nop 3
dl 28
loc 39
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace App\Middleware;
4
5
use App\Common\JsonException;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
class Logger
11
{
12
    /**
13
     * @var \Monolog\Logger
14
     */
15
    private $logger;
16
17
    public function __construct(\Monolog\Logger $logger)
18
    {
19
        $this->logger = $logger;
20
    }
21
22
    /**
23
     * Execute the middleware.
24
     *
25
     * @param ServerRequestInterface $request
26
     * @param ResponseInterface      $response
27
     * @param callable               $next
28
     *
29
     * @return ResponseInterface
30
     * @throws JsonException
31
     */
32
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
33
    {
34
        $logger     = $this->logger;
35
        $response   = $next($request, $response);
36
        $uri        = $request->getUri()->getPath();
37
        $statusCode = $response->getStatusCode();
38
39
        switch ($statusCode) {
40 View Code Duplication
            case 500:
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...
41
                $logger->addCritical('Oops!!! the server got 500 error', [
42
                    'ip'     => $request->getAttribute('ip_address'),
43
                    'uri'    => $uri,
44
                    'status' => $statusCode,
45
                ]);
46
                break;
47 View Code Duplication
            case 404:
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...
48
                $logger->addWarning('Someone calling un-existing API action', [
49
                    'ip'     => $request->getAttribute('ip_address'),
50
                    'uri'    => $uri,
51
                    'status' => $statusCode,
52
                ]);
53
                break;
54 View Code Duplication
            case 401:
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...
55
                $logger->addWarning('Someone calling API action without access', [
56
                    'ip'     => $request->getAttribute('ip_address'),
57
                    'uri'    => $uri,
58
                    'status' => $statusCode,
59
                ]);
60
                break;
61 View Code Duplication
            default:
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...
62
                $logger->addInfo('Someone calling existing API action', [
63
                    'ip'     => $request->getAttribute('ip_address'),
64
                    'uri'    => $uri,
65
                    'status' => $statusCode,
66
                ]);
67
                break;
68
        }
69
        return $response;
70
    }
71
}
72