Completed
Pull Request — master (#8)
by Alessandro
06:57
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 5
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Capsule;
6
7
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface;
8
use Facile\MongoDbBundle\Services\Loggers\Model\LogEvent;
9
use MongoDB\Collection as MongoCollection;
10
use MongoDB\Driver\Manager;
11
12
/**
13
 * Class Collection.
14
 */
15
final class Collection extends MongoCollection
16
{
17
    /**
18
     * @var DataCollectorLoggerInterface
19
     */
20
    private $logger;
21
22
    /**
23
     * Collection constructor.
24
     *
25
     * @param Manager                      $manager
26
     * @param string                       $databaseName
27
     * @param string                       $collectionName
28
     * @param array                        $options
29
     * @param DataCollectorLoggerInterface $logger
30
     */
31 2
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger)
32
    {
33 2
        parent::__construct($manager, $databaseName, $collectionName, $options);
34 2
        $this->logger = $logger;
35 2
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function find($filter = [], array $options = [])
41
    {
42
        $event = $this->startQueryLogging($filter, __FUNCTION__);
43
        $result = parent::find($filter, $options);
44
        $this->logger->logQuery($event);
45
46
        return $result;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function insertOne($document, array $options = [])
53
    {
54
        $event = $this->startQueryLogging($document, __FUNCTION__);
55
        $result = parent::insertOne($document, $options);
56
        $this->logger->logQuery($event);
57
58
        return $result;
59
    }
60
61
    /**
62
     * @param        $filter
63
     * @param string $method
64
     *
65
     * @return LogEvent
66
     */
67
    private function startQueryLogging($filter, string $method): LogEvent
68
    {
69
        $debugInfo = $this->__debugInfo();
70
71
        $event = new LogEvent();
72
        $event->setData($filter);
73
        $event->setMethod($method);
74
        $event->setCollection($debugInfo['collectionName']);
75
76
        $this->logger->startLogging($event);
77
78
        return $event;
79
    }
80
}
81
82