Completed
Push — master ( c4427d...5bb0c0 )
by Jakob
04:22
created

LoggingService   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 2
dl 0
loc 31
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A query() 0 17 1
1
<?php declare(strict_types = 1);
2
3
namespace JSKOS;
4
5
use Psr\Log\LoggerInterface;
6
use Psr\Log\LogLevel;
7
8
/**
9
 * Logs all queries to a Service with timestamp, duration, and result.
10
 */
11
class LoggingService extends Service
12
{
13
    protected $service;
14
    protected $logger;
15
    protected $level;
16
17
    public function __construct(Service $service, LoggerInterface $logger, $level = LogLevel::DEBUG)
18
    {
19
        $this->service = $service;
20
        $this->logger = $logger;
21
        $this->level = $level;
22
    }
23
24
    public function query(array $query=[], string $path=''): Result {
25
        $time = microtime(true);
26
27
        $result = $this->service->query($query, $path);
28
        $this->logger->log(
29
            $this->level,
30
            "[{time}] ".get_class($this->service)." {path}?{query} {duration}ms",
31
            [
32
                'query' => $query, 
33
                'path' => $path,                
34
                'duration' => microtime(true)-$time,
35
                'result' => $result,
36
            ]
37
        );
38
39
        return $result;
40
    }
41
}
42