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

Open in your IDE?
  1. <?php
  2. namespace Sendcloud\Shipping;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\DBALException;
  5. use Sendcloud\Shipping\Entity\Config\ConfigEntityRepository;
  6. use Sendcloud\Shipping\Service\Utility\DatabaseHandler;
  7. use Sendcloud\Shipping\Service\Utility\ShippingMethodHandler;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  10. use Shopware\Core\Framework\Plugin;
  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. /**
  15.  * Class SendcloudShipping
  16.  *
  17.  * @package Sendcloud\Shipping
  18.  */
  19. class SendcloudShipping extends Plugin
  20. {
  21.     /**
  22.      * @param InstallContext $installContext
  23.      *
  24.      * @throws DBALException
  25.      * @throws InconsistentCriteriaIdsException
  26.      */
  27.     public function postInstall(InstallContext $installContext): void
  28.     {
  29.         parent::postInstall($installContext);
  30.         $this->getShippingMethodHandler()->addServicePointShippingMethod();
  31.     }
  32.     /**
  33.      * Plugin uninstall method
  34.      *
  35.      * @param UninstallContext $uninstallContext
  36.      *
  37.      * @throws DBALException
  38.      */
  39.     public function uninstall(UninstallContext $uninstallContext): void
  40.     {
  41.         parent::uninstall($uninstallContext);
  42.         if (!$uninstallContext->keepUserData()) {
  43.             $this->removeAllSendCloudTables();
  44.         }
  45.     }
  46.     /**
  47.      * @param UpdateContext $updateContext
  48.      *
  49.      * @throws DBALException
  50.      * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
  51.      */
  52.     public function update(UpdateContext $updateContext): void
  53.     {
  54.         parent::update($updateContext);
  55.         if (version_compare($updateContext->getCurrentPluginVersion(), '1.1.6''lt')) {
  56.             $this->removeIntegrationConnectTask();
  57.         }
  58.     }
  59.     /**
  60.      * Removes all sendcloud database tables
  61.      *
  62.      * @throws DBALException
  63.      */
  64.     private function removeAllSendCloudTables(): void
  65.     {
  66.         /** @var Connection $connection */
  67.         $connection $this->container->get(Connection::class);
  68.         $databaseHandler = new DatabaseHandler($connection);
  69.         $databaseHandler->removeSendCloudTables();
  70.     }
  71.     /**
  72.      * @throws DBALException
  73.      * @throws \Doctrine\DBAL\Exception\InvalidArgumentException
  74.      */
  75.     private function removeIntegrationConnectTask(): void
  76.     {
  77.         /** @var Connection $connection */
  78.         $connection $this->container->get(Connection::class);
  79.         $databaseHandler = new DatabaseHandler($connection);
  80.         $databaseHandler->removeIntegrationConnectConnectTask();
  81.     }
  82.     /**
  83.      * Returns shipping method handler
  84.      *
  85.      * @return ShippingMethodHandler
  86.      */
  87.     private function getShippingMethodHandler(): ShippingMethodHandler
  88.     {
  89.         /** @var EntityRepositoryInterface $deliveryTimeRepository */
  90.         $deliveryTimeRepository $this->container->get('delivery_time.repository');
  91.         /** @var EntityRepositoryInterface $rulesRepository */
  92.         $rulesRepository $this->container->get('rule.repository');
  93.         /** @var EntityRepositoryInterface $shippingMethodRepository */
  94.         $shippingMethodRepository $this->container->get('shipping_method.repository');
  95.         /** @var Connection $connection */
  96.         $connection $this->container->get(Connection::class);
  97.         $configRepository = new ConfigEntityRepository($connection);
  98.         /** @var EntityRepositoryInterface $systemConfigRepository */
  99.         $systemConfigRepository $this->container->get('system_config.repository');
  100.         return new ShippingMethodHandler(
  101.             $shippingMethodRepository$rulesRepository$deliveryTimeRepository$configRepository$systemConfigRepository
  102.         );
  103.     }
  104. }