custom/plugins/MyPaShopware/src/MyPaShopware.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MyPa\Shopware;
  3. use Doctrine\DBAL\Connection;
  4. use MyPa\Shopware\Service\Shopware\CustomField\CustomFieldInstaller;
  5. use MyPa\Shopware\Service\Shopware\ShippingMethod\ShippingMethodCreatorService;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. class MyPaShopware extends Plugin
  15. {
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         parent::install($installContext);
  19.         CustomFieldInstaller::createFactory($this->container)->install($installContext->getContext());
  20.     }
  21.     public function activate(ActivateContext $activateContext): void
  22.     {
  23.         parent::activate($activateContext);
  24.         $shippingMethodCreator $this->container->get(ShippingMethodCreatorService::class);
  25.         $shippingMethodCreator->create($this->getPath(), $activateContext->getContext());
  26.     }
  27.     public function update(UpdateContext $updateContext): void
  28.     {
  29.         parent::update($updateContext);
  30.         $shippingMethodCreator $this->container->get(ShippingMethodCreatorService::class);
  31.         $shippingMethodCreator->create($this->getPath(), $updateContext->getContext());
  32.         CustomFieldInstaller::createFactory($this->container)->install($updateContext->getContext());
  33.     }
  34.     public function uninstall(UninstallContext $uninstallContext): void
  35.     {
  36.         parent::uninstall($uninstallContext);
  37.         if ($uninstallContext->keepUserData()) {
  38.             return;
  39.         }
  40.         $connection $this->container->get(Connection::class);
  41.         $off 0;
  42.         $connection->exec("SET FOREIGN_KEY_CHECKS=$off;");
  43.         $connection->exec('DROP TABLE IF EXISTS `myparcel_shipment`');
  44.         $connection->exec('DROP TABLE IF EXISTS `myparcel_shipping_option`');
  45.         $connection->executeQuery('SET FOREIGN_KEY_CHECKS=1;');
  46.         $this->deleteCustomFields($uninstallContext);
  47.     }
  48.     private function deleteCustomFields(UninstallContext $uninstallContext)
  49.     {
  50.         /** @var EntityRepository $customFieldSetRepository */
  51.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  52.         $entityIds $customFieldSetRepository->search(
  53.             (new Criteria())->addFilter(new EqualsFilter('name''myparcelShopware')),
  54.             $uninstallContext->getContext()
  55.         )->getEntities()->getIds();
  56.         if (count($entityIds) < 1) {
  57.             return;
  58.         }
  59.         $entityIds array_map(function ($element) {
  60.             return ['id' => $element];
  61.         }, array_values($entityIds));
  62.         $customFieldSetRepository->delete(
  63.             $entityIds,
  64.             $uninstallContext->getContext()
  65.         );
  66.     }
  67. }