Completed
Push — master ( 81e1d8...617a2d )
by Alessandro
63:04 queued 06:11
created

Collection::translateReadPreference()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 14.3902

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 5
cts 13
cp 0.3846
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 14
nc 6
nop 1
crap 14.3902
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 MongoDB\Driver\ReadPreference;
10
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11
12
/**
13
 * Class Collection.
14
 * @internal
15
 */
16
final class Collection extends MongoCollection
17
{
18
    /** @var EventDispatcherInterface */
19
    private $eventDispatcher;
20
21
    /**
22
     * Collection constructor.
23
     *
24
     * @param Manager                  $manager
25
     * @param string                   $databaseName
26
     * @param string                   $collectionName
27
     * @param array                    $options
28
     * @param EventDispatcherInterface $eventDispatcher
29
     *
30
     * @internal param DataCollectorLoggerInterface $logger
31
     */
32 15
    public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], EventDispatcherInterface $eventDispatcher)
33
    {
34 15
        parent::__construct($manager, $databaseName, $collectionName, $options);
35 15
        $this->eventDispatcher = $eventDispatcher;
36 15
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 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...
42
    {
43 1
        $query = $this->prepareQuery(__FUNCTION__, null, $pipeline, $options);
44 1
        $result = parent::aggregate($query->getData(), $query->getOptions());
45 1
        $this->notifyQueryExecution($query);
46
47 1
        return $result;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 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...
54
    {
55 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
56 1
        $result = parent::count($query->getFilters(), $query->getOptions());
57 1
        $this->notifyQueryExecution($query);
58
59 1
        return $result;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 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...
66
    {
67 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
68 1
        $result = parent::find($query->getFilters(), $query->getOptions());
69 1
        $this->notifyQueryExecution($query);
70
71 1
        return $result;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 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...
78
    {
79 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
80 1
        $result = parent::findOne($query->getFilters(), $query->getOptions());
81 1
        $this->notifyQueryExecution($query);
82
83 1
        return $result;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 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...
90
    {
91 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, $update, $options);
92 1
        $result = parent::findOneAndUpdate($query->getFilters(), $query->getData(), $query->getOptions());
93 1
        $this->notifyQueryExecution($query);
94
95 1
        return $result;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 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...
102
    {
103 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
104 1
        $result = parent::findOneAndDelete($query->getFilters(), $query->getOptions());
105 1
        $this->notifyQueryExecution($query);
106
107 1
        return $result;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 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...
114
    {
115 2
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
116 2
        $result = parent::deleteMany($query->getFilters(), $query->getOptions());
117 2
        $this->notifyQueryExecution($query);
118
119 2
        return $result;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 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...
126
    {
127 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, null, $options);
128 1
        $result = parent::deleteOne($query->getFilters(), $query->getOptions());
129 1
        $this->notifyQueryExecution($query);
130
131 1
        return $result;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 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...
138
    {
139 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, $replacement, $options);
140 1
        $result = parent::replaceOne($query->getFilters(), $query->getData(), $query->getOptions());
141 1
        $this->notifyQueryExecution($query);
142
143 1
        return $result;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 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...
150
    {
151 2
        $query = $this->prepareQuery(__FUNCTION__, [], $document, $options);
152 2
        $result = parent::insertOne($query->getData(), $query->getOptions());
153 2
        $this->notifyQueryExecution($query);
154
155 2
        return $result;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 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...
162
    {
163 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, $update, $options);
164 1
        $result = parent::updateOne($query->getFilters(), $query->getData(), $query->getOptions());
165 1
        $this->notifyQueryExecution($query);
166
167 1
        return $result;
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173 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...
174
    {
175 1
        $query = $this->prepareQuery(__FUNCTION__, $filter, ['fieldName' => $fieldName], $options);
176 1
        $result = parent::distinct($fieldName, $query->getFilters(), $query->getOptions());
177 1
        $this->notifyQueryExecution($query);
178
179 1
        return $result;
180
    }
181
182
    /**
183
     * @param string        $method
184
     * @param array|object  $filters
185
     * @param array|object  $data
186
     * @param array         $options
187
     *
188
     * @return Query
189
     */
190 12
    private function prepareQuery(string $method, $filters = null, $data = null, array $options): Query
191
    {
192 12
        $query = new Query();
193 12
        $query->setFilters($filters);
0 ignored issues
show
Bug introduced by
It seems like $filters defined by parameter $filters on line 190 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...
194 12
        $query->setData($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by parameter $data on line 190 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...
195 12
        $query->setOptions($options);
196 12
        $query->setMethod($method);
197 12
        $query->setCollection($this->getCollectionName());
198 12
        $query->setReadPreference(
199 12
            $this->translateReadPreference($options['readPreference'] ?? $this->__debugInfo()['readPreference'])
200
        );
201
202 12
        $this->eventDispatcher->dispatch(QueryEvent::QUERY_PREPARED, new QueryEvent($query));
203
204 12
        return $query;
205
    }
206
207
    /**
208
     * @param ReadPreference $readPreference
209
     *
210
     * @return string
211
     */
212 12
    private function translateReadPreference(ReadPreference $readPreference): string
213
    {
214 12
        switch($readPreference->getMode()){
215 12
            case ReadPreference::RP_PRIMARY:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
216
                return 'primary';
217 12
            case ReadPreference::RP_PRIMARY_PREFERRED:
218 12
                return 'primaryPreferred';
219
            case ReadPreference::RP_SECONDARY:
220
                return 'secondary';
221
            case ReadPreference::RP_SECONDARY_PREFERRED:
222
                return 'secondaryPreferred';
223
            case ReadPreference::RP_NEAREST:
224
                return 'nearest';
225
            default:
226
                return 'undefined';
227
        }
228
    }
229
230
    /**
231
     * @param Query $queryLog
232
     *
233
     * @return Query
234
     */
235 12
    private function notifyQueryExecution(Query $queryLog)
236
    {
237 12
        $queryLog->setExecutionTime(microtime(true) - $queryLog->getStart());
238
239 12
        $this->eventDispatcher->dispatch(QueryEvent::QUERY_EXECUTED, new QueryEvent($queryLog));
240 12
    }
241
}
242
243