custom/plugins/TwigelSportB2b/src/TwigelSportB2B.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Twigel\SportB2B;
  3. use Doctrine\DBAL\Exception;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\Plugin;
  6. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  7. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  8. use Twigel\SportB2B\Migration\Migration1669203562CreateTableForB2B;
  9. use Twigel\SportB2B\Migration\Migration1671716526AdvancedPrice;
  10. use Twigel\SportB2B\Service\CustomFieldService;
  11. use Twigel\SportPlugin\Migration\Migration1661765320TwigelCareerTable;
  12. use Twigel\SportPlugin\Service\CustomFieldsetService;
  13. class TwigelSportB2B extends Plugin
  14. {
  15.     /**
  16.      * Install plugin configurations, such as make migration on installation
  17.      * @throws Exception
  18.      * @param InstallContext $installContext
  19.      * @return void
  20.      */
  21.     public function install(InstallContext $installContext): void
  22.     {
  23.         parent::install($installContext);
  24.         $this->applyUpdates(
  25.             $installContext->getContext(),
  26.             null,
  27.             $installContext->getCurrentPluginVersion()
  28.         );
  29.         (new Migration1669203562CreateTableForB2B())->update($this->container->get('Doctrine\DBAL\Connection'));
  30.         (new Migration1671716526AdvancedPrice())->update($this->container->get('Doctrine\DBAL\Connection'));
  31.     }
  32.     /**
  33.      * Uninstall plugin configurations, such as make destructive migration on uninstallation
  34.      * @param UninstallContext $uninstallContext
  35.      * @return void
  36.      * @throws Exception
  37.      */
  38.     public function uninstall(UninstallContext $uninstallContext): void
  39.     {
  40.         parent::uninstall($uninstallContext);
  41.         if ($uninstallContext->keepUserData()) {
  42.             return;
  43.         }
  44.         (new CustomFieldService(
  45.             $this->container->get('custom_field_set.repository'),
  46.             $uninstallContext->getContext()))->remove();
  47.         (new Migration1669203562CreateTableForB2B())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
  48.         (new Migration1671716526AdvancedPrice())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
  49.     }
  50.     private function applyUpdates(Context $context$oldVersion null$newVersion null): void
  51.     {
  52.         $versionClosures = [
  53.             '1.0.0' => function () use ($context) {
  54.                 (new CustomFieldService(
  55.                     $this->container->get('custom_field_set.repository'),
  56.                     $context
  57.                 ))->create();
  58.                  return true;
  59.             }
  60.         ];
  61.         foreach ($versionClosures as $version => $versionClosure) {
  62.             if ($oldVersion === null ||
  63.                 (version_compare($oldVersion$version'<')
  64.                     && version_compare($version$newVersion'<='))
  65.             ) {
  66.                 if (!$versionClosure($this)) {
  67.                     return;
  68.                 }
  69.             }
  70.         }
  71.     }
  72. }