1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Capsule; |
6
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Event\Listener\QueryEvent; |
8
|
|
|
use Facile\MongoDbBundle\Models\QueryLog; |
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 |
|
public function aggregate(array $pipeline, array $options = []) |
43
|
|
|
{ |
44
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, null, $pipeline, $options); |
45
|
1 |
|
$result = parent::aggregate($pipeline, $options); |
46
|
1 |
|
$this->notifyQueryExecution($event); |
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 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, null, $options); |
57
|
1 |
|
$result = parent::count($filter, $options); |
58
|
1 |
|
$this->notifyQueryExecution($event); |
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 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, null, $options); |
69
|
1 |
|
$result = parent::find($filter, $options); |
70
|
1 |
|
$this->notifyQueryExecution($event); |
71
|
|
|
|
72
|
1 |
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
1 |
|
public function findOne($filter = [], array $options = []) |
79
|
|
|
{ |
80
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, null, $options); |
81
|
1 |
|
$result = parent::findOne($filter, $options); |
82
|
1 |
|
$this->notifyQueryExecution($event); |
83
|
|
|
|
84
|
1 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
1 |
|
public function findOneAndUpdate($filter, $update, array $options = []) |
91
|
|
|
{ |
92
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, $update, $options); |
93
|
1 |
|
$result = parent::findOneAndUpdate($filter, $update, $options); |
94
|
1 |
|
$this->notifyQueryExecution($event); |
95
|
|
|
|
96
|
1 |
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
1 |
|
public function findOneAndDelete($filter, array $options = []) |
103
|
|
|
{ |
104
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, null, $options); |
105
|
1 |
|
$result = parent::findOneAndDelete($filter, $options); |
106
|
1 |
|
$this->notifyQueryExecution($event); |
107
|
|
|
|
108
|
1 |
|
return $result; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
2 |
|
public function deleteMany($filter, array $options = []) |
115
|
|
|
{ |
116
|
2 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, null, $options); |
117
|
2 |
|
$result = parent::deleteMany($filter, $options); |
118
|
2 |
|
$this->notifyQueryExecution($event); |
119
|
|
|
|
120
|
2 |
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
1 |
|
public function deleteOne($filter, array $options = []) |
127
|
|
|
{ |
128
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, null, $options); |
129
|
1 |
|
$result = parent::deleteOne($filter, $options); |
130
|
1 |
|
$this->notifyQueryExecution($event); |
131
|
|
|
|
132
|
1 |
|
return $result; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
1 |
|
public function replaceOne($filter, $replacement, array $options = []) |
139
|
|
|
{ |
140
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, $replacement, $options); |
141
|
1 |
|
$result = parent::replaceOne($filter, $replacement, $options); |
142
|
1 |
|
$this->notifyQueryExecution($event); |
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 |
|
$event = $this->prepareEvent(__FUNCTION__, [], $document, $options); |
153
|
2 |
|
$result = parent::insertOne($document, $options); |
154
|
2 |
|
$this->notifyQueryExecution($event); |
155
|
|
|
|
156
|
2 |
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
1 |
|
public function updateOne($filter, $update, array $options = []) |
163
|
|
|
{ |
164
|
1 |
|
$event = $this->prepareEvent(__FUNCTION__, $filter, $update, $options); |
165
|
1 |
|
$result = parent::updateOne($filter, $update, $options); |
166
|
1 |
|
$this->notifyQueryExecution($event); |
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 QueryLog |
178
|
|
|
*/ |
179
|
11 |
|
private function prepareEvent(string $method, $filters = null, $data = null, array $options): QueryLog |
180
|
|
|
{ |
181
|
11 |
|
$debugInfo = $this->__debugInfo(); |
182
|
|
|
|
183
|
11 |
|
$event = new QueryLog(); |
184
|
11 |
|
$event->setFilters($filters); |
|
|
|
|
185
|
11 |
|
$event->setData($data); |
|
|
|
|
186
|
11 |
|
$event->setOptions($options); |
187
|
11 |
|
$event->setMethod($method); |
188
|
11 |
|
$event->setCollection($debugInfo['collectionName']); |
189
|
|
|
|
190
|
11 |
|
$this->eventDispatcher->dispatch(QueryEvent::QUERY_PREPARED, new QueryEvent($event)); |
191
|
|
|
|
192
|
11 |
|
return $event; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param QueryLog $queryLog |
197
|
|
|
* |
198
|
|
|
* @return QueryLog |
199
|
|
|
*/ |
200
|
11 |
|
private function notifyQueryExecution(QueryLog $queryLog) |
201
|
|
|
{ |
202
|
11 |
|
$queryLog->setExecutionTime(microtime(true) - $queryLog->getStart()); |
203
|
|
|
|
204
|
11 |
|
$this->eventDispatcher->dispatch(QueryEvent::QUERY_EXECUTED, new QueryEvent($queryLog)); |
205
|
11 |
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
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.