DoctrineMongoLogger   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 64
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A logQuery() 0 13 4
A flatValue() 0 12 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * (c) Kris Wallsmith <[email protected]> (https://github.com/doctrine/DoctrineMongoDBBundle/)
7
 */
8
9
namespace Chubbyphp\ServiceProvider\Logger;
10
11
use Psr\Log\LoggerInterface;
12
13
final class DoctrineMongoLogger
14
{
15
    /**
16
     * @var LoggerInterface
17
     */
18
    private $logger;
19
20
    /**
21
     * @var int
22
     */
23
    private $batchInsertThreshold;
24
25
    /**
26
     * @var string
27
     */
28
    private $prefix;
29
30
    /**
31
     * @param LoggerInterface $logger
32
     * @param int             $batchInsertThreshold
33
     * @param string          $prefix
34
     */
35 3
    public function __construct(LoggerInterface $logger, int $batchInsertThreshold, string $prefix)
36
    {
37 3
        $this->logger = $logger;
38 3
        $this->batchInsertThreshold = $batchInsertThreshold;
39 3
        $this->prefix = $prefix;
40 3
    }
41
42
    /**
43
     * @param array $query
44
     */
45 3
    public function logQuery(array $query)
46
    {
47 3
        if (isset($query['batchInsert'])
48 3
            && null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $query['num']) {
49 1
            $query['data'] = '**'.$query['num'].' item(s)**';
50
        }
51
52 3
        array_walk_recursive($query, function (&$value) {
53 3
            $value = $this->flatValue($value);
54 3
        });
55
56 3
        $this->logger->debug($this->prefix.json_encode($query));
57 3
    }
58
59
    /**
60
     * @param mixed $value
61
     *
62
     * @return mixed string
63
     */
64 3
    private function flatValue($value)
65
    {
66 3
        if ($value instanceof \MongoBinData) {
67 1
            return base64_encode($value->bin);
68
        }
69
70 3
        if (is_float($value) && is_infinite($value)) {
71 1
            return ($value < 0 ? '-' : '').'Infinity';
72
        }
73
74 2
        return $value;
75
    }
76
}
77