Total Complexity | 106 |
Total Lines | 622 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ThemeLifecycleService 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 ThemeLifecycleService, and based on these observations, apply Extract Interface, too.
1 | <?php declare(strict_types=1); |
||
27 | #[Package('storefront')] |
||
28 | class ThemeLifecycleService |
||
29 | { |
||
30 | /** |
||
31 | * @param EntityRepository<MediaCollection> $mediaRepository |
||
32 | * |
||
33 | * @internal |
||
34 | * |
||
35 | * @decrecated tag:v6.6.0 argument $pluginConfigurationFactory will be mandatory |
||
36 | */ |
||
37 | public function __construct( |
||
38 | private readonly StorefrontPluginRegistryInterface $pluginRegistry, |
||
39 | private readonly EntityRepository $themeRepository, |
||
40 | private readonly EntityRepository $mediaRepository, |
||
41 | private readonly EntityRepository $mediaFolderRepository, |
||
42 | private readonly EntityRepository $themeMediaRepository, |
||
43 | private readonly FileSaver $fileSaver, |
||
44 | private readonly FileNameProvider $fileNameProvider, |
||
45 | private readonly ThemeFileImporterInterface $themeFileImporter, |
||
46 | private readonly EntityRepository $languageRepository, |
||
47 | private readonly EntityRepository $themeChildRepository, |
||
48 | private readonly Connection $connection, |
||
49 | private readonly ?AbstractStorefrontPluginConfigurationFactory $pluginConfigurationFactory |
||
50 | ) { |
||
51 | } |
||
52 | |||
53 | public function refreshThemes( |
||
54 | Context $context, |
||
55 | ?StorefrontPluginConfigurationCollection $configurationCollection = null |
||
56 | ): void { |
||
57 | if ($configurationCollection === null) { |
||
58 | $configurationCollection = $this->pluginRegistry->getConfigurations()->getThemes(); |
||
59 | } |
||
60 | |||
61 | // iterate over all theme configs in the filesystem (plugins/bundles) |
||
62 | foreach ($configurationCollection as $config) { |
||
63 | $this->refreshTheme($config, $context); |
||
64 | } |
||
65 | } |
||
66 | |||
67 | public function refreshTheme(StorefrontPluginConfiguration $configuration, Context $context): void |
||
68 | { |
||
69 | $themeData = []; |
||
70 | $themeData['name'] = $configuration->getName(); |
||
71 | $themeData['technicalName'] = $configuration->getTechnicalName(); |
||
72 | $themeData['author'] = $configuration->getAuthor(); |
||
73 | $themeData['themeJson'] = $configuration->getThemeJson(); |
||
74 | |||
75 | // refresh theme after deleting media |
||
76 | $theme = $this->getThemeByTechnicalName($configuration->getTechnicalName(), $context); |
||
77 | |||
78 | // check if theme config already exists in the database |
||
79 | if ($theme) { |
||
80 | $themeData['id'] = $theme->getId(); |
||
81 | } else { |
||
82 | $themeData['active'] = true; |
||
83 | } |
||
84 | |||
85 | $themeData['translations'] = $this->getTranslationsConfiguration($configuration, $context); |
||
86 | |||
87 | $updatedData = $this->updateMediaInConfiguration($theme, $configuration, $context); |
||
88 | |||
89 | $themeData = array_merge($themeData, $updatedData); |
||
90 | |||
91 | if (!empty($configuration->getConfigInheritance())) { |
||
92 | $themeData = $this->addParentTheme($configuration, $themeData, $context); |
||
93 | } |
||
94 | |||
95 | $writtenEvent = $this->themeRepository->upsert([$themeData], $context); |
||
96 | |||
97 | if (empty($themeData['id'])) { |
||
98 | $themeData['id'] = current($writtenEvent->getPrimaryKeys(ThemeDefinition::ENTITY_NAME)); |
||
99 | } |
||
100 | |||
101 | $this->themeRepository->upsert([$themeData], $context); |
||
102 | |||
103 | if (!empty($themeData['toDeleteMedia'])) { |
||
104 | $this->themeMediaRepository->delete($themeData['toDeleteMedia'], $context); |
||
105 | } |
||
106 | |||
107 | $parentThemes = $this->getParentThemes($configuration, $themeData['id']); |
||
108 | $parentCriteria = new Criteria(); |
||
109 | $parentCriteria->addFilter(new EqualsFilter('childId', $themeData['id'])); |
||
110 | /** @var list<array<string, string>> $toDeleteIds */ |
||
111 | $toDeleteIds = $this->themeChildRepository->searchIds($parentCriteria, $context)->getIds(); |
||
112 | $this->themeChildRepository->delete($toDeleteIds, $context); |
||
113 | $this->themeChildRepository->upsert($parentThemes, $context); |
||
114 | } |
||
115 | |||
116 | public function removeTheme(string $technicalName, Context $context): void |
||
117 | { |
||
118 | $criteria = new Criteria(); |
||
119 | $criteria->addAssociation('dependentThemes'); |
||
120 | $criteria->addFilter(new EqualsFilter('technicalName', $technicalName)); |
||
121 | |||
122 | /** @var ThemeEntity|null $theme */ |
||
123 | $theme = $this->themeRepository->search($criteria, $context)->first(); |
||
124 | |||
125 | if ($theme === null) { |
||
126 | return; |
||
127 | } |
||
128 | |||
129 | $dependentThemes = $theme->getDependentThemes() ?? new ThemeCollection(); |
||
130 | $ids = [...array_values($dependentThemes->getIds()), ...[$theme->getId()]]; |
||
131 | |||
132 | $this->removeOldMedia($technicalName, $context); |
||
133 | $this->themeRepository->delete(array_map(fn (string $id) => ['id' => $id], $ids), $context); |
||
134 | } |
||
135 | |||
136 | private function getThemeByTechnicalName(string $technicalName, Context $context): ?ThemeEntity |
||
137 | { |
||
138 | $criteria = new Criteria(); |
||
139 | $criteria->addFilter(new EqualsFilter('technicalName', $technicalName)); |
||
140 | $criteria->addAssociation('previewMedia'); |
||
141 | |||
142 | $theme = $this->themeRepository->search($criteria, $context)->first(); |
||
143 | |||
144 | return $theme instanceof ThemeEntity ? $theme : null; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @return array<string, mixed>|null |
||
149 | */ |
||
150 | private function createMediaStruct(string $path, string $mediaId, ?string $themeFolderId): ?array |
||
151 | { |
||
152 | $path = $this->themeFileImporter->getRealPath($path); |
||
153 | |||
154 | if (!$this->themeFileImporter->fileExists($path)) { |
||
155 | return null; |
||
156 | } |
||
157 | |||
158 | $pathinfo = pathinfo($path); |
||
159 | |||
160 | return [ |
||
161 | 'basename' => $pathinfo['filename'], |
||
162 | 'media' => ['id' => $mediaId, 'mediaFolderId' => $themeFolderId], |
||
163 | 'mediaFile' => new MediaFile( |
||
164 | $path, |
||
165 | (string) MimeType::fromFilename($pathinfo['basename']), |
||
166 | $pathinfo['extension'] ?? '', |
||
167 | (int) filesize($path) |
||
168 | ), |
||
169 | ]; |
||
170 | } |
||
171 | |||
172 | private function getMediaDefaultFolderId(Context $context): ?string |
||
173 | { |
||
174 | $criteria = new Criteria(); |
||
175 | $criteria->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', 'theme')); |
||
176 | $criteria->addAssociation('defaultFolder'); |
||
177 | $criteria->setLimit(1); |
||
178 | $defaultFolder = $this->mediaFolderRepository->search($criteria, $context); |
||
179 | $defaultFolderId = null; |
||
180 | if ($defaultFolder->count() === 1) { |
||
181 | $defaultFolder = $defaultFolder->first(); |
||
182 | |||
183 | if ($defaultFolder instanceof MediaFolderEntity) { |
||
184 | $defaultFolderId = $defaultFolder->getId(); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | return $defaultFolderId; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return array<string, array<string, mixed>> |
||
193 | */ |
||
194 | private function getTranslationsConfiguration(StorefrontPluginConfiguration $configuration, Context $context): array |
||
195 | { |
||
196 | $systemLanguageLocale = $this->getSystemLanguageLocale($context); |
||
197 | |||
198 | $themeConfig = $configuration->getThemeConfig(); |
||
199 | if (!$themeConfig) { |
||
200 | return []; |
||
201 | } |
||
202 | |||
203 | $labelTranslations = $this->getLabelsFromConfig($themeConfig); |
||
204 | $translations = $this->mapTranslations($labelTranslations, 'labels', $systemLanguageLocale); |
||
205 | |||
206 | $helpTextTranslations = $this->getHelpTextsFromConfig($themeConfig); |
||
207 | |||
208 | return array_merge_recursive( |
||
209 | $translations, |
||
210 | $this->mapTranslations($helpTextTranslations, 'helpTexts', $systemLanguageLocale) |
||
211 | ); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param array<string, mixed> $config |
||
216 | * |
||
217 | * @return array<string, array<string, mixed>> |
||
218 | */ |
||
219 | private function getLabelsFromConfig(array $config): array |
||
220 | { |
||
221 | $translations = []; |
||
222 | if (\array_key_exists('blocks', $config)) { |
||
223 | $translations = array_merge_recursive($translations, $this->extractLabels('blocks', $config['blocks'])); |
||
224 | } |
||
225 | |||
226 | if (\array_key_exists('sections', $config)) { |
||
227 | $translations = array_merge_recursive($translations, $this->extractLabels('sections', $config['sections'])); |
||
228 | } |
||
229 | |||
230 | if (\array_key_exists('tabs', $config)) { |
||
231 | $translations = array_merge_recursive($translations, $this->extractLabels('tabs', $config['tabs'])); |
||
232 | } |
||
233 | |||
234 | if (\array_key_exists('fields', $config)) { |
||
235 | $translations = array_merge_recursive($translations, $this->extractLabels('fields', $config['fields'])); |
||
236 | } |
||
237 | |||
238 | return $translations; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param array<string, mixed> $data |
||
243 | * |
||
244 | * @return array<string, array<string, mixed>> |
||
245 | */ |
||
246 | private function extractLabels(string $prefix, array $data): array |
||
247 | { |
||
248 | $labels = []; |
||
249 | foreach ($data as $key => $item) { |
||
250 | if (\array_key_exists('label', $item)) { |
||
251 | /** |
||
252 | * @var string $locale |
||
253 | * @var string $label |
||
254 | */ |
||
255 | foreach ($item['label'] as $locale => $label) { |
||
256 | $labels[$locale][$prefix . '.' . $key] = $label; |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | return $labels; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @param array<string, mixed> $config |
||
266 | * |
||
267 | * @return array<string, array<string, mixed>> |
||
268 | */ |
||
269 | private function getHelpTextsFromConfig(array $config): array |
||
270 | { |
||
271 | $translations = []; |
||
272 | |||
273 | if (\array_key_exists('fields', $config)) { |
||
274 | $translations = array_merge_recursive($translations, $this->extractHelpTexts('fields', $config['fields'])); |
||
275 | } |
||
276 | |||
277 | return $translations; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param array<string, mixed> $data |
||
282 | * |
||
283 | * @return array<string, array<string, mixed>> |
||
284 | */ |
||
285 | private function extractHelpTexts(string $prefix, array $data): array |
||
286 | { |
||
287 | $helpTexts = []; |
||
288 | foreach ($data as $key => $item) { |
||
289 | if (!isset($item['helpText'])) { |
||
290 | continue; |
||
291 | } |
||
292 | |||
293 | /** |
||
294 | * @var string $locale |
||
295 | * @var string $label |
||
296 | */ |
||
297 | foreach ($item['helpText'] as $locale => $label) { |
||
298 | $helpTexts[$locale][$prefix . '.' . $key] = $label; |
||
299 | } |
||
300 | } |
||
301 | |||
302 | return $helpTexts; |
||
303 | } |
||
304 | |||
305 | private function removeOldMedia(string $technicalName, Context $context): void |
||
336 | // don't delete files that are associated with other entities. |
||
337 | // This files will be recreated using the file name strategy for duplicated filenames. |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * @return array<string, mixed> |
||
344 | */ |
||
345 | private function updateMediaInConfiguration( |
||
346 | ?ThemeEntity $theme, |
||
347 | StorefrontPluginConfiguration $pluginConfiguration, |
||
348 | Context $context |
||
349 | ): array { |
||
350 | $media = []; |
||
351 | $themeData = []; |
||
352 | $themeFolderId = $this->getMediaDefaultFolderId($context); |
||
353 | |||
354 | $installedConfiguration = null; |
||
355 | if ($theme && \is_array($theme->getThemeJson()) && $this->pluginConfigurationFactory) { |
||
356 | $installedConfiguration = $this->pluginConfigurationFactory->createFromThemeJson( |
||
357 | $theme->getTechnicalName() ?? 'childTheme', |
||
358 | $theme->getThemeJson(), |
||
359 | $pluginConfiguration->getBasePath(), |
||
360 | false |
||
361 | ); |
||
362 | } |
||
363 | |||
364 | if ( |
||
365 | $pluginConfiguration->getPreviewMedia() |
||
366 | && $pluginConfiguration->getPreviewMedia() !== $installedConfiguration?->getPreviewMedia() |
||
367 | && ( |
||
368 | $theme === null |
||
369 | || $theme->getPreviewMedia() === null |
||
370 | || basename($installedConfiguration?->getPreviewMedia() ?? '') !== $theme->getPreviewMedia()->getFileNameIncludingExtension() |
||
371 | ) |
||
372 | ) { |
||
373 | $mediaId = Uuid::randomHex(); |
||
374 | |||
375 | $path = $pluginConfiguration->getPreviewMedia(); |
||
376 | |||
377 | $mediaItem = $this->createMediaStruct($path, $mediaId, $themeFolderId); |
||
378 | |||
379 | if ($mediaItem) { |
||
380 | $themeData['previewMediaId'] = $mediaId; |
||
381 | $media[$path] = $mediaItem; |
||
382 | } |
||
383 | } |
||
384 | |||
385 | $baseConfig = $pluginConfiguration->getThemeConfig() ?? []; |
||
386 | $installedBaseConfig = $installedConfiguration?->getThemeConfig() ?? []; |
||
387 | |||
388 | $currentThemeMedia = null; |
||
389 | $currentMediaIds = null; |
||
390 | $toDeleteIds = null; |
||
391 | // get existing MediaFiles |
||
392 | if ($theme !== null && \array_key_exists('fields', $theme->getBaseConfig() ?? [])) { |
||
393 | foreach ($theme->getBaseConfig()['fields'] as $key => $field) { |
||
394 | if ($this->hasOldMedia($field) === false) { |
||
395 | continue; |
||
396 | } |
||
397 | $currentMediaIds[$key] = $field['value']; |
||
398 | } |
||
399 | |||
400 | if (!empty($currentMediaIds)) { |
||
401 | $currentThemeMedia = $this->mediaRepository->search(new Criteria($currentMediaIds), $context); |
||
402 | } |
||
403 | } |
||
404 | |||
405 | if (\array_key_exists('fields', $baseConfig)) { |
||
406 | foreach ($baseConfig['fields'] as $key => $field) { |
||
407 | if ($this->hasNewMedia($field) === false) { |
||
408 | continue; |
||
409 | } |
||
410 | |||
411 | if ( |
||
412 | isset($installedBaseConfig['fields'][$key]['value']) |
||
413 | && $field['value'] === $installedBaseConfig['fields'][$key]['value'] |
||
414 | ) { |
||
415 | $baseConfig['fields'][$key]['value'] = $currentMediaIds[$key] ?? $baseConfig['fields'][$key]['value']; |
||
416 | |||
417 | continue; |
||
418 | } |
||
419 | |||
420 | $path = $pluginConfiguration->getBasePath() . \DIRECTORY_SEPARATOR . $field['value']; |
||
421 | |||
422 | if (!\array_key_exists($path, $media)) { |
||
423 | if ( |
||
424 | $currentThemeMedia !== null |
||
425 | && !empty($currentMediaIds) |
||
426 | && isset($currentMediaIds[$key]) |
||
427 | && $currentThemeMedia->getEntities()->get($currentMediaIds[$key])?->getFileNameIncludingExtension() === basename($path)) { |
||
428 | continue; |
||
429 | } |
||
430 | |||
431 | $criteriaMedia = new Criteria(); |
||
432 | $criteriaMedia->addFilter(new EqualsFilter('fileName', basename($path))); |
||
433 | if ($this->mediaRepository->searchIds($criteriaMedia, $context)->getTotal() > 0) { |
||
434 | continue; |
||
435 | } |
||
436 | |||
437 | $mediaId = Uuid::randomHex(); |
||
438 | $mediaItem = $this->createMediaStruct($path, $mediaId, $themeFolderId); |
||
439 | |||
440 | if (!$mediaItem) { |
||
441 | continue; |
||
442 | } |
||
443 | |||
444 | $media[$path] = $mediaItem; |
||
445 | |||
446 | // replace media path with media ids |
||
447 | $baseConfig['fields'][$key]['value'] = $mediaId; |
||
448 | } else { |
||
449 | $baseConfig['fields'][$key]['value'] = $media[$path]['media']['id']; |
||
450 | } |
||
451 | if ($theme && isset($currentMediaIds[$key])) { |
||
452 | $toDeleteIds[] = $currentMediaIds[$key]; |
||
453 | } |
||
454 | } |
||
455 | $themeData['baseConfig'] = $baseConfig; |
||
456 | } |
||
457 | |||
458 | $mediaIds = []; |
||
459 | |||
460 | if (!empty($media)) { |
||
461 | $mediaIds = array_column($media, 'media'); |
||
462 | |||
463 | $this->mediaRepository->create($mediaIds, $context); |
||
464 | |||
465 | foreach ($media as $item) { |
||
466 | if (!$item['mediaFile'] instanceof MediaFile) { |
||
467 | throw MediaException::missingFile($item['media']['id']); |
||
468 | } |
||
469 | |||
470 | try { |
||
471 | $this->fileSaver->persistFileToMedia($item['mediaFile'], $item['basename'], $item['media']['id'], $context); |
||
472 | } catch (MediaException $e) { |
||
473 | if ($e->getErrorCode() !== MediaException::MEDIA_DUPLICATED_FILE_NAME) { |
||
474 | throw $e; |
||
475 | } |
||
476 | |||
477 | $newFileName = $this->fileNameProvider->provide( |
||
478 | $item['basename'], |
||
479 | $item['mediaFile']->getFileExtension(), |
||
480 | null, |
||
481 | $context |
||
482 | ); |
||
483 | $this->fileSaver->persistFileToMedia($item['mediaFile'], $newFileName, $item['media']['id'], $context); |
||
484 | } |
||
485 | } |
||
486 | } |
||
487 | |||
488 | $themeData['media'] = $mediaIds; |
||
489 | |||
490 | if ($theme && \is_array($toDeleteIds)) { |
||
491 | $toDeleteIds = array_unique($toDeleteIds); |
||
492 | foreach ($toDeleteIds as $id) { |
||
493 | if (Uuid::isValid($id)) { |
||
494 | $themeData['toDeleteMedia'][] = [ |
||
495 | 'mediaId' => $id, |
||
496 | 'themeId' => $theme->getId(), |
||
497 | ]; |
||
498 | } |
||
499 | } |
||
500 | } |
||
501 | |||
502 | return $themeData; |
||
503 | } |
||
504 | |||
505 | private function getSystemLanguageLocale(Context $context): string |
||
506 | { |
||
507 | $criteria = new Criteria(); |
||
508 | $criteria->addAssociation('translationCode'); |
||
509 | $criteria->addFilter(new EqualsFilter('id', Defaults::LANGUAGE_SYSTEM)); |
||
510 | |||
511 | /** @var LanguageEntity $language */ |
||
512 | $language = $this->languageRepository->search($criteria, $context)->first(); |
||
513 | /** @var LocaleEntity $locale */ |
||
514 | $locale = $language->getTranslationCode(); |
||
515 | |||
516 | return $locale->getCode(); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * @param array<string, mixed> $translations |
||
521 | * |
||
522 | * @return array<string, array<string, mixed>> |
||
523 | */ |
||
524 | private function mapTranslations(array $translations, string $property, string $systemLanguageLocale): array |
||
525 | { |
||
526 | $result = []; |
||
527 | $containsSystemLanguage = false; |
||
528 | foreach ($translations as $locale => $translation) { |
||
529 | if ($locale === $systemLanguageLocale) { |
||
530 | $containsSystemLanguage = true; |
||
531 | } |
||
532 | $result[$locale] = [$property => $translation]; |
||
533 | } |
||
534 | |||
535 | if (!$containsSystemLanguage && \count($translations) > 0) { |
||
536 | $translation = array_shift($translations); |
||
537 | if (\array_key_exists('en-GB', $translations)) { |
||
538 | $translation = $translations['en-GB']; |
||
539 | } |
||
540 | $result[$systemLanguageLocale] = [$property => $translation]; |
||
541 | } |
||
542 | |||
543 | return $result; |
||
544 | } |
||
545 | |||
546 | /** |
||
547 | * @param array<string, mixed> $themeData |
||
548 | * |
||
549 | * @return array<string, mixed> |
||
550 | */ |
||
551 | private function addParentTheme(StorefrontPluginConfiguration $configuration, array $themeData, Context $context): array |
||
576 | } |
||
577 | |||
578 | /** |
||
579 | * @return list<array{parentId: string, childId: string}> |
||
580 | */ |
||
581 | private function getParentThemes(StorefrontPluginConfiguration $config, string $id): array |
||
609 | } |
||
610 | |||
611 | /** |
||
612 | * @return list<array{technicalName: string, parentThemeId: string}> |
||
613 | */ |
||
614 | private function getAllThemesPlain(): array |
||
615 | { |
||
616 | /** @var list<array{technicalName: string, parentThemeId: string}> $result */ |
||
617 | $result = $this->connection->fetchAllAssociative( |
||
618 | 'SELECT theme.technical_name as technicalName, LOWER(HEX(theme.id)) as parentThemeId FROM theme' |
||
619 | ); |
||
620 | |||
621 | return $result; |
||
622 | } |
||
623 | |||
624 | private function isDependentTheme( |
||
630 | ; |
||
631 | } |
||
632 | |||
633 | /** |
||
634 | * @param array<int|string, mixed> $field |
||
635 | */ |
||
636 | private function hasNewMedia(array $field): bool |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * @param array<int|string, mixed> $field |
||
644 | */ |
||
645 | private function hasOldMedia(array $field): bool |
||
651 |