custom/plugins/TwigelSportBages/src/TwigelSportBadges.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Twigel\SportBadges;
  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\SportBadges\Migration\Migration1669810672TwigelCreateBadgesTable;
  9. class TwigelSportBadges extends Plugin
  10. {
  11.     /**
  12.      * @throws Exception
  13.      * @param InstallContext $installContext
  14.      * @return void
  15.      */
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         $this->applyUpdates(
  20.             $installContext->getContext(),
  21.             null,
  22.             $installContext->getCurrentPluginVersion()
  23.         );
  24.         (new Migration1669810672TwigelCreateBadgesTable())->update($this->container->get('Doctrine\DBAL\Connection'));
  25.     }
  26.     /**
  27.      * @param UninstallContext $uninstallContext
  28.      * @return void
  29.      * @throws Exception
  30.      */
  31.     public function uninstall(UninstallContext $uninstallContext): void
  32.     {
  33.         parent::uninstall($uninstallContext);
  34.         if ($uninstallContext->keepUserData()) {
  35.             return;
  36.         }
  37.         (new Migration1669810672TwigelCreateBadgesTable())->updateDestructive($this->container->get('Doctrine\DBAL\Connection'));
  38.     }
  39.     private function applyUpdates(Context $context$oldVersion null$newVersion null): void
  40.     {
  41.         $versionClosures = [
  42.             '1.0.0' => function () use ($context) {
  43.                 return true;
  44.             }
  45.         ];
  46.         foreach ($versionClosures as $version => $versionClosure) {
  47.             if ($oldVersion === null ||
  48.                 (version_compare($oldVersion$version'<')
  49.                     && version_compare($version$newVersion'<='))
  50.             ) {
  51.                 if (!$versionClosure($this)) {
  52.                     return;
  53.                 }
  54.             }
  55.         }
  56.     }
  57. }