Completed
Pull Request — master (#8)
by Alessandro
02:58
created

Collection::find()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
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 8
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger)
32
    {
33 8
        parent::__construct($manager, $databaseName, $collectionName, $options);
34 8
        $this->logger = $logger;
35 8
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1
    public function find($filter = [], array $options = [])
41
    {
42 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
43 1
        $result = parent::find($filter, $options);
44 1
        $this->logger->logQuery($event);
45
46 1
        return $result;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1 View Code Duplication
    public function findOne($filter = [], array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
55 1
        $result = parent::findOne($filter, $options);
56 1
        $this->logger->logQuery($event);
57
58 1
        return $result;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1 View Code Duplication
    public function findOneAndUpdate($filter, $update, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
    {
66 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
67 1
        $result = parent::findOneAndUpdate($filter, $update, $options);
68 1
        $this->logger->logQuery($event);
69
70 1
        return $result;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 1 View Code Duplication
    public function findOneAndDelete($filter, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
79 1
        $result = parent::findOneAndDelete($filter, $options);
80 1
        $this->logger->logQuery($event);
81
82 1
        return $result;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1 View Code Duplication
    public function insertOne($document, array $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
    {
90 1
        $event = $this->startQueryLogging($document, __FUNCTION__);
91 1
        $result = parent::insertOne($document, $options);
92 1
        $this->logger->logQuery($event);
93
94 1
        return $result;
95
    }
96
97
    /**
98
     * @param        $filter
99
     * @param string $method
100
     *
101
     * @return LogEvent
102
     */
103 5
    private function startQueryLogging($filter, string $method): LogEvent
104
    {
105 5
        $debugInfo = $this->__debugInfo();
106
107 5
        $event = new LogEvent();
108 5
        $event->setData($filter);
109 5
        $event->setMethod($method);
110 5
        $event->setCollection($debugInfo['collectionName']);
111
112 5
        $this->logger->startLogging($event);
113
114 5
        return $event;
115
    }
116
}
117
118