| Total Complexity | 71 |
| Total Lines | 508 |
| 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); |
||
| 24 | #[Package('storefront')] |
||
| 25 | class ThemeLifecycleService |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @internal |
||
| 29 | */ |
||
| 30 | public function __construct( |
||
| 31 | private readonly StorefrontPluginRegistryInterface $pluginRegistry, |
||
| 32 | private readonly EntityRepository $themeRepository, |
||
| 33 | private readonly EntityRepository $mediaRepository, |
||
| 34 | private readonly EntityRepository $mediaFolderRepository, |
||
| 35 | private readonly EntityRepository $themeMediaRepository, |
||
| 36 | private readonly FileSaver $fileSaver, |
||
| 37 | private readonly FileNameProvider $fileNameProvider, |
||
| 38 | private readonly ThemeFileImporterInterface $themeFileImporter, |
||
| 39 | private readonly EntityRepository $languageRepository, |
||
| 40 | private readonly EntityRepository $themeChildRepository, |
||
| 41 | private readonly Connection $connection |
||
| 42 | ) { |
||
| 43 | } |
||
| 44 | |||
| 45 | public function refreshThemes( |
||
| 46 | Context $context, |
||
| 47 | ?StorefrontPluginConfigurationCollection $configurationCollection = null |
||
| 48 | ): void { |
||
| 49 | if ($configurationCollection === null) { |
||
| 50 | $configurationCollection = $this->pluginRegistry->getConfigurations()->getThemes(); |
||
| 51 | } |
||
| 52 | |||
| 53 | // iterate over all theme configs in the filesystem (plugins/bundles) |
||
| 54 | foreach ($configurationCollection as $config) { |
||
| 55 | $this->refreshTheme($config, $context); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | public function refreshTheme(StorefrontPluginConfiguration $configuration, Context $context): void |
||
| 60 | { |
||
| 61 | $themeData = []; |
||
| 62 | $themeData['name'] = $configuration->getName(); |
||
| 63 | $themeData['technicalName'] = $configuration->getTechnicalName(); |
||
| 64 | $themeData['author'] = $configuration->getAuthor(); |
||
| 65 | |||
| 66 | // refresh theme after deleting media |
||
| 67 | $theme = $this->getThemeByTechnicalName($configuration->getTechnicalName(), $context); |
||
| 68 | |||
| 69 | // check if theme config already exists in the database |
||
| 70 | if ($theme) { |
||
| 71 | $themeData['id'] = $theme->getId(); |
||
| 72 | } else { |
||
| 73 | $themeData['active'] = true; |
||
| 74 | } |
||
| 75 | |||
| 76 | $themeData['translations'] = $this->getTranslationsConfiguration($configuration, $context); |
||
| 77 | |||
| 78 | $updatedData = $this->updateMediaInConfiguration($theme, $configuration, $context); |
||
| 79 | |||
| 80 | $themeData = array_merge($themeData, $updatedData); |
||
| 81 | |||
| 82 | if (!empty($configuration->getConfigInheritance())) { |
||
| 83 | $themeData = $this->addParentTheme($configuration, $themeData, $context); |
||
| 84 | } |
||
| 85 | |||
| 86 | $writtenEvent = $this->themeRepository->upsert([$themeData], $context); |
||
| 87 | |||
| 88 | if (!isset($themeData['id']) || empty($themeData['id'])) { |
||
| 89 | $themeData['id'] = current($writtenEvent->getPrimaryKeys(ThemeDefinition::ENTITY_NAME)); |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->themeRepository->upsert([$themeData], $context); |
||
| 93 | |||
| 94 | $parentThemes = $this->getParentThemes($configuration, $themeData['id']); |
||
| 95 | $parentCriteria = new Criteria(); |
||
| 96 | $parentCriteria->addFilter(new EqualsFilter('childId', $themeData['id'])); |
||
| 97 | /** @var list<array<string, string>> $toDeleteIds */ |
||
| 98 | $toDeleteIds = $this->themeChildRepository->searchIds($parentCriteria, $context)->getIds(); |
||
| 99 | $this->themeChildRepository->delete($toDeleteIds, $context); |
||
| 100 | $this->themeChildRepository->upsert($parentThemes, $context); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function removeTheme(string $technicalName, Context $context): void |
||
| 104 | { |
||
| 105 | $criteria = new Criteria(); |
||
| 106 | $criteria->addAssociation('dependentThemes'); |
||
| 107 | $criteria->addFilter(new EqualsFilter('technicalName', $technicalName)); |
||
| 108 | |||
| 109 | /** @var ThemeEntity|null $theme */ |
||
| 110 | $theme = $this->themeRepository->search($criteria, $context)->first(); |
||
| 111 | |||
| 112 | if ($theme === null) { |
||
| 113 | return; |
||
| 114 | } |
||
| 115 | |||
| 116 | $dependentThemes = $theme->getDependentThemes() ?? new ThemeCollection(); |
||
| 117 | $ids = [...array_values($dependentThemes->getIds()), ...[$theme->getId()]]; |
||
| 118 | |||
| 119 | $this->removeOldMedia($technicalName, $context); |
||
| 120 | $this->themeRepository->delete(array_map(fn (string $id) => ['id' => $id], $ids), $context); |
||
| 121 | } |
||
| 122 | |||
| 123 | private function getThemeByTechnicalName(string $technicalName, Context $context): ?ThemeEntity |
||
| 124 | { |
||
| 125 | $criteria = new Criteria(); |
||
| 126 | $criteria->addFilter(new EqualsFilter('technicalName', $technicalName)); |
||
| 127 | |||
| 128 | return $this->themeRepository->search($criteria, $context)->first(); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @return array<string, mixed>|null |
||
| 133 | */ |
||
| 134 | private function createMediaStruct(string $path, string $mediaId, ?string $themeFolderId): ?array |
||
| 135 | { |
||
| 136 | $path = $this->themeFileImporter->getRealPath($path); |
||
| 137 | |||
| 138 | if (!$this->themeFileImporter->fileExists($path)) { |
||
| 139 | return null; |
||
| 140 | } |
||
| 141 | |||
| 142 | $pathinfo = pathinfo($path); |
||
| 143 | |||
| 144 | return [ |
||
| 145 | 'basename' => $pathinfo['filename'], |
||
| 146 | 'media' => ['id' => $mediaId, 'mediaFolderId' => $themeFolderId], |
||
| 147 | 'mediaFile' => new MediaFile( |
||
| 148 | $path, |
||
| 149 | (string) MimeType::fromFilename($pathinfo['basename']), |
||
| 150 | $pathinfo['extension'] ?? '', |
||
| 151 | (int) filesize($path) |
||
| 152 | ), |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | |||
| 156 | private function getMediaDefaultFolderId(Context $context): ?string |
||
| 157 | { |
||
| 158 | $criteria = new Criteria(); |
||
| 159 | $criteria->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', 'theme')); |
||
| 160 | $criteria->addAssociation('defaultFolder'); |
||
| 161 | $criteria->setLimit(1); |
||
| 162 | $defaultFolder = $this->mediaFolderRepository->search($criteria, $context); |
||
| 163 | $defaultFolderId = null; |
||
| 164 | if ($defaultFolder->count() === 1) { |
||
| 165 | $defaultFolderId = $defaultFolder->first()->getId(); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $defaultFolderId; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @return array<string, array<string, mixed>> |
||
| 173 | */ |
||
| 174 | private function getTranslationsConfiguration(StorefrontPluginConfiguration $configuration, Context $context): array |
||
| 175 | { |
||
| 176 | $systemLanguageLocale = $this->getSystemLanguageLocale($context); |
||
| 177 | |||
| 178 | $themeConfig = $configuration->getThemeConfig(); |
||
| 179 | if (!$themeConfig) { |
||
| 180 | return []; |
||
| 181 | } |
||
| 182 | |||
| 183 | $labelTranslations = $this->getLabelsFromConfig($themeConfig); |
||
| 184 | $translations = $this->mapTranslations($labelTranslations, 'labels', $systemLanguageLocale); |
||
| 185 | |||
| 186 | $helpTextTranslations = $this->getHelpTextsFromConfig($themeConfig); |
||
| 187 | |||
| 188 | return array_merge_recursive( |
||
| 189 | $translations, |
||
| 190 | $this->mapTranslations($helpTextTranslations, 'helpTexts', $systemLanguageLocale) |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param array<string, mixed> $config |
||
| 196 | * |
||
| 197 | * @return array<string, array<string, mixed>> |
||
| 198 | */ |
||
| 199 | private function getLabelsFromConfig(array $config): array |
||
| 200 | { |
||
| 201 | $translations = []; |
||
| 202 | if (\array_key_exists('blocks', $config)) { |
||
| 203 | $translations = array_merge_recursive($translations, $this->extractLabels('blocks', $config['blocks'])); |
||
| 204 | } |
||
| 205 | |||
| 206 | if (\array_key_exists('sections', $config)) { |
||
| 207 | $translations = array_merge_recursive($translations, $this->extractLabels('sections', $config['sections'])); |
||
| 208 | } |
||
| 209 | |||
| 210 | if (\array_key_exists('tabs', $config)) { |
||
| 211 | $translations = array_merge_recursive($translations, $this->extractLabels('tabs', $config['tabs'])); |
||
| 212 | } |
||
| 213 | |||
| 214 | if (\array_key_exists('fields', $config)) { |
||
| 215 | $translations = array_merge_recursive($translations, $this->extractLabels('fields', $config['fields'])); |
||
| 216 | } |
||
| 217 | |||
| 218 | return $translations; |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * @param array<string, mixed> $data |
||
| 223 | * |
||
| 224 | * @return array<string, array<string, mixed>> |
||
| 225 | */ |
||
| 226 | private function extractLabels(string $prefix, array $data): array |
||
| 227 | { |
||
| 228 | $labels = []; |
||
| 229 | foreach ($data as $key => $item) { |
||
| 230 | if (\array_key_exists('label', $item)) { |
||
| 231 | /** |
||
| 232 | * @var string $locale |
||
| 233 | * @var string $label |
||
| 234 | */ |
||
| 235 | foreach ($item['label'] as $locale => $label) { |
||
| 236 | $labels[$locale][$prefix . '.' . $key] = $label; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | return $labels; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param array<string, mixed> $config |
||
| 246 | * |
||
| 247 | * @return array<string, array<string, mixed>> |
||
| 248 | */ |
||
| 249 | private function getHelpTextsFromConfig(array $config): array |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * @param array<string, mixed> $data |
||
| 262 | * |
||
| 263 | * @return array<string, array<string, mixed>> |
||
| 264 | */ |
||
| 265 | private function extractHelpTexts(string $prefix, array $data): array |
||
| 283 | } |
||
| 284 | |||
| 285 | private function removeOldMedia(string $technicalName, Context $context): void |
||
| 286 | { |
||
| 287 | $theme = $this->getThemeByTechnicalName($technicalName, $context); |
||
| 288 | |||
| 289 | if (!$theme) { |
||
| 290 | return; |
||
| 291 | } |
||
| 292 | |||
| 293 | // find all assigned media files |
||
| 294 | $criteria = new Criteria(); |
||
| 295 | $criteria->addFilter(new EqualsFilter('media.themeMedia.id', $theme->getId())); |
||
| 296 | $result = $this->mediaRepository->searchIds($criteria, $context); |
||
| 297 | |||
| 298 | // delete theme media association |
||
| 299 | $themeMediaData = []; |
||
| 300 | foreach ($result->getIds() as $id) { |
||
| 301 | $themeMediaData[] = ['themeId' => $theme->getId(), 'mediaId' => $id]; |
||
| 302 | } |
||
| 303 | |||
| 304 | if (empty($themeMediaData)) { |
||
| 305 | return; |
||
| 306 | } |
||
| 307 | |||
| 308 | // remove associations between theme and media first |
||
| 309 | $this->themeMediaRepository->delete($themeMediaData, $context); |
||
| 310 | |||
| 311 | // delete media associated with theme |
||
| 312 | foreach ($themeMediaData as $item) { |
||
| 313 | try { |
||
| 314 | $this->mediaRepository->delete([['id' => $item['mediaId']]], $context); |
||
| 315 | } catch (RestrictDeleteViolationException) { |
||
| 316 | // don't delete files that are associated with other entities. |
||
| 317 | // This files will be recreated using the file name strategy for duplicated filenames. |
||
| 318 | } |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @return array<string, mixed> |
||
| 324 | */ |
||
| 325 | private function updateMediaInConfiguration( |
||
| 405 | } |
||
| 406 | |||
| 407 | private function getSystemLanguageLocale(Context $context): string |
||
| 408 | { |
||
| 409 | $criteria = new Criteria(); |
||
| 410 | $criteria->addAssociation('translationCode'); |
||
| 411 | $criteria->addFilter(new EqualsFilter('id', Defaults::LANGUAGE_SYSTEM)); |
||
| 412 | |||
| 413 | /** @var LanguageEntity $language */ |
||
| 414 | $language = $this->languageRepository->search($criteria, $context)->first(); |
||
| 415 | /** @var LocaleEntity $locale */ |
||
| 416 | $locale = $language->getTranslationCode(); |
||
| 417 | |||
| 418 | return $locale->getCode(); |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param array<string, mixed> $translations |
||
| 423 | * |
||
| 424 | * @return array<string, array<string, mixed>> |
||
| 425 | */ |
||
| 426 | private function mapTranslations(array $translations, string $property, string $systemLanguageLocale): array |
||
| 427 | { |
||
| 428 | $result = []; |
||
| 429 | $containsSystemLanguage = false; |
||
| 430 | foreach ($translations as $locale => $translation) { |
||
| 431 | if ($locale === $systemLanguageLocale) { |
||
| 432 | $containsSystemLanguage = true; |
||
| 433 | } |
||
| 434 | $result[$locale] = [$property => $translation]; |
||
| 435 | } |
||
| 436 | |||
| 437 | if (!$containsSystemLanguage && \count($translations) > 0) { |
||
| 438 | $translation = array_shift($translations); |
||
| 439 | if (\array_key_exists('en-GB', $translations)) { |
||
| 440 | $translation = $translations['en-GB']; |
||
| 441 | } |
||
| 442 | $result[$systemLanguageLocale] = [$property => $translation]; |
||
| 443 | } |
||
| 444 | |||
| 445 | return $result; |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param array<string, mixed> $themeData |
||
| 450 | * |
||
| 451 | * @return array<string, mixed> |
||
| 452 | */ |
||
| 453 | private function addParentTheme(StorefrontPluginConfiguration $configuration, array $themeData, Context $context): array |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @return list<array{parentId: string, childId: string}> |
||
| 482 | */ |
||
| 483 | private function getParentThemes(StorefrontPluginConfiguration $config, string $id): array |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return list<array{technicalName: string, parentThemeId: string}> |
||
| 515 | */ |
||
| 516 | private function getAllThemesPlain(): array |
||
| 517 | { |
||
| 518 | /** @var list<array{technicalName: string, parentThemeId: string}> $result */ |
||
| 519 | $result = $this->connection->fetchAllAssociative( |
||
| 520 | 'SELECT theme.technical_name as technicalName, LOWER(HEX(theme.id)) as parentThemeId FROM theme' |
||
| 521 | ); |
||
| 522 | |||
| 523 | return $result; |
||
| 524 | } |
||
| 525 | |||
| 526 | private function isDependentTheme( |
||
| 532 | ; |
||
| 533 | } |
||
| 535 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths