Completed
Pull Request — master (#8)
by Alessandro
03:13
created

Collection   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 23.53%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 3
c 2
b 1
f 1
lcom 0
cbo 3
dl 0
loc 57
ccs 4
cts 17
cp 0.2353
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 8 1
A startQueryLogging() 0 13 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 1
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger)
32
    {
33 1
        parent::__construct($manager, $databaseName, $collectionName, $options);
34 1
        $this->logger = $logger;
35 1
    }
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