themeFilesystemCollection = $themeFilesystemCollection;
$this->themeCollectionFactory = $collectionFactory;
$this->themeRegistration = $registration;
$this->themeResourceModel = $themeResourceModel;
}
/**
* @inheritdoc
*/
public function import(array $data)
{
$messages = ['Theme import was started.'];
try {
// Registers themes from filesystem
$this->themeRegistration->register();
/** @var ThemeDbCollection $collection */
$collection = $this->themeCollectionFactory->create();
// List of themes full paths which are located in filesystem
$themesInFs = $this->themeFilesystemCollection->getAllIds();
/**
* Removes themes if they are not present in configuration files and filesystem
* @var Data $theme
*/
foreach ($collection->getItems() as $theme) {
$themeFullPath = $theme->getFullPath();
if (!key_exists($themeFullPath, $data) && !in_array($themeFullPath, $themesInFs)) {
$this->themeResourceModel->delete($theme);
}
}
} catch (\Exception $exception) {
throw new InvalidTransitionException(__('%1', $exception->getMessage()), $exception);
}
$messages[] = 'Theme import finished.';
return $messages;
}
/**
* Returns array of warning messages which contain information about which changes (removing, registration)
* will be applied to themes.
*
* @param array $data The data that should be imported, used for creating warning messages
* @return array
*/
public function getWarningMessages(array $data)
{
$themesInFile = array_keys($data);
$themesInDb = [];
/** @var ThemeDbCollection $collection */
$collection = $this->themeCollectionFactory->create();
/** @var Data $theme */
foreach ($collection->getItems() as $theme) {
$themesInDb[] = $theme->getFullPath();
}
$toBeRegistered = $this->themeFilesystemCollection->getAllIds();
$toBeRemoved = array_diff($themesInDb, $toBeRegistered, $themesInFile);
$newThemes = array_diff($toBeRegistered, $themesInDb);
$messages = [];
if ($newThemes) {
$messages[] = 'The following themes will be registered: ' . implode(', ', $newThemes);
}
if ($toBeRemoved) {
$messages[] = 'The following themes will be removed: ' . implode(', ', $toBeRemoved);
}
return $messages;
}
}