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

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\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\Plugin;
  21. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  22. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  23. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  24. /**
  25.  * Class CleverReach
  26.  *
  27.  * @package Crsw\CleverReachOfficial
  28.  */
  29. class CrswCleverReachOfficial extends Plugin
  30. {
  31.     /**
  32.      * @inheritdoc
  33.      * @param InstallContext $installContext
  34.      */
  35.     public function install(InstallContext $installContext): void
  36.     {
  37.         Bootstrap::init();
  38.         $this->registerServices();
  39.         parent::install($installContext);
  40.     }
  41.     /**
  42.      * @inheritdoc
  43.      * @param UpdateContext $context
  44.      */
  45.     public function update(UpdateContext $context): void
  46.     {
  47.         Bootstrap::init();
  48.         $this->registerServices();
  49.         ServiceRegister::registerService(
  50.             EntityRepositoryInterface::class,
  51.             function () {
  52.                 return $this->container->get('cleverreach_entity.repository');
  53.             }
  54.         );
  55.         parent::update($context);
  56.     }
  57.     /**
  58.      * Plugin uninstall method.
  59.      *
  60.      * @param UninstallContext $uninstallContext
  61.      */
  62.     public function uninstall(UninstallContext $uninstallContext): void
  63.     {
  64.         Bootstrap::init();
  65.         $this->registerServices();
  66.         parent::uninstall($uninstallContext);
  67.         $this->getUninstallService()->removeData();
  68.         if (!$uninstallContext->keepUserData()) {
  69.             $this->removeTable();
  70.         }
  71.     }
  72.     /**
  73.      * Removes CleverReach table.
  74.      */
  75.     private function removeTable(): void
  76.     {
  77.         try {
  78.             /** @var Connection $connection */
  79.             $connection $this->container->get(Connection::class);
  80.             $databaseHandler = new DatabaseHandler($connection);
  81.             $databaseHandler->removeCleverReachTables();
  82.         } catch (DBALException $e) {
  83.             Logger::logError($e->getMessage());
  84.         }
  85.     }
  86.     /**
  87.      * @return UninstallService
  88.      *
  89.      * @noinspection PhpParamsInspection
  90.      */
  91.     private function getUninstallService(): UninstallService
  92.     {
  93.         /** @var Connection $connection */
  94.         $connection $this->container->get(Connection::class);
  95.         $databaseHandler = new DatabaseHandler($connection);
  96.         return new UninstallService(
  97.             ServiceRegister::getService(GroupService::class),
  98.             ServiceRegister::getService(FormEventsService::class),
  99.             ServiceRegister::getService(ReceiverEventsService::class),
  100.             ServiceRegister::getService(Proxy::class),
  101.             ServiceRegister::getService(DynamicContentService::class),
  102.             ServiceRegister::getService(DynamicContentProxy::class),
  103.             ServiceRegister::getService(TokenProxy::class),
  104.             $databaseHandler
  105.         );
  106.     }
  107.     private function registerServices(): void
  108.     {
  109.         ServiceRegister::registerService(Connection::class, function () {
  110.             return $this->container->get(Connection::class);
  111.         });
  112.         ServiceRegister::registerService(ShopLoggerAdapter::class, function () {
  113.             return new LoggerService($this->container->get('kernel'));
  114.         });
  115.     }
  116. }