Completed
Push — master ( ac4bf2...b53b48 )
by Alessandro
03:38
created

Collection   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 142
Duplicated Lines 33.8 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 11
c 2
b 1
f 1
lcom 1
cbo 3
dl 48
loc 142
ccs 54
cts 54
cp 1
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A find() 0 8 1
A findOne() 8 8 1
A findOneAndUpdate() 8 8 1
A findOneAndDelete() 8 8 1
A deleteMany() 8 8 1
A deleteOne() 8 8 1
A replaceOne() 0 12 2
A insertOne() 8 8 1
A startQueryLogging() 0 13 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 11
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger)
32
    {
33 11
        parent::__construct($manager, $databaseName, $collectionName, $options);
34 11
        $this->logger = $logger;
35 11
    }
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 deleteMany($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...
89
    {
90 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
91 1
        $result = parent::deleteMany($filter, $options);
92 1
        $this->logger->logQuery($event);
93
94 1
        return $result;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 1 View Code Duplication
    public function deleteOne($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...
101
    {
102 1
        $event = $this->startQueryLogging($filter, __FUNCTION__);
103 1
        $result = parent::deleteOne($filter, $options);
104 1
        $this->logger->logQuery($event);
105
106 1
        return $result;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function replaceOne($filter, $replacement, array $options = [])
113
    {
114
        $data = [
115 1
            "filter" => $filter,
116 1
            "replacement" => is_array($replacement) ? $replacement : $replacement->toArray()
117
        ];
118 1
        $event = $this->startQueryLogging($data, __FUNCTION__);
119 1
        $result = parent::replaceOne($filter, $replacement, $options);
120 1
        $this->logger->logQuery($event);
121
122 1
        return $result;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 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...
129
    {
130 1
        $event = $this->startQueryLogging($document, __FUNCTION__);
131 1
        $result = parent::insertOne($document, $options);
132 1
        $this->logger->logQuery($event);
133
134 1
        return $result;
135
    }
136
137
    /**
138
     * @param        $filter
139
     * @param string $method
140
     *
141
     * @return LogEvent
142
     */
143 8
    private function startQueryLogging($filter, string $method): LogEvent
144
    {
145 8
        $debugInfo = $this->__debugInfo();
146
147 8
        $event = new LogEvent();
148 8
        $event->setData($filter);
149 8
        $event->setMethod($method);
150 8
        $event->setCollection($debugInfo['collectionName']);
151
152 8
        $this->logger->startLogging($event);
153
154 8
        return $event;
155
    }
156
}
157
158