Completed
Push — master ( 2a5669...b65547 )
by Alessandro
9s
created

Collection::distinct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace Facile\MongoDbBundle\Capsule;
4
5
use Facile\MongoDbBundle\Event\QueryEvent;
6
use Facile\MongoDbBundle\Models\Query;
7
use MongoDB\Collection as MongoCollection;
8
use MongoDB\Driver\Manager;
9
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10
11
/**
12
 * Class Collection.
13
 * @internal
14
 */
15
final class Collection extends MongoCollection
16
{
17
    /** @var EventDispatcherInterface */
18
    private $eventDispatcher;
19
20
    /**
21
     * Collection constructor.
22
     *
23
     * @param Manager                  $manager
24
     * @param string                   $databaseName
25
     * @param string                   $collectionName
26
     * @param array                    $options
27
     * @param EventDispatcherInterface $eventDispatcher
28
     *
29
     * @internal param DataCollectorLoggerInterface $logger
30
     */
31 15
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], EventDispatcherInterface $eventDispatcher)
32
    {
33 15
        parent::__construct($manager, $databaseName, $collectionName, $options);
34 15
        $this->eventDispatcher = $eventDispatcher;
35 15
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 1 View Code Duplication
    public function aggregate(array $pipeline, 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...
41
    {
42 1
        $query = $this->prepareQuery(__FUNCTION__, null, $pipeline, $options);
43 1
        $result = parent::aggregate($query->getData(), $query->getOptions());
44 1
        $this->notifyQueryExecution($query);
45
46 1
        return $result;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 1 View Code Duplication
    public function count($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
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
55 1
        $result = parent::count($query->getFilters(), $query->getOptions());
56 1
        $this->notifyQueryExecution($query);
57
58 1
        return $result;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1 View Code Duplication
    public function find($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...
65
    {
66 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
67 1
        $result = parent::find($query->getFilters(), $query->getOptions());
68 1
        $this->notifyQueryExecution($query);
69
70 1
        return $result;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 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...
77
    {
78 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
79 1
        $result = parent::findOne($query->getFilters(), $query->getOptions());
80 1
        $this->notifyQueryExecution($query);
81
82 1
        return $result;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 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...
89
    {
90 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, $update, $options);
91 1
        $result = parent::findOneAndUpdate($query->getFilters(), $query->getData(), $query->getOptions());
92 1
        $this->notifyQueryExecution($query);
93
94 1
        return $result;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 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...
101
    {
102 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
103 1
        $result = parent::findOneAndDelete($query->getFilters(), $query->getOptions());
104 1
        $this->notifyQueryExecution($query);
105
106 1
        return $result;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 2 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...
113
    {
114 2
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
115 2
        $result = parent::deleteMany($query->getFilters(), $query->getOptions());
116 2
        $this->notifyQueryExecution($query);
117
118 2
        return $result;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 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...
125
    {
126 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
127 1
        $result = parent::deleteOne($query->getFilters(), $query->getOptions());
128 1
        $this->notifyQueryExecution($query);
129
130 1
        return $result;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1 View Code Duplication
    public function replaceOne($filter, $replacement, 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...
137
    {
138 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, $replacement, $options);
139 1
        $result = parent::replaceOne($query->getFilters(), $query->getData(), $query->getOptions());
140 1
        $this->notifyQueryExecution($query);
141
142 1
        return $result;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 2 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...
149
    {
150 2
        $query = $this->prepareQuery(__FUNCTION__, [], $document, $options);
151 2
        $result = parent::insertOne($query->getData(), $query->getOptions());
152 2
        $this->notifyQueryExecution($query);
153
154 2
        return $result;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1 View Code Duplication
    public function updateOne($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...
161
    {
162 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, $update, $options);
163 1
        $result = parent::updateOne($query->getFilters(), $query->getData(), $query->getOptions());
164 1
        $this->notifyQueryExecution($query);
165
166 1
        return $result;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 1 View Code Duplication
    public function distinct($fieldName, $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...
173
    {
174 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, ['fieldName' => $fieldName], $options);
175 1
        $result = parent::distinct($fieldName, $query->getFilters(), $query->getOptions());
176 1
        $this->notifyQueryExecution($query);
177
178 1
        return $result;
179
    }
180
181
    /**
182
     * @param string $method
183
     * @param array|object  $filters
184
     * @param array|object  $data
185
     * @param array  $options
186
     *
187
     * @return Query
188
     */
189 12
    private function prepareQuery(string $method, $filters = null, $data = null, array $options): Query
190
    {
191 12
        $query = new Query();
192 12
        $query->setFilters($filters);
0 ignored issues
show
Bug introduced by
It seems like $filters defined by parameter $filters on line 189 can also be of type null; however, Facile\MongoDbBundle\Models\Query::setFilters() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
193 12
        $query->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 189 can also be of type null; however, Facile\MongoDbBundle\Models\Query::setData() does only seem to accept array|object, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
194 12
        $query->setOptions($options);
195 12
        $query->setMethod($method);
196 12
        $query->setCollection($this->getCollectionName());
197
198 12
        $this->eventDispatcher->dispatch(QueryEvent::QUERY_PREPARED, new QueryEvent($query));
199
200 12
        return $query;
201
    }
202
203
    /**
204
     * @param Query $queryLog
205
     *
206
     * @return Query
207
     */
208 12
    private function notifyQueryExecution(Query $queryLog)
209
    {
210 12
        $queryLog->setExecutionTime(microtime(true) - $queryLog->getStart());
211
212 12
        $this->eventDispatcher->dispatch(QueryEvent::QUERY_EXECUTED, new QueryEvent($queryLog));
213 12
    }
214
}
215
216