Test Setup Failed
Push — master ( 42e323...580240 )
by Dominik
03:08
created

DoctrineMongoLogger::flatValue()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 8.8571
cc 5
eloc 6
nc 4
nop 1
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 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
    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 (isset($query['batchInsert'])
48
            && null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $query['num']) {
49
            $query['data'] = '**'.$query['num'].' item(s)**';
50
        }
51
52
        array_walk_recursive($query, function (&$value) {
53
            $value = $this->flatValue($value);
54
        });
55
56
        $this->logger->debug($this->prefix.json_encode($query));
57
    }
58
59
    /**
60
     * @param mixed $value
61
     *
62
     * @return mixed string
63
     */
64
    private function flatValue($value)
65
    {
66
        if ($value instanceof \MongoBinData) {
67
            return base64_encode($value->bin);
68
        }
69
70
        if (is_float($value) && is_infinite($value)) {
71
            return ($value < 0 ? '-' : '').'Infinity';
72
        }
73
74
        return $value;
75
    }
76
}
77