1 | <?php |
||||||
2 | |||||||
3 | namespace Xiidea\EasyConfigBundle\Services\Manager; |
||||||
4 | |||||||
5 | use Doctrine\DBAL\Types\Types; |
||||||
6 | use Symfony\Component\Form\FormFactoryInterface; |
||||||
7 | use Symfony\Component\Form\FormInterface; |
||||||
8 | use Symfony\Component\Form\FormView; |
||||||
9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
||||||
10 | use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
||||||
11 | use Xiidea\EasyConfigBundle\Model\BaseConfig; |
||||||
12 | use Xiidea\EasyConfigBundle\Services\FormGroup\ConfigGroupInterface; |
||||||
13 | use Xiidea\EasyConfigBundle\Services\Repository\ConfigRepositoryInterface; |
||||||
14 | |||||||
15 | class ConfigManager |
||||||
16 | { |
||||||
17 | private array $groups = []; |
||||||
18 | |||||||
19 | public function __construct( |
||||||
20 | private ConfigRepositoryInterface $repository, |
||||||
21 | private FormFactoryInterface $formFactory, |
||||||
22 | private TokenStorageInterface $tokenStorage, |
||||||
23 | private AuthorizationCheckerInterface $checker, |
||||||
24 | $configurationGroups = [] |
||||||
25 | ) { |
||||||
26 | foreach ($configurationGroups as $group) { |
||||||
27 | $this->groups[$group->getNameSpace()] = $group; |
||||||
28 | } |
||||||
29 | } |
||||||
30 | |||||||
31 | public function addConfigGroup(ConfigGroupInterface $group): void |
||||||
32 | { |
||||||
33 | $this->groups[$group->getNameSpace()] = $group; |
||||||
34 | } |
||||||
35 | |||||||
36 | /** |
||||||
37 | * @return ConfigGroupInterface[] |
||||||
38 | */ |
||||||
39 | public function getConfigurationGroups(): array |
||||||
40 | { |
||||||
41 | $username = $this->getUsername(); |
||||||
42 | $groups = []; |
||||||
43 | |||||||
44 | foreach ($this->groups as $key => $group) { |
||||||
45 | $groups[str_replace($username.'.', '', $key)] = $group; |
||||||
46 | } |
||||||
47 | |||||||
48 | return $groups; |
||||||
49 | } |
||||||
50 | |||||||
51 | public function getConfigurationGroupForms(): array |
||||||
52 | { |
||||||
53 | $return = []; |
||||||
54 | |||||||
55 | foreach ($this->groups as $groupKey => $policyGroup) { |
||||||
56 | $return[] = [ |
||||||
57 | 'key' => $groupKey, |
||||||
58 | 'label' => $policyGroup->getLabel(), |
||||||
59 | 'form' => $this->createFormView($policyGroup, $groupKey), |
||||||
60 | 'isEditable' => $this->checker->isGranted($policyGroup->getAuthorSecurityLevels()), |
||||||
61 | ]; |
||||||
62 | } |
||||||
63 | |||||||
64 | return $return; |
||||||
65 | } |
||||||
66 | |||||||
67 | public function getConfigurationsByGroup(string $groupKey): array |
||||||
68 | { |
||||||
69 | $username = $this->getUsername(); |
||||||
70 | $configurations = $this->repository->getConfigurationByUsernameAndGroup( |
||||||
71 | $username, |
||||||
72 | $groupKey |
||||||
73 | ); |
||||||
74 | $results = []; |
||||||
75 | |||||||
76 | /** |
||||||
77 | * @var BaseConfig $configuration |
||||||
78 | */ |
||||||
79 | foreach ($configurations as $configuration) { |
||||||
80 | $key = str_replace($username.'.', '', $configuration->getId()); |
||||||
81 | $key = str_replace($groupKey.'.', '', $key); |
||||||
82 | |||||||
83 | if (str_contains($configuration->getId(), $username)) { |
||||||
84 | $results[$key] = $configuration->getValue(); |
||||||
85 | } elseif (!array_key_exists($key, $results)) { |
||||||
86 | $results[$key] = $configuration->getValue(); |
||||||
87 | } |
||||||
88 | } |
||||||
89 | |||||||
90 | return $results; |
||||||
91 | } |
||||||
92 | |||||||
93 | public function getConfigurationValueByKey(string $key): string |
||||||
94 | { |
||||||
95 | $username = $this->getUsername(); |
||||||
96 | $configurations = $this->repository->getConfigurationByUsernameAndKey($username, $key); |
||||||
97 | $value = ''; |
||||||
98 | |||||||
99 | /** |
||||||
100 | * @var BaseConfig $configuration |
||||||
101 | */ |
||||||
102 | foreach ($configurations as $configuration) { |
||||||
103 | $value = $configuration->getValue(); |
||||||
104 | |||||||
105 | if (str_contains($configuration->getId(), $username.$key)) { |
||||||
106 | break; |
||||||
107 | } |
||||||
108 | } |
||||||
109 | |||||||
110 | return $value; |
||||||
111 | } |
||||||
112 | |||||||
113 | /** |
||||||
114 | * @param $policyGroup |
||||||
115 | * @param $groupKey |
||||||
116 | */ |
||||||
117 | protected function createFormView(ConfigGroupInterface $policyGroup, $groupKey): FormView |
||||||
118 | { |
||||||
119 | return $policyGroup |
||||||
120 | ->getForm($this->formFactory, $this->getConfigurationsByGroupKey($groupKey)) |
||||||
121 | ->createView(); |
||||||
122 | } |
||||||
123 | |||||||
124 | public function getConfigurationsByGroupKey($group): array |
||||||
125 | { |
||||||
126 | return $this->repository->getValuesByGroupKey($group); |
||||||
127 | } |
||||||
128 | |||||||
129 | public function getConfigurationGroup($key): ConfigGroupInterface |
||||||
130 | { |
||||||
131 | return $this->groups[$key]; |
||||||
132 | } |
||||||
133 | |||||||
134 | private function guessType(FormInterface $item): ?string |
||||||
135 | { |
||||||
136 | $getNormData = $item->getNormData(); |
||||||
137 | |||||||
138 | if (is_string($getNormData)) { |
||||||
139 | return null; |
||||||
140 | } |
||||||
141 | |||||||
142 | if ($getNormData instanceof \DateTime) { |
||||||
143 | return Types::DATE_MUTABLE; |
||||||
144 | } |
||||||
145 | |||||||
146 | if (is_array($getNormData)) { |
||||||
147 | return 'json'; |
||||||
148 | } |
||||||
149 | |||||||
150 | if (is_int($getNormData)) { |
||||||
151 | return Types::INTEGER; |
||||||
152 | } |
||||||
153 | |||||||
154 | return null; |
||||||
155 | } |
||||||
156 | |||||||
157 | public function saveGroupData($key, FormInterface $form) |
||||||
158 | { |
||||||
159 | $types = []; |
||||||
160 | |||||||
161 | foreach ($form->all() as $item) { |
||||||
162 | $types[$item->getName()] = $this->guessType($item); |
||||||
163 | } |
||||||
164 | |||||||
165 | $this->repository->saveMultiple($key, $form->getData(), $types); |
||||||
166 | } |
||||||
167 | |||||||
168 | public function saveUserGroupData($key, FormInterface $form) |
||||||
169 | { |
||||||
170 | $types = []; |
||||||
171 | |||||||
172 | foreach ($form->all() as $item) { |
||||||
173 | $types[$item->getName()] = $this->guessType($item); |
||||||
174 | } |
||||||
175 | |||||||
176 | $formData = $form->getData(); |
||||||
177 | |||||||
178 | foreach ($formData as $k => $val) { |
||||||
179 | $checkBoxKey = $k.'Preference'; |
||||||
180 | |||||||
181 | if (array_key_exists($checkBoxKey, $formData)) { |
||||||
182 | if ($formData[$checkBoxKey]) { |
||||||
183 | unset($formData[$k]); |
||||||
184 | $this->repository->removeByKey($key.'.'.$k); |
||||||
185 | } |
||||||
186 | |||||||
187 | unset($types[$checkBoxKey]); |
||||||
188 | unset($formData[$checkBoxKey]); |
||||||
189 | } |
||||||
190 | } |
||||||
191 | |||||||
192 | $this->repository->saveMultiple($key, $formData, $types); |
||||||
193 | } |
||||||
194 | |||||||
195 | public function getConfigurationGroupForm($group, $data = null): FormInterface |
||||||
196 | { |
||||||
197 | if (null === $data) { |
||||||
198 | $data = $this->getConfigurationsByGroupKey($group); |
||||||
199 | } |
||||||
200 | |||||||
201 | return $this->groups[$group]->getForm($this->formFactory, $data); |
||||||
202 | } |
||||||
203 | |||||||
204 | public function getConfigurationGroupLabel($group): string |
||||||
205 | { |
||||||
206 | return $this->groups[$group]->getLabel(); |
||||||
207 | } |
||||||
208 | |||||||
209 | public function getUserConfigurationValuesByGroupKey($groupKey): array |
||||||
210 | { |
||||||
211 | $username = $this->getUsername(); |
||||||
212 | $configurations = $this->repository->getConfigurationByUsernameAndGroup( |
||||||
213 | $username, |
||||||
214 | $groupKey |
||||||
215 | ); |
||||||
216 | $values = []; |
||||||
217 | |||||||
218 | foreach ($configurations as $configuration) { |
||||||
219 | $key = str_replace("{$username}.", '', $configuration->getId()); |
||||||
220 | $key = str_replace("{$groupKey}.", '', $key); |
||||||
221 | |||||||
222 | if (str_contains($configuration->getId(), $username)) { |
||||||
223 | $values[$key] = $configuration->getValue(); |
||||||
224 | $values[$key.'Preference'] = false; |
||||||
225 | } elseif (!array_key_exists($key, $values)) { |
||||||
226 | $values[$key] = $configuration->getValue(); |
||||||
227 | $values[$key.'Preference'] = true; |
||||||
228 | } |
||||||
229 | } |
||||||
230 | |||||||
231 | return $values; |
||||||
232 | } |
||||||
233 | |||||||
234 | public function concatUsernameWithKey($key): string |
||||||
235 | { |
||||||
236 | $username = $this->getUsername(); |
||||||
237 | |||||||
238 | if (!str_starts_with($key, $username)) { |
||||||
239 | $key = "{$username}.{$key}"; |
||||||
240 | } |
||||||
241 | |||||||
242 | return $key; |
||||||
243 | } |
||||||
244 | |||||||
245 | public function getValueByKey(int $isGlobal, string $key): string |
||||||
246 | { |
||||||
247 | $username = $this->getUsername(); |
||||||
248 | $key = str_replace("{$username}.", '', $key); |
||||||
249 | |||||||
250 | if (!$isGlobal) { |
||||||
251 | $key = $username.'.'.$key; |
||||||
252 | } |
||||||
253 | |||||||
254 | $result = $this->repository->getConfigurationValue($key); |
||||||
255 | |||||||
256 | if ($result === null) { |
||||||
257 | $result = ''; |
||||||
258 | } |
||||||
259 | |||||||
260 | return $result; |
||||||
261 | } |
||||||
262 | |||||||
263 | private function getUsername(): ?string |
||||||
264 | { |
||||||
265 | if (null == $this->tokenStorage->getToken()) { |
||||||
266 | return ''; |
||||||
267 | } |
||||||
268 | |||||||
269 | return $this->tokenStorage->getToken()->getUser()->getUserIdentifier(); |
||||||
270 | } |
||||||
271 | |||||||
272 | protected function typeCast($value, $type) |
||||||
273 | { |
||||||
274 | if (empty($value)) { |
||||||
275 | return null; |
||||||
276 | } |
||||||
277 | |||||||
278 | return match ($type) { |
||||||
279 | Types::DATE_MUTABLE, Types::DATETIME_MUTABLE => new \DateTime($value), |
||||||
280 | Types::BOOLEAN => (bool)$value, |
||||||
281 | Types::INTEGER => (int)$value, |
||||||
282 | Types::JSON => json_decode($value), |
||||||
283 | default => $value, |
||||||
284 | }; |
||||||
285 | } |
||||||
286 | |||||||
287 | public function getConfigurationValue($id, $type = null) |
||||||
288 | { |
||||||
289 | $key = $this->concatUsernameWithKey($id); |
||||||
290 | $value = $this->repository->getConfigurationValue($key); |
||||||
291 | if (null === $value) { |
||||||
292 | $value = $this->repository->getConfigurationValue($id); |
||||||
293 | } |
||||||
294 | |||||||
295 | if (null === $type) { |
||||||
296 | return $value; |
||||||
297 | } |
||||||
298 | |||||||
299 | return $this->typeCast($value, $type); |
||||||
0 ignored issues
–
show
|
|||||||
300 | } |
||||||
301 | |||||||
302 | public function getFrontendConfigValuesByGroup($group): array |
||||||
303 | { |
||||||
304 | $items = $this->repository->loadAllByGroup($group, true, true); |
||||||
305 | $userItems = $this->repository->loadAllByGroup( |
||||||
306 | $this->concatUsernameWithKey($group), |
||||||
307 | true, |
||||||
308 | true |
||||||
309 | ); |
||||||
310 | foreach ($userItems as $key => $item) { |
||||||
311 | if ('DEFAULT' === $item) { |
||||||
312 | continue; |
||||||
313 | } |
||||||
314 | $items[$key] = $item; |
||||||
315 | } |
||||||
316 | |||||||
317 | return $items; |
||||||
318 | } |
||||||
319 | |||||||
320 | public function getGlobalAndUserConfigurationByKey(string $key): ?array |
||||||
321 | { |
||||||
322 | return $this->repository->getGlobalAndUserConfigurationByKey($this->getUsername(), $key); |
||||||
323 | } |
||||||
324 | |||||||
325 | public function getRepository(): ConfigRepositoryInterface |
||||||
326 | { |
||||||
327 | return $this->repository; |
||||||
328 | } |
||||||
329 | |||||||
330 | public function getConfigurationJson($id, $default = []) |
||||||
331 | { |
||||||
332 | $data = $this->repository->find($id); |
||||||
0 ignored issues
–
show
The method
find() does not exist on Xiidea\EasyConfigBundle\...nfigRepositoryInterface .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
333 | |||||||
334 | $data = json_decode($data->getValue(), true); |
||||||
335 | |||||||
336 | if (json_last_error()) { |
||||||
337 | return $default; |
||||||
338 | } |
||||||
339 | |||||||
340 | return array_merge_recursive($default, $data); |
||||||
341 | } |
||||||
342 | |||||||
343 | public function saveConfigurationJson($id, array $data) |
||||||
344 | { |
||||||
345 | if (empty($data)) { |
||||||
346 | return null; |
||||||
347 | } |
||||||
348 | |||||||
349 | $data = json_encode($data); |
||||||
350 | |||||||
351 | if (json_last_error()) { |
||||||
352 | return null; |
||||||
353 | } |
||||||
354 | |||||||
355 | return $this->repository->save($id, $data, 'json'); |
||||||
356 | } |
||||||
357 | } |
||||||
358 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.