<?php declare(strict_types=1);
namespace Dvdw\PlatformChoice;
use Doctrine\DBAL\Connection;
use Dvdw\CustomFieldInstaller\System\CustomField\CustomFieldInstaller;
use Dvdw\PlatformChoice\Content\Category\CategoryCustomFields;
use Dvdw\PlatformChoice\System\SalesChannel\SalesChannelCustomFields;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
class DvdwPlatformChoice extends Plugin
{
private ?CustomFieldInstaller $customFieldInstaller = null;
public function postInstall(InstallContext $installContext): void
{
parent::postInstall($installContext);
try {
$this->createCustomFields($installContext->getContext());
} catch (\Throwable $e) {
echo ($e->getMessage()) . PHP_EOL;
}
}
public function uninstall(UninstallContext $uninstallContext): void
{
$uninstallContext->setAutoMigrate(false);
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
$this->deleteCustomFields($uninstallContext->getContext());
/** @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(
CategoryCustomFields::SET_NAME,
CategoryCustomFields::FIELDS,
$context
);
$this->customFieldInstaller->install(
SalesChannelCustomFields::SET_NAME,
SalesChannelCustomFields::FIELDS,
$context
);
}
private function deleteCustomFields(Context $context): void
{
$this->setCustomFieldInstaller();
$this->customFieldInstaller->uninstall(CategoryCustomFields::SET_NAME, $context);
$this->customFieldInstaller->uninstall(SalesChannelCustomFields::SET_NAME, $context);
}
private function setCustomFieldInstaller(): void
{
if (isset($this->customFieldInstaller)) {
return;
}
$this->customFieldInstaller = $this->container->get(
'Dvdw\CustomFieldInstaller\System\CustomField\CustomFieldInstaller'
);
}
}