custom/plugins/CrswCleverReachOfficial/src/CrswCleverReachOfficial.php line 31

Open in your IDE?
  1. <?php
  2. namespace Crsw\CleverReachOfficial;
  3. use Crsw\CleverReachOfficial\Components\Utility\Bootstrap;
  4. use Crsw\CleverReachOfficial\Components\Utility\DatabaseHandler;
  5. use Crsw\CleverReachOfficial\Core\BusinessLogic\Authorization\Http\TokenProxy;
  6. use Crsw\CleverReachOfficial\Core\BusinessLogic\DynamicContent\Contracts\DynamicContentService;
  7. use Crsw\CleverReachOfficial\Core\BusinessLogic\DynamicContent\Http\Proxy as DynamicContentProxy;
  8. use Crsw\CleverReachOfficial\Core\BusinessLogic\Form\FormEventsService;
  9. use Crsw\CleverReachOfficial\Core\BusinessLogic\Group\Contracts\GroupService;
  10. use Crsw\CleverReachOfficial\Core\BusinessLogic\Receiver\ReceiverEventsService;
  11. use Crsw\CleverReachOfficial\Core\BusinessLogic\WebHookEvent\Http\Proxy;
  12. use Crsw\CleverReachOfficial\Core\Infrastructure\Logger\Interfaces\ShopLoggerAdapter;
  13. use Crsw\CleverReachOfficial\Core\Infrastructure\Logger\Logger;
  14. use Crsw\CleverReachOfficial\Core\Infrastructure\ServiceRegister;
  15. use Crsw\CleverReachOfficial\Service\BusinessLogic\Uninstall\UninstallService;
  16. use Crsw\CleverReachOfficial\Service\Infrastructure\LoggerService;
  17. use Doctrine\DBAL\Connection;
  18. use Doctrine\DBAL\DBALException;
  19. use Shopware\Core\Framework\Plugin;
  20. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  21. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  22. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  23. /**
  24.  * Class CleverReach
  25.  *
  26.  * @package Crsw\CleverReachOfficial
  27.  */
  28. class CrswCleverReachOfficial extends Plugin
  29. {
  30.     /**
  31.      * @inheritdoc
  32.      * @param InstallContext $installContext
  33.      */
  34.     public function install(InstallContext $installContext): void
  35.     {
  36.         Bootstrap::init();
  37.         $this->registerServices();
  38.         parent::install($installContext);
  39.     }
  40.     /**
  41.      * @inheritdoc
  42.      * @param UpdateContext $context
  43.      */
  44.     public function update(UpdateContext $context): void
  45.     {
  46.         Bootstrap::init();
  47.         $this->registerServices();
  48.         parent::update($context);
  49.     }
  50.     /**
  51.      * Plugin uninstall method.
  52.      *
  53.      * @param UninstallContext $uninstallContext
  54.      */
  55.     public function uninstall(UninstallContext $uninstallContext): void
  56.     {
  57.         Bootstrap::init();
  58.         $this->registerServices();
  59.         parent::uninstall($uninstallContext);
  60.         $this->getUninstallService()->removeData();
  61.         if (!$uninstallContext->keepUserData()) {
  62.             $this->removeTable();
  63.         }
  64.     }
  65.     /**
  66.      * Removes CleverReach table.
  67.      */
  68.     private function removeTable(): void
  69.     {
  70.         try {
  71.             /** @var Connection $connection */
  72.             $connection $this->container->get(Connection::class);
  73.             $databaseHandler = new DatabaseHandler($connection);
  74.             $databaseHandler->removeCleverReachTables();
  75.         } catch (DBALException $e) {
  76.             Logger::logError($e->getMessage());
  77.         }
  78.     }
  79.     /**
  80.      * @return UninstallService
  81.      *
  82.      * @noinspection PhpParamsInspection
  83.      */
  84.     private function getUninstallService(): UninstallService
  85.     {
  86.         /** @var Connection $connection */
  87.         $connection $this->container->get(Connection::class);
  88.         $databaseHandler = new DatabaseHandler($connection);
  89.         return new UninstallService(
  90.             ServiceRegister::getService(GroupService::class),
  91.             ServiceRegister::getService(FormEventsService::class),
  92.             ServiceRegister::getService(ReceiverEventsService::class),
  93.             ServiceRegister::getService(Proxy::class),
  94.             ServiceRegister::getService(DynamicContentService::class),
  95.             ServiceRegister::getService(DynamicContentProxy::class),
  96.             ServiceRegister::getService(TokenProxy::class),
  97.             $databaseHandler
  98.         );
  99.     }
  100.     private function registerServices(): void
  101.     {
  102.         ServiceRegister::registerService(Connection::class, function () {
  103.             return $this->container->get(Connection::class);
  104.         });
  105.         ServiceRegister::registerService(ShopLoggerAdapter::class, function () {
  106.             return new LoggerService($this->container->get('kernel'));
  107.         });
  108.     }
  109. }