Completed
Pull Request — master (#8)
by Alessandro
06:41
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
     * @param array $filter
39
     * @param array $options
40
     *
41
     * @return \MongoDB\Driver\Cursor
42
     */
43
    public function find($filter = [], array $options = [])
44
    {
45
        $event = $this->startQueryLogging($filter, __FUNCTION__);
46
        $result = parent::find($filter, $options);
47
        $this->logger->logQuery($event);
48
49
        return $result;
50
    }
51
52
    /**
53
     * @param        $filter
54
     * @param string $method
55
     *
56
     * @return LogEvent
57
     */
58
    private function startQueryLogging($filter, string $method): LogEvent
59
    {
60
        $debugInfo = $this->__debugInfo();
61
62
        $event = new LogEvent();
63
        $event->setFilter($filter);
64
        $event->setMethod($method);
65
        $event->setCollection($debugInfo['collectionName']);
66
67
        $this->logger->startLogging();
68
69
        return $event;
70
    }
71
}
72
73