| Total Complexity | 56 | 
| Total Lines | 390 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 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);  | 
            ||
| 21 | class ThemeLifecycleService  | 
            ||
| 22 | { | 
            ||
| 23 | /**  | 
            ||
| 24 | * @var StorefrontPluginRegistryInterface  | 
            ||
| 25 | */  | 
            ||
| 26 | private $pluginRegistry;  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * @var EntityRepositoryInterface  | 
            ||
| 30 | */  | 
            ||
| 31 | private $themeRepository;  | 
            ||
| 32 | |||
| 33 | /**  | 
            ||
| 34 | * @var EntityRepositoryInterface  | 
            ||
| 35 | */  | 
            ||
| 36 | private $mediaRepository;  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * @var EntityRepositoryInterface  | 
            ||
| 40 | */  | 
            ||
| 41 | private $mediaFolderRepository;  | 
            ||
| 42 | |||
| 43 | /**  | 
            ||
| 44 | * @var EntityRepositoryInterface  | 
            ||
| 45 | */  | 
            ||
| 46 | private $themeMediaRepository;  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * @var FileSaver  | 
            ||
| 50 | */  | 
            ||
| 51 | private $fileSaver;  | 
            ||
| 52 | |||
| 53 | /**  | 
            ||
| 54 | * @var ThemeFileImporterInterface  | 
            ||
| 55 | */  | 
            ||
| 56 | private $themeFileImporter;  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * @var FileNameProvider  | 
            ||
| 60 | */  | 
            ||
| 61 | private $fileNameProvider;  | 
            ||
| 62 | |||
| 63 | /**  | 
            ||
| 64 | * @var EntityRepositoryInterface  | 
            ||
| 65 | */  | 
            ||
| 66 | private $languageRepository;  | 
            ||
| 67 | |||
| 68 | public function __construct(  | 
            ||
| 69 | StorefrontPluginRegistryInterface $pluginRegistry,  | 
            ||
| 70 | EntityRepositoryInterface $themeRepository,  | 
            ||
| 71 | EntityRepositoryInterface $mediaRepository,  | 
            ||
| 72 | EntityRepositoryInterface $mediaFolderRepository,  | 
            ||
| 73 | EntityRepositoryInterface $themeMediaRepository,  | 
            ||
| 74 | FileSaver $fileSaver,  | 
            ||
| 75 | FileNameProvider $fileNameProvider,  | 
            ||
| 76 | ThemeFileImporterInterface $themeFileImporter,  | 
            ||
| 77 | EntityRepositoryInterface $languageRepository  | 
            ||
| 78 |     ) { | 
            ||
| 79 | $this->pluginRegistry = $pluginRegistry;  | 
            ||
| 80 | $this->themeRepository = $themeRepository;  | 
            ||
| 81 | $this->mediaRepository = $mediaRepository;  | 
            ||
| 82 | $this->mediaFolderRepository = $mediaFolderRepository;  | 
            ||
| 83 | $this->themeMediaRepository = $themeMediaRepository;  | 
            ||
| 84 | $this->fileSaver = $fileSaver;  | 
            ||
| 85 | $this->fileNameProvider = $fileNameProvider;  | 
            ||
| 86 | $this->themeFileImporter = $themeFileImporter;  | 
            ||
| 87 | $this->languageRepository = $languageRepository;  | 
            ||
| 88 | }  | 
            ||
| 89 | |||
| 90 | public function refreshThemes(  | 
            ||
| 101 | }  | 
            ||
| 102 | }  | 
            ||
| 103 | |||
| 104 | public function refreshTheme(StorefrontPluginConfiguration $configuration, Context $context): void  | 
            ||
