Total Complexity | 58 |
Total Lines | 341 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 3 | Features | 1 |
Complex classes like ConfigManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ConfigManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
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 |
||
243 | } |
||
244 | |||
245 | public function getValueByKey(int $isGlobal, string $key): string |
||
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; |
||
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); |
||
|
|||
300 | } |
||
301 | |||
302 | public function getFrontendConfigValuesByGroup($group): array |
||
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); |
||
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) |
||
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.