1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\SettingsBundle\Service; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
15
|
|
|
use ONGR\CookiesBundle\Cookie\Model\GenericCookie; |
16
|
|
|
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue; |
17
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\TermsAggregation; |
18
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\TopHitsAggregation; |
19
|
|
|
use ONGR\SettingsBundle\Exception\SettingNotFoundException; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
21
|
|
|
use ONGR\ElasticsearchBundle\Service\Repository; |
22
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
23
|
|
|
use ONGR\SettingsBundle\Document\Setting; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class SettingsManager responsible for managing settings actions. |
27
|
|
|
*/ |
28
|
|
|
class SettingsManager |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Symfony event dispatcher. |
32
|
|
|
* |
33
|
|
|
* @var EventDispatcherInterface |
34
|
|
|
*/ |
35
|
|
|
private $eventDispatcher; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Elasticsearch manager which handles setting repository. |
39
|
|
|
* |
40
|
|
|
* @var Manager |
41
|
|
|
*/ |
42
|
|
|
private $manager; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Settings repository. |
46
|
|
|
* |
47
|
|
|
* @var Repository |
48
|
|
|
*/ |
49
|
|
|
private $repo; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Cache pool container. |
53
|
|
|
* |
54
|
|
|
* @var CacheProvider |
55
|
|
|
*/ |
56
|
|
|
private $cache; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Cookie storage for active cookies. |
60
|
|
|
* |
61
|
|
|
* @var GenericCookie |
62
|
|
|
*/ |
63
|
|
|
private $activeProfilesCookie; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Active profiles setting name to store in the cache engine. |
67
|
|
|
* |
68
|
|
|
* @var string |
69
|
|
|
*/ |
70
|
|
|
private $activeProfilesSettingName; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Repository $repo |
74
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
75
|
|
|
*/ |
76
|
|
|
public function __construct( |
77
|
|
|
$repo, |
78
|
|
|
EventDispatcherInterface $eventDispatcher |
79
|
|
|
) { |
80
|
|
|
$this->repo = $repo; |
81
|
|
|
$this->manager = $repo->getManager(); |
82
|
|
|
$this->eventDispatcher = $eventDispatcher; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return CacheProvider |
87
|
|
|
*/ |
88
|
|
|
public function getCache() |
89
|
|
|
{ |
90
|
|
|
return $this->cache; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param CacheProvider $cache |
95
|
|
|
*/ |
96
|
|
|
public function setCache($cache) |
97
|
|
|
{ |
98
|
|
|
$this->cache = $cache; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return GenericCookie |
103
|
|
|
*/ |
104
|
|
|
public function getActiveProfilesCookie() |
105
|
|
|
{ |
106
|
|
|
return $this->activeProfilesCookie; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param GenericCookie $activeProfilesCookie |
111
|
|
|
*/ |
112
|
|
|
public function setActiveProfilesCookie($activeProfilesCookie) |
113
|
|
|
{ |
114
|
|
|
$this->activeProfilesCookie = $activeProfilesCookie; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getActiveProfilesSettingName() |
121
|
|
|
{ |
122
|
|
|
return $this->activeProfilesSettingName; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param string $activeProfilesSettingName |
127
|
|
|
*/ |
128
|
|
|
public function setActiveProfilesSettingName($activeProfilesSettingName) |
129
|
|
|
{ |
130
|
|
|
$this->activeProfilesSettingName = $activeProfilesSettingName; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Creates setting. |
135
|
|
|
* |
136
|
|
|
* @param array $data |
137
|
|
|
* |
138
|
|
|
* @return Setting |
139
|
|
|
*/ |
140
|
|
|
public function create(array $data = []) |
141
|
|
|
{ |
142
|
|
|
$data = array_filter($data); |
143
|
|
|
if (!isset($data['name']) || !isset($data['type'])) { |
144
|
|
|
throw new \LogicException('Missing one of the mandatory field!'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (!isset($data['value'])) { |
148
|
|
|
$data['value'] = 0; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$name = $data['name']; |
152
|
|
|
$existingSetting = $this->get($name); |
153
|
|
|
|
154
|
|
|
if ($existingSetting) { |
155
|
|
|
throw new \LogicException(sprintf('Setting %s already exists.', $name)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$settingClass = $this->repo->getClassName(); |
159
|
|
|
/** @var Setting $setting */ |
160
|
|
|
$setting = new $settingClass(); |
161
|
|
|
|
162
|
|
|
#TODO Introduce array populate function in Setting document instead of this foreach. |
163
|
|
|
foreach ($data as $key => $value) { |
164
|
|
|
$setting->{'set'.ucfirst($key)}($value); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$this->manager->persist($setting); |
168
|
|
|
$this->manager->commit(); |
169
|
|
|
|
170
|
|
|
return $setting; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Overwrites setting parameters with given name. |
175
|
|
|
* |
176
|
|
|
* @param string $name |
177
|
|
|
* @param array $data |
178
|
|
|
* |
179
|
|
|
* @return Setting |
180
|
|
|
*/ |
181
|
|
|
public function update($name, $data = []) |
182
|
|
|
{ |
183
|
|
|
$setting = $this->get($name); |
184
|
|
|
|
185
|
|
|
if (!$setting) { |
186
|
|
|
throw new \LogicException(sprintf('Setting %s not exist.', $name)); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
#TODO Add populate function to document class |
190
|
|
|
foreach ($data as $key => $value) { |
191
|
|
|
$setting->{'set'.ucfirst($key)}($value); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->manager->persist($setting); |
195
|
|
|
$this->manager->commit(); |
196
|
|
|
|
197
|
|
|
return $setting; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Deletes a setting. |
202
|
|
|
* |
203
|
|
|
* @param string $name |
204
|
|
|
* |
205
|
|
|
* @return array |
206
|
|
|
*/ |
207
|
|
|
public function delete($name) |
208
|
|
|
{ |
209
|
|
|
$setting = $this->repo->findOneBy(['name' => $name]); |
210
|
|
|
return $this->repo->remove($setting->getId()); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns setting object. |
215
|
|
|
* |
216
|
|
|
* @param string $name |
217
|
|
|
* |
218
|
|
|
* @return Setting |
219
|
|
|
*/ |
220
|
|
|
public function get($name) |
221
|
|
|
{ |
222
|
|
|
/** @var Setting $setting */ |
223
|
|
|
$setting = $this->repo->findOneBy(['name' => $name]); |
224
|
|
|
|
225
|
|
|
return $setting; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Get setting value by current active profiles setting. |
230
|
|
|
* |
231
|
|
|
* @param string $name |
232
|
|
|
* @param bool $default |
233
|
|
|
* |
234
|
|
|
* @return mixed |
235
|
|
|
*/ |
236
|
|
|
public function getValue($name, $default = null) |
237
|
|
|
{ |
238
|
|
|
$setting = $this->get($name); |
239
|
|
|
|
240
|
|
|
if ($setting) { |
241
|
|
|
return $setting->getValue(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return $default; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Get all full profile information. |
249
|
|
|
* |
250
|
|
|
* @return array |
251
|
|
|
*/ |
252
|
|
|
public function getAllProfiles() |
253
|
|
|
{ |
254
|
|
|
$profiles = []; |
255
|
|
|
|
256
|
|
|
$search = $this->repo->createSearch(); |
257
|
|
|
$topHitsAgg = new TopHitsAggregation('documents', 20); |
258
|
|
|
$termAgg = new TermsAggregation('profiles', 'profile'); |
259
|
|
|
$termAgg->addAggregation($topHitsAgg); |
260
|
|
|
$search->addAggregation($termAgg); |
261
|
|
|
|
262
|
|
|
$result = $this->repo->execute($search); |
263
|
|
|
|
264
|
|
|
/** @var Setting $activeProfiles */ |
265
|
|
|
$activeProfiles = $this->getValue($this->activeProfilesSetting, []); |
|
|
|
|
266
|
|
|
|
267
|
|
|
/** @var AggregationValue $agg */ |
268
|
|
|
foreach ($result->getAggregation('profiles') as $agg) { |
|
|
|
|
269
|
|
|
$settings = []; |
270
|
|
|
$docs = $agg->getAggregation('documents'); |
271
|
|
|
foreach ($docs['hits']['hits'] as $doc) { |
272
|
|
|
$settings[] = $doc['_source']['name']; |
273
|
|
|
} |
274
|
|
|
$name = $agg->getValue('key'); |
275
|
|
|
$profiles[] = [ |
276
|
|
|
'active' => $activeProfiles ? in_array($agg->getValue('key'), $activeProfiles->getValue()) : false, |
277
|
|
|
'name' => $name, |
278
|
|
|
'settings' => implode(', ', $settings), |
279
|
|
|
]; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $profiles; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Get only profile names. |
287
|
|
|
* |
288
|
|
|
* @param bool $onlyActive |
289
|
|
|
* |
290
|
|
|
* @return array |
291
|
|
|
*/ |
292
|
|
|
public function getAllProfilesNameList($onlyActive = false) |
293
|
|
|
{ |
294
|
|
|
$profiles = []; |
295
|
|
|
$allProfiles = $this->getAllProfiles(); |
296
|
|
|
|
297
|
|
|
foreach ($allProfiles as $profile) { |
298
|
|
|
if ($onlyActive and !$profile['active']) { |
|
|
|
|
299
|
|
|
continue; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$profiles[] = $profile['name']; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
return $profiles; |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.