1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Facile\MongoDbBundle\Capsule; |
6
|
|
|
|
7
|
|
|
use Facile\MongoDbBundle\Models\LogEvent; |
8
|
|
|
use Facile\MongoDbBundle\Services\Loggers\DataCollectorLoggerInterface; |
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
|
13 |
|
public function __construct(Manager $manager, $databaseName, $collectionName, array $options = [], DataCollectorLoggerInterface $logger) |
32
|
|
|
{ |
33
|
13 |
|
parent::__construct($manager, $databaseName, $collectionName, $options); |
34
|
13 |
|
$this->logger = $logger; |
35
|
13 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
View Code Duplication |
public function count($filter = [], array $options = []) |
|
|
|
|
41
|
|
|
{ |
42
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options); |
43
|
1 |
|
$result = parent::count($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 find($filter = [], array $options = []) |
|
|
|
|
53
|
|
|
{ |
54
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options); |
55
|
1 |
|
$result = parent::find($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 findOne($filter = [], array $options = []) |
|
|
|
|
65
|
|
|
{ |
66
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options); |
67
|
1 |
|
$result = parent::findOne($filter, $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 findOneAndUpdate($filter, $update, array $options = []) |
|
|
|
|
77
|
|
|
{ |
78
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, $update, $options); |
79
|
1 |
|
$result = parent::findOneAndUpdate($filter, $update, $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 findOneAndDelete($filter, array $options = []) |
|
|
|
|
89
|
|
|
{ |
90
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options); |
91
|
1 |
|
$result = parent::findOneAndDelete($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 deleteMany($filter, array $options = []) |
|
|
|
|
101
|
|
|
{ |
102
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options); |
103
|
1 |
|
$result = parent::deleteMany($filter, $options); |
104
|
1 |
|
$this->logger->logQuery($event); |
105
|
|
|
|
106
|
1 |
|
return $result; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
View Code Duplication |
public function deleteOne($filter, array $options = []) |
|
|
|
|
113
|
|
|
{ |
114
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, null, $options); |
115
|
1 |
|
$result = parent::deleteOne($filter, $options); |
116
|
1 |
|
$this->logger->logQuery($event); |
117
|
|
|
|
118
|
1 |
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
2 |
View Code Duplication |
public function replaceOne($filter, $replacement, array $options = []) |
|
|
|
|
125
|
|
|
{ |
126
|
2 |
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, $replacement, $options); |
127
|
2 |
|
$result = parent::replaceOne($filter, $replacement, $options); |
128
|
2 |
|
$this->logger->logQuery($event); |
129
|
|
|
|
130
|
2 |
|
return $result; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
1 |
View Code Duplication |
public function insertOne($document, array $options = []) |
|
|
|
|
137
|
|
|
{ |
138
|
1 |
|
$event = $this->startQueryLogging(__FUNCTION__, [], $document, $options); |
139
|
1 |
|
$result = parent::insertOne($document, $options); |
140
|
1 |
|
$this->logger->logQuery($event); |
141
|
|
|
|
142
|
1 |
|
return $result; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
View Code Duplication |
public function updateOne($filter, $update, array $options = []) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
$event = $this->startQueryLogging(__FUNCTION__, $filter, $update, $options); |
151
|
|
|
$result = parent::updateOne($filter, $update, $options); |
152
|
|
|
$this->logger->logQuery($event); |
153
|
10 |
|
|
154
|
|
|
return $result; |
155
|
10 |
|
} |
156
|
|
|
|
157
|
10 |
|
/** |
158
|
10 |
|
* @param string $method |
159
|
10 |
|
* @param array $filters |
160
|
10 |
|
* @param array $data |
161
|
10 |
|
* @param array $options |
162
|
10 |
|
* |
163
|
|
|
* @return LogEvent |
164
|
10 |
|
*/ |
165
|
|
|
private function startQueryLogging(string $method, array $filters, $data = null, array $options): LogEvent |
166
|
10 |
|
{ |
167
|
|
|
$debugInfo = $this->__debugInfo(); |
168
|
|
|
|
169
|
|
|
$event = new LogEvent(); |
170
|
|
|
$event->setFilters($filters); |
171
|
|
|
$event->setData($data); |
|
|
|
|
172
|
|
|
$event->setOptions($options); |
173
|
|
|
$event->setMethod($method); |
174
|
|
|
$event->setCollection($debugInfo['collectionName']); |
175
|
|
|
|
176
|
|
|
$this->logger->startLogging($event); |
177
|
|
|
|
178
|
|
|
return $event; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
|
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.