custom/plugins/MolliePayments/src/MolliePayments.php line 22

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kiener\MolliePayments;
  3. use Exception;
  4. use Kiener\MolliePayments\Compatibility\DependencyLoader;
  5. use Kiener\MolliePayments\Components\Installer\PluginInstaller;
  6. use Kiener\MolliePayments\Repository\CustomFieldSet\CustomFieldSetRepository;
  7. use Kiener\MolliePayments\Service\CustomFieldService;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\Migration\MigrationCollection;
  12. use Shopware\Core\Framework\Plugin;
  13. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  15. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  16. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  17. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. class MolliePayments extends Plugin
  20. {
  21.     const PLUGIN_VERSION '3.6.0';
  22.     /**
  23.      * @param ContainerBuilder $container
  24.      * @throws Exception
  25.      */
  26.     public function build(ContainerBuilder $container): void
  27.     {
  28.         parent::build($container);
  29.         $this->container $container;
  30.         # load the dependencies that are compatible
  31.         # with our current shopware version
  32.         $loader = new DependencyLoader($container);
  33.         $loader->loadServices();
  34.     }
  35.     /**
  36.      * @return void
  37.      */
  38.     public function boot(): void
  39.     {
  40.         parent::boot();
  41.     }
  42.     /**
  43.      * @param InstallContext $context
  44.      * @return void
  45.      */
  46.     public function install(InstallContext $context): void
  47.     {
  48.         parent::install($context);
  49.         /** @var EntityRepository $customFieldRepository */
  50.         $customFieldRepository $this->container->get('custom_field_set.repository');
  51.         // Add custom fields
  52.         $customFieldService = new CustomFieldService(
  53.             new CustomFieldSetRepository(
  54.                 $customFieldRepository
  55.             )
  56.         );
  57.         $customFieldService->addCustomFields($context->getContext());
  58.         $this->runDbMigrations($context->getMigrationCollection());
  59.     }
  60.     /**
  61.      * @param UpdateContext $context
  62.      * @throws \Doctrine\DBAL\Exception
  63.      * @return void
  64.      */
  65.     public function update(UpdateContext $context): void
  66.     {
  67.         parent::update($context);
  68.         if ($context->getPlugin()->isActive() === true) {
  69.             # only prepare our whole plugin
  70.             # if it is indeed active at the moment.
  71.             # otherwise service would not be found of course
  72.             $this->preparePlugin($context->getContext());
  73.             $this->runDbMigrations($context->getMigrationCollection());
  74.         }
  75.     }
  76.     /**
  77.      * @param InstallContext $context
  78.      * @return void
  79.      */
  80.     public function postInstall(InstallContext $context): void
  81.     {
  82.         parent::postInstall($context);
  83.     }
  84.     /**
  85.      * @param UninstallContext $context
  86.      * @return void
  87.      */
  88.     public function uninstall(UninstallContext $context): void
  89.     {
  90.         parent::uninstall($context);
  91.     }
  92.     /**
  93.      * @param ActivateContext $context
  94.      * @throws \Doctrine\DBAL\Exception
  95.      * @return void
  96.      */
  97.     public function activate(ActivateContext $context): void
  98.     {
  99.         parent::activate($context);
  100.         $this->preparePlugin($context->getContext());
  101.         $this->runDbMigrations($context->getMigrationCollection());
  102.     }
  103.     /**
  104.      * @param DeactivateContext $context
  105.      * @return void
  106.      */
  107.     public function deactivate(DeactivateContext $context): void
  108.     {
  109.         parent::deactivate($context);
  110.     }
  111.     /**
  112.      * @param Context $context
  113.      * @throws \Doctrine\DBAL\Exception
  114.      */
  115.     private function preparePlugin(Context $context): void
  116.     {
  117.         /** @var PluginInstaller $pluginInstaller */
  118.         $pluginInstaller $this->container->get(PluginInstaller::class);
  119.         $pluginInstaller->install($context);
  120.     }
  121.     /**
  122.      * @param MigrationCollection $migrationCollection
  123.      * @return void
  124.      */
  125.     private function runDbMigrations(MigrationCollection $migrationCollection): void
  126.     {
  127.         $migrationCollection->migrateInPlace();
  128.     }
  129. }