Completed
Push — master ( a34a29...da1581 )
by Jacob
7s
created

Logger   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 11
c 1
b 0
f 1
lcom 1
cbo 0
dl 0
loc 59
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
C logQuery() 0 23 10
1
<?php
2
3
namespace As3\Modlr\Persister\MongoDb;
4
5
use Psr\Log\LoggerInterface;
6
7
/**
8
 * Implements query logging support for the MongoDB Persister
9
 *
10
 * @author Josh Worden <[email protected]>
11
 */
12
class Logger
13
{
14
    /**
15
     * @var LoggerInterface
16
     */
17
    private $logger;
18
19
    /**
20
     * @var string
21
     */
22
    private $prefix;
23
24
    /**
25
     * @var int
26
     */
27
    private $batchInsertThreshold = 4;
28
29
    /**
30
     * DI Constructor
31
     *
32
     * @param   LoggerInterface  $logger
33
     * @param   string      $prefix
34
     */
35
    public function __construct(LoggerInterface $logger, $prefix = 'MongoDB query', $batchThreshold = 4)
36
    {
37
        $this->logger = $logger;
38
        $this->prefix = $prefix;
39
        $this->batchInsertThreshold = $batchThreshold;
40
    }
41
42
    /**
43
     * Logs a mongodb query
44
     *
45
     * @param   array       $query  The query to log
46
     */
47
    public function logQuery(array $query)
48
    {
49
        if (isset($query['batchInsert']) && null !== $this->batchInsertThreshold && $this->batchInsertThreshold <= $query['num']) {
50
            $query['data'] = '**'.$query['num'].' item(s)**';
51
        }
52
53
        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...
54
            if ($value instanceof \MongoBinData) {
55
                $value = base64_encode($value->bin);
56
                return;
57
            }
58
            if (is_float($value) && is_infinite($value)) {
59
                $value = ($value < 0 ? '-' : '') . 'Infinity';
60
                return;
61
            }
62
            if (is_float($value) && is_nan($value)) {
63
                $value = 'NaN';
64
                return;
65
            }
66
        });
67
68
        $this->logger->info($this->prefix, $query);
69
    }
70
}
71