<?php declare(strict_types=1);
namespace Dvdw\Events;
use Doctrine\DBAL\Connection;
use Dvdw\CustomFieldInstaller\System\CustomField\CustomFieldInstaller;
use Dvdw\Events\Content\Media\DvdwMediaService;
use Dvdw\Events\Content\Product\ProductCustomFields;
use Shopware\Core\Content\Media\Aggregate\MediaFolderConfiguration\MediaFolderConfigurationDefinition;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Dbal\Common\RepositoryIterator;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class DvdwEvents extends Plugin
{
private ?CustomFieldInstaller $customFieldInstaller = null;
//Need to use interfaces because of decorators
private ?EntityRepositoryInterface $mediaRepo = null;
private ?EntityRepositoryInterface $mediaFolderRepo = null;
private ?EntityRepositoryInterface $thumbSizeRepo = null;
public function postInstall(InstallContext $installContext): void
{
parent::postInstall($installContext);
$context = $installContext->getContext();
try {
$this->createCustomFields($context);
$this->createMediaFolder($context);
} catch (\Throwable $e) {
echo ($e->getMessage()) . PHP_EOL;
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
$uninstallContext->setAutoMigrate(false);
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$context = $uninstallContext->getContext();
$this->deleteCustomFields($context);
$this->deleteMediaFolderAndContents($context);
/** @var Connection $connection */
$connection = $this->container->get('Doctrine\DBAL\Connection');
foreach ($uninstallContext->getMigrationCollection()->getMigrationSteps() as $migrationStep) {
$migrationStep->updateDestructive($connection);
}
$this->removeMigrations();
}
private function createCustomFields(Context $context): void
{
$this->setCustomFieldInstaller();
$this->customFieldInstaller->install(
ProductCustomFields::SET_NAME,
ProductCustomFields::FIELDS,
$context
);
}
private function deleteCustomFields(Context $context): void
{
$this->setCustomFieldInstaller();
$this->customFieldInstaller->uninstall(ProductCustomFields::SET_NAME, $context);
}
private function setCustomFieldInstaller(): void
{
if (isset($this->customFieldInstaller)) {
return;
}
$this->customFieldInstaller = $this->container->get(
'Dvdw\CustomFieldInstaller\System\CustomField\CustomFieldInstaller'
);
}
private function createMediaFolder(Context $context): void
{
$this->setMediaRepos();
if ($this->getParentMediaFolderId($context) !== null) {
return;
}
$thumbIds = $this->thumbSizeRepo->searchIds(new Criteria(), $context)->getIds();
$thumbIds = array_map(
static function(string $id) {
return ['id' => $id];
}
, $thumbIds);
$this->mediaFolderRepo->create([
[
'name' => DvdwMediaService::PARENT_FOLDER_NAME,
'configuration' => (new MediaFolderConfigurationDefinition())->getDefaults(),
'mediaThumbnailSizes' => $thumbIds,
'useParentConfiguration' => false
]
], $context);
}
private function deleteMediaFolderAndContents(Context $context): void
{
$this->setMediaRepos();
$criteria = (new Criteria())->addFilter(new EqualsFilter('mediaFolder.name', DvdwMediaService::PARENT_FOLDER_NAME));
$criteria->setLimit(250);
$iterator = new RepositoryIterator($this->mediaRepo, $context, $criteria);
while (($ids = $iterator->fetchIds()) !== null) {
$toDelete = array_map(
static function(string $id) {
return ['id' => $id];
}
, $ids);
$this->mediaRepo->delete($toDelete, $context);
}
$folderId = $this->getParentMediaFolderId($context);
if ($folderId === null) {
return;
}
$this->mediaFolderRepo->delete([['id' => $folderId]], $context);
}
private function setMediaRepos(): void
{
if (!isset($this->mediaRepo)) {
$this->mediaRepo = $this->container->get('media.repository');
}
if (!isset($this->mediaFolderRepo)) {
$this->mediaFolderRepo = $this->container->get('media_folder.repository');
}
if (!isset($this->thumbSizeRepo)) {
$this->thumbSizeRepo = $this->container->get('media_thumbnail_size.repository');
}
}
private function getParentMediaFolderId(Context $context): ?string
{
$this->setMediaRepos();
$criteria = (new Criteria())->addFilter(new EqualsFilter('name', DvdwMediaService::PARENT_FOLDER_NAME));
$criteria->setLimit(1);
return $this->mediaFolderRepo->searchIds($criteria, $context)->firstId();
}
}