| 105 |     { | 
            ||
| 106 |         if ($configuration->getTechnicalName() === null) { | 
            ||
| 107 |             throw new \LogicException('Bundle can not exist without technical name'); | 
            ||
| 108 | }  | 
            ||
| 109 | |||
| 110 | $themeData['name'] = $configuration->getName();  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 111 | $themeData['technicalName'] = $configuration->getTechnicalName();  | 
            ||
| 112 | $themeData['author'] = $configuration->getAuthor();  | 
            ||
| 113 | |||
| 114 | $this->removeOldMedia($configuration->getTechnicalName(), $context);  | 
            ||
| 115 | |||
| 116 | // refresh theme after deleting media  | 
            ||
| 117 | $theme = $this->getThemeByTechnicalName($configuration->getTechnicalName(), $context);  | 
            ||
| 118 | |||
| 119 | // check if theme config already exists in the database  | 
            ||
| 120 |         if ($theme) { | 
            ||
| 121 | $themeData['id'] = $theme->getId();  | 
            ||
| 122 |         } else { | 
            ||
| 123 | $themeData['active'] = true;  | 
            ||
| 124 | }  | 
            ||
| 125 | |||
| 126 | $themeData['translations'] = $this->getTranslationsConfiguration($configuration, $context);  | 
            ||
| 127 | |||
| 128 | $updatedData = $this->updateMediaInConfiguration($theme, $configuration, $context);  | 
            ||
| 129 | |||
| 130 | $themeData = array_merge($themeData, $updatedData);  | 
            ||
| 131 | |||
| 132 | $this->themeRepository->upsert([$themeData], $context);  | 
            ||
| 133 | }  | 
            ||
| 134 | |||
| 135 | private function getThemeByTechnicalName(string $technicalName, Context $context): ?ThemeEntity  | 
            ||
| 136 |     { | 
            ||
| 137 | $criteria = new Criteria();  | 
            ||
| 138 |         $criteria->addFilter(new EqualsFilter('technicalName', $technicalName)); | 
            ||
| 139 | |||
| 140 | return $this->themeRepository->search($criteria, $context)->first();  | 
            ||
| 141 | }  | 
            ||
| 142 | |||
| 143 | private function createMediaStruct(string $path, string $mediaId, string $themeFolderId): ?array  | 
            ||
| 144 |     { | 
            ||
| 145 |         if (!$this->fileExists($path)) { | 
            ||
| 146 | return null;  | 
            ||
| 147 | }  | 
            ||
| 148 | |||
| 149 | $path = $this->themeFileImporter->getRealPath($path);  | 
            ||
| 150 | |||
| 151 | $pathinfo = pathinfo($path);  | 
            ||
| 152 | |||
| 153 | return [  | 
            ||
| 154 | 'basename' => $pathinfo['filename'],  | 
            ||
| 155 | 'media' => ['id' => $mediaId, 'mediaFolderId' => $themeFolderId],  | 
            ||
| 156 | 'mediaFile' => new MediaFile(  | 
            ||
| 157 | $path,  | 
            ||
| 158 | mimetype_from_filename($pathinfo['basename']),  | 
            ||
| 159 | $pathinfo['extension'],  | 
            ||
| 160 | filesize($path)  | 
            ||
| 161 | ),  | 
            ||
| 162 | ];  | 
            ||
| 163 | }  | 
            ||
| 164 | |||
| 165 | private function getMediaDefaultFolderId(string $folder, Context $context): ?string  | 
            ||
| 166 |     { | 
            ||
| 167 | $criteria = new Criteria();  | 
            ||
| 168 |         $criteria->addFilter(new EqualsFilter('media_folder.defaultFolder.entity', $folder)); | 
            ||
| 169 |         $criteria->addAssociation('defaultFolder'); | 
            ||
| 170 | $criteria->setLimit(1);  | 
            ||
| 171 | $defaultFolder = $this->mediaFolderRepository->search($criteria, $context);  | 
            ||
| 172 | $defaultFolderId = null;  | 
            ||
| 173 |         if ($defaultFolder->count() === 1) { | 
            ||
| 174 | $defaultFolderId = $defaultFolder->first()->getId();  | 
            ||
| 175 | }  | 
            ||
| 176 | |||
| 177 | return $defaultFolderId;  | 
            ||
| 178 | }  | 
            ||
