<?php declare(strict_types=1);
namespace Twigel\SportB2B;
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\SportB2B\Migration\Migration1669203562CreateTableForB2B;
use Twigel\SportB2B\Migration\Migration1671716526AdvancedPrice;
use Twigel\SportB2B\Service\CustomFieldService;
use Twigel\SportPlugin\Migration\Migration1661765320TwigelCareerTable;
use Twigel\SportPlugin\Service\CustomFieldsetService;
class TwigelSportB2B 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 Migration1669203562CreateTableForB2B())->update($this->container->get('Doctrine\DBAL\Connection'));
(new Migration1671716526AdvancedPrice())->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 CustomFieldService(
$this->container->get('custom_field_set.repository'),
$uninstallContext->getContext()))->remove();
(new Migration1669203562CreateTableForB2B())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
(new Migration1671716526AdvancedPrice())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
}
private function applyUpdates(Context $context, $oldVersion = null, $newVersion = null): void
{
$versionClosures = [
'1.0.0' => function () use ($context) {
(new CustomFieldService(
$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;
}
}
}
}
}