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. |
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 string $name |
137
|
|
|
* @param array $data |
138
|
|
|
* |
139
|
|
|
* @return Setting |
140
|
|
|
*/ |
141
|
|
|
public function create($name, array $data = []) |
142
|
|
|
{ |
143
|
|
|
$existingSetting = $this->get($name); |
144
|
|
|
|
145
|
|
|
if ($existingSetting) { |
146
|
|
|
throw new \LogicException(sprintf('Setting %s already exists.', $name)); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$settingClass = $this->repo->getClassName(); |
150
|
|
|
/** @var Setting $setting */ |
151
|
|
|
$setting = new $settingClass(); |
152
|
|
|
|
153
|
|
|
$setting->setName($name); |
154
|
|
|
|
155
|
|
|
foreach ($data as $key => $value) { |
156
|
|
|
$setting->{'set'.ucfirst($key)}($value); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->manager->persist($setting); |
160
|
|
|
$this->manager->commit(); |
161
|
|
|
|
162
|
|
|
return $setting; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Overwrites setting parameters with given name. |
167
|
|
|
* |
168
|
|
|
* @param string $name |
169
|
|
|
* @param array $data |
170
|
|
|
* |
171
|
|
|
* @return Setting |
172
|
|
|
*/ |
173
|
|
|
public function update($name, $data = []) |
174
|
|
|
{ |
175
|
|
|
$setting = $this->get($name); |
176
|
|
|
|
177
|
|
|
if (!$setting) { |
178
|
|
|
throw new \LogicException(sprintf('Setting %s not exist.', $name)); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
foreach ($data as $key => $value) { |
182
|
|
|
$setting->{'set'.ucfirst($key)}($value); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$this->manager->persist($setting); |
186
|
|
|
$this->manager->commit(); |
187
|
|
|
|
188
|
|
|
return $setting; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Deletes a setting. |
193
|
|
|
* |
194
|
|
|
* @param string $name |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function delete($name) |
199
|
|
|
{ |
200
|
|
|
$setting = $this->repo->findOneBy(['name' => $name]); |
201
|
|
|
return $this->repo->remove($setting->getId()); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Returns setting object. |
206
|
|
|
* |
207
|
|
|
* @param string $name |
208
|
|
|
* |
209
|
|
|
* @return Setting |
210
|
|
|
*/ |
211
|
|
|
public function get($name) |
212
|
|
|
{ |
213
|
|
|
/** @var Setting $setting */ |
214
|
|
|
$setting = $this->repo->findOneBy(['name' => $name]); |
215
|
|
|
|
216
|
|
|
if (!$setting) { |
217
|
|
|
throw new SettingNotFoundException(sprintf('Setting %s not exist.', $name)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $setting; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Get setting value by current active profiles setting. |
225
|
|
|
* |
226
|
|
|
* @param string $name |
227
|
|
|
* @param bool $default |
228
|
|
|
* |
229
|
|
|
* @return mixed |
230
|
|
|
*/ |
231
|
|
|
public function getValue($name, $default = false) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
return null; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Get all full profile information. |
240
|
|
|
* |
241
|
|
|
* @param string $search Optional search term to search across all profile data. |
242
|
|
|
* |
243
|
|
|
* @return array |
244
|
|
|
*/ |
245
|
|
|
public function getAllProfiles($search = null) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$profiles = []; |
248
|
|
|
|
249
|
|
|
$search = $this->repo->createSearch(); |
250
|
|
|
$topHitsAgg = new TopHitsAggregation('documents', 20); |
251
|
|
|
$termAgg = new TermsAggregation('profiles', 'profile'); |
252
|
|
|
$termAgg->addAggregation($topHitsAgg); |
253
|
|
|
$search->addAggregation($termAgg); |
254
|
|
|
|
255
|
|
|
$result = $this->repo->execute($search); |
256
|
|
|
|
257
|
|
|
/** @var Setting $activeProfiles */ |
258
|
|
|
$activeProfiles = $this->get($this->activeProfilesSetting, []); |
|
|
|
|
259
|
|
|
|
260
|
|
|
/** @var AggregationValue $agg */ |
261
|
|
|
foreach ($result->getAggregation('profiles') as $agg) { |
|
|
|
|
262
|
|
|
$settings = []; |
263
|
|
|
$docs = $agg->getAggregation('documents'); |
264
|
|
|
foreach ($docs['hits']['hits'] as $doc) { |
265
|
|
|
$settings[] = $doc['_source']['name']; |
266
|
|
|
} |
267
|
|
|
$name = $agg->getValue('key'); |
268
|
|
|
$profiles[] = [ |
269
|
|
|
'active' => $activeProfiles ? in_array($agg->getValue('key'), $activeProfiles->getValue()) : false, |
270
|
|
|
'name' => $name, |
271
|
|
|
'settings' => implode(', ', $settings), |
272
|
|
|
]; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $profiles; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get only profile names. |
280
|
|
|
* |
281
|
|
|
* @param bool $onlyActive |
282
|
|
|
* |
283
|
|
|
* @return array |
284
|
|
|
*/ |
285
|
|
|
public function getAllProfilesNameList($onlyActive = false) |
286
|
|
|
{ |
287
|
|
|
$profiles = []; |
288
|
|
|
$allProfiles = $this->getAllProfiles(); |
289
|
|
|
|
290
|
|
|
foreach ($allProfiles as $profile) { |
291
|
|
|
if ($onlyActive and !$profile['active']) { |
|
|
|
|
292
|
|
|
continue; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$profiles[] = $profile['name']; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
return $profiles; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.