| 179 | |||
| 180 | private function getTranslationsConfiguration(StorefrontPluginConfiguration $configuration, Context $context): array  | 
            ||
| 181 |     { | 
            ||
| 182 | $systemLanguageLocale = $this->getSystemLanguageLocale($context);  | 
            ||
| 183 | |||
| 184 | $labelTranslations = $this->getLabelsFromConfig($configuration->getThemeConfig());  | 
            ||
| 185 | $translations = $this->mapTranslations($labelTranslations, 'labels', $systemLanguageLocale);  | 
            ||
| 186 | |||
| 187 | $helpTextTranslations = $this->getHelpTextsFromConfig($configuration->getThemeConfig());  | 
            ||
| 188 | |||
| 189 | return array_merge_recursive(  | 
            ||
| 190 | $translations,  | 
            ||
| 191 | $this->mapTranslations($helpTextTranslations, 'helpTexts', $systemLanguageLocale)  | 
            ||
| 192 | );  | 
            ||
| 193 | }  | 
            ||
| 194 | |||
| 195 | private function getLabelsFromConfig(array $config): array  | 
            ||
| 196 |     { | 
            ||
| 197 | $translations = [];  | 
            ||
| 198 |         if (array_key_exists('blocks', $config)) { | 
            ||
| 199 |             $translations = array_merge_recursive($translations, $this->extractLabels('blocks', $config['blocks'])); | 
            ||
| 200 | }  | 
            ||
| 201 | |||
| 202 |         if (array_key_exists('sections', $config)) { | 
            ||
| 203 |             $translations = array_merge_recursive($translations, $this->extractLabels('sections', $config['sections'])); | 
            ||
| 204 | }  | 
            ||
| 205 | |||
| 206 |         if (array_key_exists('tabs', $config)) { | 
            ||
| 207 |             $translations = array_merge_recursive($translations, $this->extractLabels('tabs', $config['tabs'])); | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 |         if (array_key_exists('fields', $config)) { | 
            ||
| 211 |             $translations = array_merge_recursive($translations, $this->extractLabels('fields', $config['fields'])); | 
            ||
| 212 | }  | 
            ||
| 213 | |||
| 214 | return $translations;  | 
            ||
| 215 | }  | 
            ||
| 216 | |||
| 217 | private function extractLabels(string $prefix, array $data): array  | 
            ||
| 218 |     { | 
            ||
| 219 | $labels = [];  | 
            ||
| 220 |         foreach ($data as $key => $item) { | 
            ||
| 221 |             if (array_key_exists('label', $item)) { | 
            ||
| 222 |                 foreach ($item['label'] as $locale => $label) { | 
            ||
| 223 | $labels[$locale][$prefix . '.' . $key] = $label;  | 
            ||
| 224 | }  | 
            ||
| 225 | }  | 
            ||
| 226 | }  | 
            ||
| 227 | |||
| 228 | return $labels;  | 
            ||
| 229 | }  | 
            ||
| 230 | |||
| 231 | private function getHelpTextsFromConfig(array $config): array  | 
            ||
| 232 |     { | 
            ||
| 233 | $translations = [];  | 
            ||
| 234 | |||
| 235 |         if (array_key_exists('fields', $config)) { | 
            ||
| 236 |             $translations = array_merge_recursive($translations, $this->extractHelpTexts('fields', $config['fields'])); | 
            ||
| 237 | }  | 
            ||
| 238 | |||
| 239 | return $translations;  | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | private function extractHelpTexts(string $prefix, array $data): array  | 
            ||
| 243 |     { | 
            ||
| 244 | $helpTexts = [];  | 
            ||
| 245 |         foreach ($data as $key => $item) { | 
            ||
| 246 |             if (!isset($item['helpText'])) { | 
            ||
| 247 | continue;  | 
            ||
| 248 | }  | 
            ||
| 249 | |||
| 250 |             foreach ($item['helpText'] as $locale => $label) { | 
            ||
| 251 | $helpTexts[$locale][$prefix . '.' . $key] = $label;  | 
            ||
| 252 | }  | 
            ||
| 253 | }  | 
            ||
| 254 | |||
| 255 | return $helpTexts;  | 
            ||
| 256 | }  | 
            ||
| 257 | |||
| 258 | private function fileExists(string $path): bool  | 
            ||
| 259 |     { | 
            ||
| 260 | return $this->themeFileImporter->fileExists($path);  | 
            ||
| 261 | }  | 
            ||
| 262 | |||
| 263 | private function removeOldMedia(string $technicalName, Context $context): void  | 
            ||
| 264 |     { | 
            ||
| 265 | $theme = $this->getThemeByTechnicalName($technicalName, $context);  | 
            ||
| 266 | |||
| 267 |         if (!$theme) { | 
            ||
| 268 | return;  | 
            ||
| 269 | }  | 
            ||
| 270 | |||
| 271 | // find all assigned media files  | 
            ||
| 272 | $criteria = new Criteria();  | 
            ||
| 273 |         $criteria->addFilter(new EqualsFilter('media.themeMedia.id', $theme->getId())); | 
            ||
| 274 | $result = $this->mediaRepository->searchIds($criteria, $context);  | 
            ||
| 275 | |||
| 276 | // delete theme media association  | 
            ||
| 277 | $themeMediaData = [];  | 
            ||
| 278 |         foreach ($result->getIds() as $id) { | 
            ||
| 279 | $themeMediaData[] = ['themeId' => $theme->getId(), 'mediaId' => $id];  | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 |         if (empty($themeMediaData)) { | 
            ||
| 283 | return;  | 
            ||
| 284 | }  | 
            ||
| 285 | |||
| 286 | // remove associations between theme and media first  | 
            ||
| 287 | $this->themeMediaRepository->delete($themeMediaData, $context);  | 
            ||
| 288 | |||
| 289 | // delete media associated with theme  | 
            ||
| 290 |         foreach ($themeMediaData as $item) { | 
            ||
| 291 |             try { | 
            ||
| 292 | $this->mediaRepository->delete([['id' => $item['mediaId']]], $context);  | 
            ||
| 293 |             } catch (RestrictDeleteViolationException $e) { | 
            ||
| 294 | // don't delete files that are associated with other entities.  | 
            ||
| 295 | // This files will be recreated using the file name strategy for duplicated filenames.  | 
            ||
| 296 | }  | 
            ||
| 297 | }  | 
            ||
| 298 | }  | 
            ||
| 299 | |||
| 300 | private function updateMediaInConfiguration(?ThemeEntity $theme, StorefrontPluginConfiguration $pluginConfiguration, Context $context): array  | 
            ||
| 377 | }  | 
            ||
| 378 | |||
| 379 | private function getSystemLanguageLocale(Context $context): string  | 
            ||
| 380 |     { | 
            ||
| 381 | $criteria = new Criteria();  | 
            ||
| 382 |         $criteria->addAssociation('translationCode'); | 
            ||
| 383 |         $criteria->addFilter(new EqualsFilter('id', Defaults::LANGUAGE_SYSTEM)); | 
            ||
| 384 | |||
| 385 | /** @var LanguageEntity $language */  | 
            ||
| 386 | $language = $this->languageRepository->search($criteria, $context)->first();  | 
            ||
| 387 | |||
| 388 | return $language->getTranslationCode()->getCode();  | 
            ||
| 389 | }  | 
            ||
| 390 | |||
| 391 | private function mapTranslations(array $translations, string $property, string $systemLanguageLocale): array  | 
            ||
| 411 | }  | 
            ||
| 412 | }  | 
            ||
| 413 |