1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Capsule; |
6
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Event\QueryEvent; |
8
|
|
|
use Facile\MongoDbBundle\Models\Query; |
9
|
|
|
use MongoDB\Collection as MongoCollection; |
10
|
|
|
use MongoDB\Driver\Manager; |
11
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Collection. |
15
|
|
|
* @internal |
16
|
|
|
*/ |
17
|
|
|
final class Collection extends MongoCollection |
18
|
|
|
{ |
19
|
|
|
/** @var EventDispatcherInterface */ |
20
|
|
|
private $eventDispatcher; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Collection constructor. |
24
|
|
|
* |
25
|
|
|
* @param Manager $manager |
26
|
|
|
* @param string $databaseName |
27
|
|
|
* @param string $collectionName |
28
|
|
|
* @param array $options |
29
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
30
|
|
|
* |
31
|
|
|
* @internal param DataCollectorLoggerInterface $logger |
32
|
|
|
*/ |
33
|
14 |
|
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], EventDispatcherInterface $eventDispatcher) |
34
|
|
|
{ |
35
|
14 |
|
parent::__construct($manager, $databaseName, $collectionName, $options); |
36
|
14 |
|
$this->eventDispatcher = $eventDispatcher; |
37
|
14 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
1 |
View Code Duplication |
public function aggregate(array $pipeline, array $options = []) |
|
|
|
|
43
|
|
|
{ |
44
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, null, $pipeline, $options); |
45
|
1 |
|
$result = parent::aggregate($query->getData(), $query->getOptions()); |
46
|
1 |
|
$this->notifyQueryExecution($query); |
47
|
|
|
|
48
|
1 |
|
return $result; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
1 |
View Code Duplication |
public function count($filter = [], array $options = []) |
|
|
|
|
55
|
|
|
{ |
56
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); |
57
|
1 |
|
$result = parent::count($query->getFilters(), $query->getOptions()); |
58
|
1 |
|
$this->notifyQueryExecution($query); |
59
|
|
|
|
60
|
1 |
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
1 |
View Code Duplication |
public function find($filter = [], array $options = []) |
|
|
|
|
67
|
|
|
{ |
68
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); |
69
|
1 |
|
$result = parent::find($query->getFilters(), $query->getOptions()); |
70
|
1 |
|
$this->notifyQueryExecution($query); |
71
|
|
|
|
72
|
1 |
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
View Code Duplication |
public function findOne($filter = [], array $options = []) |
|
|
|
|
79
|
|
|
{ |
80
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); |
81
|
1 |
|
$result = parent::findOne($query->getFilters(), $query->getOptions()); |
82
|
1 |
|
$this->notifyQueryExecution($query); |
83
|
|
|
|
84
|
1 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
View Code Duplication |
public function findOneAndUpdate($filter, $update, array $options = []) |
|
|
|
|
91
|
|
|
{ |
92
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, $update, $options); |
93
|
1 |
|
$result = parent::findOneAndUpdate($query->getFilters(), $query->getData(), $query->getOptions()); |
94
|
1 |
|
$this->notifyQueryExecution($query); |
95
|
|
|
|
96
|
1 |
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
View Code Duplication |
public function findOneAndDelete($filter, array $options = []) |
|
|
|
|
103
|
|
|
{ |
104
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); |
105
|
1 |
|
$result = parent::findOneAndDelete($query->getFilters(), $query->getOptions()); |
106
|
1 |
|
$this->notifyQueryExecution($query); |
107
|
|
|
|
108
|
1 |
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
2 |
View Code Duplication |
public function deleteMany($filter, array $options = []) |
|
|
|
|
115
|
|
|
{ |
116
|
2 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); |
117
|
2 |
|
$result = parent::deleteMany($query->getFilters(), $query->getOptions()); |
118
|
2 |
|
$this->notifyQueryExecution($query); |
119
|
|
|
|
120
|
2 |
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
1 |
View Code Duplication |
public function deleteOne($filter, array $options = []) |
|
|
|
|
127
|
|
|
{ |
128
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, null, $options); |
129
|
1 |
|
$result = parent::deleteOne($query->getFilters(), $query->getOptions()); |
130
|
1 |
|
$this->notifyQueryExecution($query); |
131
|
|
|
|
132
|
1 |
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
1 |
View Code Duplication |
public function replaceOne($filter, $replacement, array $options = []) |
|
|
|
|
139
|
|
|
{ |
140
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, $replacement, $options); |
141
|
1 |
|
$result = parent::replaceOne($query->getFilters(), $query->getData(), $query->getOptions()); |
142
|
1 |
|
$this->notifyQueryExecution($query); |
143
|
|
|
|
144
|
1 |
|
return $result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritdoc} |
149
|
|
|
*/ |
150
|
2 |
View Code Duplication |
public function insertOne($document, array $options = []) |
|
|
|
|
151
|
|
|
{ |
152
|
2 |
|
$query = $this->prepareQuery(__FUNCTION__, [], $document, $options); |
153
|
2 |
|
$result = parent::insertOne($query->getData(), $query->getOptions()); |
154
|
2 |
|
$this->notifyQueryExecution($query); |
155
|
|
|
|
156
|
2 |
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
1 |
View Code Duplication |
public function updateOne($filter, $update, array $options = []) |
|
|
|
|
163
|
|
|
{ |
164
|
1 |
|
$query = $this->prepareQuery(__FUNCTION__, $filter, $update, $options); |
165
|
1 |
|
$result = parent::updateOne($query->getFilters(), $query->getData(), $query->getOptions()); |
166
|
1 |
|
$this->notifyQueryExecution($query); |
167
|
|
|
|
168
|
1 |
|
return $result; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $method |
173
|
|
|
* @param array|object $filters |
174
|
|
|
* @param array|object $data |
175
|
|
|
* @param array $options |
176
|
|
|
* |
177
|
|
|
* @return Query |
178
|
|
|
*/ |
179
|
11 |
|
private function prepareQuery(string $method, $filters = null, $data = null, array $options): Query |
180
|
|
|
{ |
181
|
11 |
|
$query = new Query(); |
182
|
11 |
|
$query->setFilters($filters); |
|
|
|
|
183
|
11 |
|
$query->setData($data); |
|
|
|
|
184
|
11 |
|
$query->setOptions($options); |
185
|
11 |
|
$query->setMethod($method); |
186
|
11 |
|
$query->setCollection($this->getCollectionName()); |
187
|
|
|
|
188
|
11 |
|
$this->eventDispatcher->dispatch(QueryEvent::QUERY_PREPARED, new QueryEvent($query)); |
189
|
|
|
|
190
|
11 |
|
return $query; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param Query $queryLog |
195
|
|
|
* |
196
|
|
|
* @return Query |
197
|
|
|
*/ |
198
|
11 |
|
private function notifyQueryExecution(Query $queryLog) |
199
|
|
|
{ |
200
|
11 |
|
$queryLog->setExecutionTime(microtime(true) - $queryLog->getStart()); |
201
|
|
|
|
202
|
11 |
|
$this->eventDispatcher->dispatch(QueryEvent::QUERY_EXECUTED, new QueryEvent($queryLog)); |
203
|
11 |
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
|
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.