Total Complexity | 57 |
Total Lines | 314 |
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) |
||
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) |
||
290 | } |
||
291 | } |
||
292 | |||
293 | public function getConfigurationValue($id, $type = null) |
||
294 | { |
||
295 | $key = $this->concatUsernameWithKey($id); |
||
296 | $value = $this->repository->getConfigurationValue($key); |
||
297 | if (null === $value) { |
||
298 | $value = $this->repository->getConfigurationValue($id); |
||
299 | } |
||
300 | |||
301 | if (null === $type) { |
||
302 | return $value; |
||
303 | } |
||
304 | |||
305 | return $this->typeCast($value, $type); |
||
306 | } |
||
307 | |||
308 | public function getFrontendConfigValuesByGroup($group): array |
||
324 | } |
||
325 | |||
326 | public function getGlobalAndUserConfigurationByKey(string $key): ?array |
||
327 | { |
||
328 | return $this->repository->getGlobalAndUserConfigurationByKey($this->getUsername(), $key); |
||
329 | } |
||
330 | } |
||
331 |