Test Setup Failed
Push — master ( d09c31...a11866 )
by Dominik
02:04
created

DoctrineMongoDbLogger   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C logQuery() 0 31 11
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
class DoctrineMongoDbLogger
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
    public function __construct(LoggerInterface $logger, int $batchInsertThreshold, string $prefix)
36
    {
37
        $this->logger = $logger;
38
        $this->batchInsertThreshold = $batchInsertThreshold;
39
        $this->prefix = $prefix;
40
    }
41
42
    /**
43
     * @param array $query
44
     */
45
    public function logQuery(array $query)
46
    {
47
        if (null === $this->logger) {
48
            return;
49
        }
50
51
        if (isset($query['batchInsert'])
52
            && null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $query['num']) {
53
            $query['data'] = '**'.$query['num'].' item(s)**';
54
        }
55
56
        array_walk_recursive($query, function (&$value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
            if ($value instanceof \MongoBinData) {
58
                $value = base64_encode($value->bin);
59
60
                return;
61
            }
62
            if (is_float($value) && is_infinite($value)) {
63
                $value = ($value < 0 ? '-' : '').'Infinity';
64
65
                return;
66
            }
67
            if (is_float($value) && is_nan($value)) {
68
                $value = 'NaN';
69
70
                return;
71
            }
72
        });
73
74
        $this->logger->debug($this->prefix.json_encode($query));
75
    }
76
}
77