Completed
Push — master ( b2e200...1332b8 )
by Oscar
04:48
created

AccessLog::logger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Psr7Middlewares\Middleware;
4
5
use Psr7Middlewares\Middleware;
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
    /**
14
     * @var LoggerInterface|null The router container
15
     */
16
    private $logger;
17
18
    /**
19
     * @var bool
20
     */
21
    private $combined = false;
22
23
    /**
24
     * Constructor.Set the LoggerInterface instance.
25
     *
26
     * @param LoggerInterface $logger
27
     */
28
    public function __construct(LoggerInterface $logger = null)
29
    {
30
        if ($logger !== null) {
31
            $this->logger($logger);
32
        }
33
    }
34
35
    /**
36
     * Set the LoggerInterface instance.
37
     *
38
     * @param LoggerInterface $logger
39
     *
40
     * @return self
41
     */
42
    public function logger(LoggerInterface $logger)
43
    {
44
        $this->logger = $logger;
45
46
        return $this;
47
    }
48
49
    /**
50
     * Whether use the combined log format instead the common log format.
51
     *
52
     * @param bool $combined
53
     *
54
     * @return self
55
     */
56
    public function combined($combined = true)
57
    {
58
        $this->combined = $combined;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Execute the middleware.
65
     *
66
     * @param ServerRequestInterface $request
67
     * @param ResponseInterface      $response
68
     * @param callable               $next
69
     *
70
     * @return ResponseInterface
71
     */
72
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
73
    {
74
        if (empty($this->logger)) {
75
            throw new RuntimeException('No PSR-4 logger instance has been provided');
76
        }
77
78
        if (!Middleware::hasAttribute($request, ClientIp::KEY)) {
79
            throw new RuntimeException('AccessLog middleware needs ClientIp executed before');
80
        }
81
82
        $message = $this->combined ? self::combinedFormat($request, $response) : self::commonFormat($request, $response);
83
84
        if ($response->getStatusCode() >= 400 && $response->getStatusCode() < 600) {
85
            $this->logger->addError($message);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Log\LoggerInterface as the method addError() does only exist in the following implementations of said interface: Monolog\Logger.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
86
        } else {
87
            $this->logger->addInfo($message);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Psr\Log\LoggerInterface as the method addInfo() does only exist in the following implementations of said interface: Monolog\Logger.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
88
        }
89
90
        return $next($request, $response);
91
    }
92
93
    /**
94
     * Generates a message using the Apache's Common Log format
95
     * https://httpd.apache.org/docs/2.4/logs.html#accesslog.
96
     * 
97
     * Note: The user identifier (identd) is ommited intentionally
98
     * 
99
     * @param ServerRequestInterface $request
100
     * @param ResponseInterface      $response
101
     * 
102
     * @return string
103
     */
104
    private static function commonFormat(ServerRequestInterface $request, ResponseInterface $response)
105
    {
106
        return sprintf('%s %s [%s] "%s %s %s/%s" %d %d',
107
            ClientIp::getIp($request),
108
            $request->getUri()->getUserInfo() ?: '-',
109
            strftime('%d/%b/%Y:%H:%M:%S %z'),
110
            strtoupper($request->getMethod()),
111
            $request->getUri()->getPath(),
112
            strtoupper($request->getUri()->getScheme()),
113
            $request->getProtocolVersion(),
114
            $response->getStatusCode(),
115
            $response->getBody()->getSize()
116
        );
117
    }
118
119
    /**
120
     * Generates a message using the Apache's Combined Log format
121
     * This is exactly the same than Common Log, with the addition of two more fields: Referer and User-Agent headers.
122
     * 
123
     * @param ServerRequestInterface $request
124
     * @param ResponseInterface      $response
125
     * 
126
     * @return string
127
     */
128
    private static function combinedFormat(ServerRequestInterface $request, ResponseInterface $response)
129
    {
130
        return sprintf('%s "%s" "%s"',
131
            self::commonFormat($request, $response),
132
            $request->getHeaderLine('Referer'),
133
            $request->getHeaderLine('User-Agent')
134
        );
135
    }
136
}
137