<?php declare(strict_types=1);
namespace Twigel\SportPlugin;
use Doctrine\DBAL\Exception;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Twigel\SportPlugin\Migration\Migration1661765320TwigelCareerTable;
use Twigel\SportPlugin\Service\CustomFieldsetService;
class TwigelSportPlugin extends Plugin
{
/**
* Install plugin configurations, such as make migration on installation
* @throws Exception
* @param InstallContext $installContext
* @return void
*/
public function install(InstallContext $installContext): void
{
parent::install($installContext);
$this->applyUpdates(
$installContext->getContext(),
null,
$installContext->getCurrentPluginVersion()
);
(new Migration1661765320TwigelCareerTable())->update($this->container->get('Doctrine\DBAL\Connection'));
}
/**
* Uninstall plugin configurations, such as make destructive migration on uninstallation
* @param UninstallContext $uninstallContext
* @return void
* @throws Exception
*/
public function uninstall(UninstallContext $uninstallContext): void
{
parent::uninstall($uninstallContext);
if ($uninstallContext->keepUserData()) {
return;
}
(new Migration1661765320TwigelCareerTable())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
(new CustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$uninstallContext->getContext()
))->remove();
}
private function applyUpdates(Context $context, $oldVersion = null, $newVersion = null): void
{
$versionClosures = [
'1.0.0' => function () use ($context) {
(new CustomFieldsetService(
$this->container->get('custom_field_set.repository'),
$context
))->create();
return true;
}
];
foreach ($versionClosures as $version => $versionClosure) {
if ($oldVersion === null ||
(version_compare($oldVersion, $version, '<')
&& version_compare($version, $newVersion, '<='))
) {
if (!$versionClosure($this)) {
return;
}
}
}
}
}