1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by IntelliJ IDEA. |
4
|
|
|
* User: gerk |
5
|
|
|
* Date: 26.01.17 |
6
|
|
|
* Time: 12:11 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace PeekAndPoke\Component\Slumber\FrameworkIntegration; |
10
|
|
|
|
11
|
|
|
use PeekAndPoke\Component\Slumber\Data\Cursor; |
12
|
|
|
use PeekAndPoke\Component\Slumber\Data\Error\DuplicateError; |
13
|
|
|
use PeekAndPoke\Component\Slumber\Data\Result; |
14
|
|
|
use PeekAndPoke\Component\Slumber\Data\StorageDriver; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Karsten J. Gerber <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ProfilingStorageDriverDecorator implements StorageDriver |
21
|
|
|
{ |
22
|
|
|
/** @var StorageProfiler */ |
23
|
|
|
private $profiler; |
24
|
|
|
/** @var StorageDriver */ |
25
|
|
|
private $driver; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* ProfilingDriverDecorator constructor. |
29
|
|
|
* |
30
|
|
|
* @param StorageProfiler $profiler |
31
|
|
|
* @param StorageDriver $driver |
32
|
|
|
*/ |
33
|
|
|
public function __construct(StorageProfiler $profiler, StorageDriver $driver) |
34
|
|
|
{ |
35
|
|
|
$this->profiler = $profiler; |
36
|
|
|
$this->driver = $driver; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return \ReflectionClass |
41
|
|
|
*/ |
42
|
|
|
public function getEntityBaseClass() |
43
|
|
|
{ |
44
|
|
|
return $this->driver->getEntityBaseClass(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return mixed // TODO: better return type |
49
|
|
|
*/ |
50
|
|
|
public function buildIndexes() |
51
|
|
|
{ |
52
|
|
|
return $this->profile( |
53
|
|
|
'buildIndexes', |
54
|
|
|
[], |
55
|
|
|
function () { |
56
|
|
|
return $this->driver->buildIndexes(); |
57
|
|
|
} |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $item |
63
|
|
|
* |
64
|
|
|
* @return Result\InsertOneResult |
65
|
|
|
* |
66
|
|
|
* @throws DuplicateError |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function insert($item) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return $this->profile( |
71
|
|
|
'insert', |
72
|
|
|
[ |
73
|
|
|
'type' => \get_class($item), |
74
|
|
|
], |
75
|
|
|
function () use ($item) { |
76
|
|
|
return $this->driver->insert($item); |
77
|
|
|
} |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Insert ore update an item |
83
|
|
|
* |
84
|
|
|
* @param mixed $item |
85
|
|
|
* |
86
|
|
|
* @return Result\SaveOneResult |
87
|
|
|
* |
88
|
|
|
* @throws DuplicateError |
89
|
|
|
*/ |
90
|
|
View Code Duplication |
public function save($item) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
return $this->profile( |
93
|
|
|
'save', |
94
|
|
|
[ |
95
|
|
|
'type' => \get_class($item), |
96
|
|
|
], |
97
|
|
|
function () use ($item) { |
98
|
|
|
return $this->driver->save($item); |
99
|
|
|
} |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $item |
105
|
|
|
* |
106
|
|
|
* @return Result\RemoveResult |
107
|
|
|
*/ |
108
|
|
View Code Duplication |
public function remove($item) |
|
|
|
|
109
|
|
|
{ |
110
|
|
|
return $this->profile( |
111
|
|
|
'remove', |
112
|
|
|
[ |
113
|
|
|
'type' => \get_class($item), |
114
|
|
|
], |
115
|
|
|
function () use ($item) { |
116
|
|
|
return $this->driver->remove($item); |
117
|
|
|
} |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Remove all from this collection |
123
|
|
|
* |
124
|
|
|
* @param array|null $query |
125
|
|
|
* |
126
|
|
|
* @return Result\RemoveResult |
127
|
|
|
*/ |
128
|
|
|
public function removeAll(array $query = null) |
129
|
|
|
{ |
130
|
|
|
return $this->profile( |
131
|
|
|
'removeAll', |
132
|
|
|
[ |
133
|
|
|
'query' => $query |
134
|
|
|
], |
135
|
|
|
function () use ($query) { |
136
|
|
|
return $this->driver->removeAll($query); |
137
|
|
|
} |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $query |
143
|
|
|
* |
144
|
|
|
* @return Cursor |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
public function find(array $query = null) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
return $this->profile( |
149
|
|
|
'find', |
150
|
|
|
[ |
151
|
|
|
'query' => $query, |
152
|
|
|
], |
153
|
|
|
function () use ($query) { |
154
|
|
|
return $this->driver->find($query); |
155
|
|
|
} |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param array|null $query |
161
|
|
|
* |
162
|
|
|
* @return mixed|null |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
public function findOne(array $query = null) |
|
|
|
|
165
|
|
|
{ |
166
|
|
|
return $this->profile( |
167
|
|
|
'findOne', |
168
|
|
|
[ |
169
|
|
|
'query' => $query, |
170
|
|
|
], |
171
|
|
|
function () use ($query) { |
172
|
|
|
return $this->driver->findOne($query); |
173
|
|
|
} |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param string $name |
179
|
|
|
* @param array $data |
180
|
|
|
* @param callable $inner |
181
|
|
|
* |
182
|
|
|
* @return mixed |
183
|
|
|
*/ |
184
|
|
|
private function profile($name, array $data, callable $inner) |
185
|
|
|
{ |
186
|
|
|
$sample = $this->profiler->start($name, $data); |
187
|
|
|
|
188
|
|
|
$result = $inner(); |
189
|
|
|
|
190
|
|
|
$sample->stop(); |
191
|
|
|
|
192
|
|
|
return $result; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
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.