AccessLog   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 7
dl 0
loc 109
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A combined() 0 6 1
A commonFormat() 0 14 2
A combinedFormat() 0 8 1
B __invoke() 0 17 5
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Utils;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Log\LoggerInterface;
9
use RuntimeException;
10
11
class AccessLog
12
{
13
    use Utils\AttributeTrait;
14
15
    /**
16
     * @var LoggerInterface The router container
17
     */
18
    private $logger;
19
20
    /**
21
     * @var bool
22
     */
23
    private $combined = false;
24
25
    /**
26
     * Set the LoggerInterface instance.
27
     *
28
     * @param LoggerInterface $logger
29
     */
30
    public function __construct(LoggerInterface $logger)
31
    {
32
        $this->logger = $logger;
33
    }
34
35
    /**
36
     * Whether use the combined log format instead the common log format.
37
     *
38
     * @param bool $combined
39
     *
40
     * @return self
41
     */
42
    public function combined($combined = true)
43
    {
44
        $this->combined = $combined;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Execute the middleware.
51
     *
52
     * @param ServerRequestInterface $request
53
     * @param ResponseInterface      $response
54
     * @param callable               $next
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
59
    {
60
        if (!self::hasAttribute($request, ClientIp::KEY)) {
61
            throw new RuntimeException('AccessLog middleware needs ClientIp executed before');
62
        }
63
64
        $response = $next($request, $response);
65
        $message = $this->combined ? self::combinedFormat($request, $response) : self::commonFormat($request, $response);
66
67
        if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
68
            $this->logger->error($message);
69
        } else {
70
            $this->logger->info($message);
71
        }
72
73
        return $response;
74
    }
75
76
    /**
77
     * Generates a message using the Apache's Common Log format
78
     * https://httpd.apache.org/docs/2.4/logs.html#accesslog.
79
     *
80
     * Note: The user identifier (identd) is ommited intentionally
81
     *
82
     * @param ServerRequestInterface $request
83
     * @param ResponseInterface      $response
84
     *
85
     * @return string
86
     */
87
    private static function commonFormat(ServerRequestInterface $request, ResponseInterface $response)
88
    {
89
        return sprintf('%s %s [%s] "%s %s %s/%s" %d %d',
90
            ClientIp::getIp($request),
91
            $request->getUri()->getUserInfo() ?: '-',
92
            strftime('%d/%b/%Y:%H:%M:%S %z'),
93
            strtoupper($request->getMethod()),
94
            $request->getUri()->getPath(),
95
            strtoupper($request->getUri()->getScheme()),
96
            $request->getProtocolVersion(),
97
            $response->getStatusCode(),
98
            $response->getBody()->getSize()
99
        );
100
    }
101
102
    /**
103
     * Generates a message using the Apache's Combined Log format
104
     * This is exactly the same than Common Log, with the addition of two more fields: Referer and User-Agent headers.
105
     *
106
     * @param ServerRequestInterface $request
107
     * @param ResponseInterface      $response
108
     *
109
     * @return string
110
     */
111
    private static function combinedFormat(ServerRequestInterface $request, ResponseInterface $response)
112
    {
113
        return sprintf('%s "%s" "%s"',
114
            self::commonFormat($request, $response),
115
            $request->getHeaderLine('Referer'),
116
            $request->getHeaderLine('User-Agent')
117
        );
118
    }
119
}
120