Issues (87)

src/Server/AccessOutput.php (1 issue)

1
<?php
2
3
namespace SwooleTW\Http\Server;
4
5
use DateTimeInterface;
6
use Illuminate\Http\Request;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
10
/**
11
 * Class AccessLog
12
 *
13
 * @codeCoverageIgnore
14
 */
15
class AccessOutput
16
{
17
    /**
18
     * @var \Illuminate\Console\OutputStyle
19
     */
20
    protected $output;
21
22
    /**
23
     * AccessOutput constructor.
24
     *
25
     * @param \Symfony\Component\Console\Output\ConsoleOutput $output
26
     */
27
    public function __construct(ConsoleOutput $output)
28
    {
29
        $this->output = $output;
0 ignored issues
show
Documentation Bug introduced by
It seems like $output of type Symfony\Component\Console\Output\ConsoleOutput is incompatible with the declared type Illuminate\Console\OutputStyle of property $output.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
    }
31
32
    /**
33
     * Access log.
34
     *
35
     * @param \Illuminate\Http\Request $request
36
     * @param \Symfony\Component\HttpFoundation\Response $response
37
     */
38
    public function log(Request $request, Response $response): void
39
    {
40
        $host = $request->url();
41
        $method = $request->method();
42
        $agent = $request->userAgent();
43
        $date = $this->date($response->getDate());
44
        $status = $response->getStatusCode();
45
        $style = $this->style($status);
46
47
        $this->output->writeln(
48
            sprintf("%s %s %s <$style>%d</$style> %s", $host, $date, $method, $status, $agent)
49
        );
50
    }
51
52
    /**
53
     * @param \DateTimeInterface $date
54
     *
55
     * @return string
56
     */
57
    protected function date(DateTimeInterface $date): string
58
    {
59
        return $date->format('Y-m-d H:i:s');
60
    }
61
62
    /**
63
     * @param int $status
64
     *
65
     * @return string
66
     */
67
    protected function style(int $status): string
68
    {
69
        return $status !== Response::HTTP_OK ? 'error' : 'info';
70
    }
71
}
72