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\Settings\General; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
16
|
|
|
use ONGR\ElasticsearchBundle\Service\Repository; |
17
|
|
|
use ONGR\ElasticsearchBundle\Service\Manager; |
18
|
|
|
use ONGR\SettingsBundle\Document\Setting; |
19
|
|
|
use ONGR\SettingsBundle\Event\SettingChangeEvent; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class SettingsManager responsible for managing settings actions. |
23
|
|
|
*/ |
24
|
|
|
class SettingsManager |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var TranslatorInterface |
28
|
|
|
*/ |
29
|
|
|
protected $translator; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EventDispatcherInterface |
33
|
|
|
*/ |
34
|
|
|
protected $eventDispatcher; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Manager |
38
|
|
|
*/ |
39
|
|
|
protected $manager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Repository |
43
|
|
|
*/ |
44
|
|
|
protected $repo; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param TranslatorInterface $translator |
48
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
49
|
|
|
* @param Manager $manager |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
TranslatorInterface $translator, |
53
|
|
|
EventDispatcherInterface $eventDispatcher, |
54
|
|
|
Manager $manager |
55
|
|
|
) { |
56
|
|
|
$this->translator = $translator; |
57
|
|
|
$this->eventDispatcher = $eventDispatcher; |
58
|
|
|
$this->manager = $manager; |
59
|
|
|
$this->repo = $this->manager->getRepository('ONGRSettingsBundle:Setting'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Overwrites setting with given name. |
64
|
|
|
* |
65
|
|
|
* @param string $name |
66
|
|
|
* @param string $type |
67
|
|
|
* @param string $description |
68
|
|
|
* @param string|array $value |
69
|
|
|
* @param array $profiles |
70
|
|
|
* |
71
|
|
|
* @throws \LogicException |
72
|
|
|
*/ |
73
|
|
|
public function set($name, $type, $description, $value, $profiles) |
74
|
|
|
{ |
75
|
|
|
foreach ($profiles as $profile) { |
76
|
|
|
$setting = new Setting(); |
77
|
|
|
$setting->setId($profile . '_' . $name); |
78
|
|
|
$setting->setName($name); |
79
|
|
|
$setting->setDescription($description); |
80
|
|
|
$setting->setData((object)['value' => $value]); |
81
|
|
|
$setting->setType($type); |
82
|
|
|
$setting->setProfile($profile); |
83
|
|
|
|
84
|
|
|
$this->manager->persist($setting); |
85
|
|
|
} |
86
|
|
|
$this->manager->commit(); |
87
|
|
|
$this->manager->flush(); |
88
|
|
|
|
89
|
|
|
$this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('save')); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Saves setting. |
94
|
|
|
* |
95
|
|
|
* @param array $settings |
96
|
|
|
*/ |
97
|
|
|
public function save(array $settings) |
98
|
|
|
{ |
99
|
|
|
foreach ($settings as $setting) { |
100
|
|
|
$this->manager->persist($setting); |
101
|
|
|
} |
102
|
|
|
$this->manager->commit(); |
103
|
|
|
|
104
|
|
|
$this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('save')); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Removes a setting. |
109
|
|
|
* |
110
|
|
|
* @param Setting $setting |
111
|
|
|
*/ |
112
|
|
|
public function remove(Setting $setting) |
113
|
|
|
{ |
114
|
|
|
$this->repo->remove($setting->getId()); |
115
|
|
|
$this->manager->flush(); |
116
|
|
|
$this->manager->refresh(); |
117
|
|
|
|
118
|
|
|
$this->eventDispatcher->dispatch('ongr_settings.setting_change', new SettingChangeEvent('delete')); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Copy a setting to the new profile. |
123
|
|
|
* |
124
|
|
|
* @param Setting $setting |
125
|
|
|
* @param array $newProfiles |
126
|
|
|
*/ |
127
|
|
|
public function duplicate(Setting $setting, $newProfiles) |
128
|
|
|
{ |
129
|
|
|
$newSettings = []; |
130
|
|
|
foreach ($newProfiles as $profile) { |
131
|
|
|
$newSetting = clone $setting; |
132
|
|
|
|
133
|
|
|
$newSetting->setId($profile . '_' . $setting->getName()); |
134
|
|
|
$newSetting->setProfile($profile); |
135
|
|
|
$newSettings[] = $newSetting; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->save($newSettings); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns setting model by name and profile or creates new if $mustExist is set to FALSE. |
143
|
|
|
* |
144
|
|
|
* @param string $name |
145
|
|
|
* @param string $profile |
146
|
|
|
* @param bool $mustExist |
147
|
|
|
* @param string $type |
148
|
|
|
* |
149
|
|
|
* @throws \UnexpectedValueException |
150
|
|
|
* |
151
|
|
|
* @return Setting |
152
|
|
|
*/ |
153
|
|
|
public function get($name, $profile = 'default', $mustExist = true, $type = 'string') |
154
|
|
|
{ |
155
|
|
|
$setting = $this->repo->find($profile . '_' . $name); |
|
|
|
|
156
|
|
|
if ($setting === null) { |
157
|
|
|
if ($mustExist === true) { |
158
|
|
|
throw new \UnexpectedValueException(); |
159
|
|
|
} |
160
|
|
|
$setting = $this->createSetting($name, $profile, $type); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $setting; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Creates new setting object. |
168
|
|
|
* |
169
|
|
|
* @param string $name |
170
|
|
|
* @param string $profile |
171
|
|
|
* @param string $type |
172
|
|
|
* |
173
|
|
|
* @return Setting |
174
|
|
|
*/ |
175
|
|
|
protected function createSetting($name, $profile, $type) |
176
|
|
|
{ |
177
|
|
|
$setting = new Setting(); |
178
|
|
|
$setting->setId($profile . '_' . $name); |
179
|
|
|
$setting->setName($name); |
180
|
|
|
$setting->setProfile($profile); |
181
|
|
|
$setting->setType($type); |
182
|
|
|
|
183
|
|
|
if ($type == 'array') { |
184
|
|
|
$setting->setData(['value' => null]); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $setting; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: