Logger   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 66
ccs 22
cts 22
cp 1
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setResolvedRoute() 0 4 1
A setHandler() 0 4 1
A start() 0 4 1
A finish() 0 10 1
1
<?php
2
3
namespace Kelemen\ApiNette\Logger;
4
5
use Kelemen\ApiNette\Handler\BaseHandler;
6
use Kelemen\ApiNette\Logger\Storage\LoggerStorageInterface;
7
use Kelemen\ApiNette\Response\ApiResponse;
8
use Kelemen\ApiNette\Route\ResolvedRoute;
9
use Nette\Http\Request;
10
11
class Logger
12
{
13
    /** @var LoggerStorageInterface */
14
    private $storage;
15
16
    /** @var Request */
17
    private $httpRequest;
18
19
    /** @var ResolvedRoute */
20
    private $resolvedRoute;
21
22
    /** @var BaseHandler */
23
    private $handler;
24
25
    /** @var int */
26
    private $start = 0;
27
28
    /**
29
     * @param Request $httpRequest
30
     * @param LoggerStorageInterface $storage
31
     */
32 24
    public function __construct(Request $httpRequest, LoggerStorageInterface $storage)
33
    {
34 24
        $this->httpRequest = $httpRequest;
35 24
        $this->storage = $storage;
36 24
    }
37
38
    /**
39
     * @param ResolvedRoute $resolvedRoute
40
     */
41 22
    public function setResolvedRoute(ResolvedRoute $resolvedRoute)
42
    {
43 22
        $this->resolvedRoute = $resolvedRoute;
44 22
    }
45
46
    /**
47
     * @param BaseHandler $handler
48
     */
49 18
    public function setHandler(BaseHandler $handler)
50
    {
51 18
        $this->handler = $handler;
52 18
    }
53
54
    /**
55
     * Start logging
56
     */
57 12
    public function start()
58
    {
59 12
        $this->start = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
The property $start was declared of type integer, but microtime(true) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
60 12
    }
61
62
    /**
63
     * Finish logging
64
     * @param ApiResponse $response
65
     */
66 12
    public function finish(ApiResponse $response)
67
    {
68 12
        $this->storage->store(
69 12
            $this->httpRequest,
70 12
            $response,
71 12
            round((microtime(true) - $this->start) * 1000),
72 12
            $this->resolvedRoute,
73 12
            $this->handler
74 6
        );
75 12
    }
76
}
77