custom/plugins/LaudertFriendlyCaptcha/src/LaudertFriendlyCaptcha.php line 15

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Laudert\FriendlyCaptcha;
  4. use Doctrine\DBAL\Connection;
  5. use Laudert\FriendlyCaptcha\Config\Manager;
  6. use Laudert\FriendlyCaptcha\Config\ManagerInterface;
  7. use Laudert\FriendlyCaptcha\Exception\MissingActiveCaptchasConfigException;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. class LaudertFriendlyCaptcha extends Plugin
  12. {
  13.     public const CONFIG_KEY 'core.basicInformation.activeCaptchasV2.friendlyCaptcha';
  14.     /**
  15.      * @throws MissingActiveCaptchasConfigException
  16.      * @throws \JsonException
  17.      */
  18.     public function deactivate(DeactivateContext $deactivateContext): void
  19.     {
  20.         // disable friendly captcha on deactivation because shopware would
  21.         // try to load the captcha template and break otherwise
  22.         $this->getManager()->updateConfig($this->getConnection(), [
  23.             'isActive' => false,
  24.         ]);
  25.     }
  26.     protected function getConnection(): Connection
  27.     {
  28.         $connection $this->container->get(Connection::class);
  29.         if (!$connection instanceof Connection) {
  30.             throw new \RuntimeException();
  31.         }
  32.         return $connection;
  33.     }
  34.     protected function getManager(): ManagerInterface
  35.     {
  36.         $manager $this->container->get(ManagerInterface::class);
  37.         if (!$manager instanceof ManagerInterface) {
  38.             throw new \RuntimeException();
  39.         }
  40.         return $manager;
  41.     }
  42.     /**
  43.      * @throws MissingActiveCaptchasConfigException
  44.      * @throws \JsonException
  45.      */
  46.     public function uninstall(UninstallContext $uninstallContext): void
  47.     {
  48.         // since the template loading issue has been fixed in $this->deactivate(),
  49.         // we must only check for data removal here
  50.         if ($uninstallContext->keepUserData()) {
  51.             return;
  52.         }
  53.         // we want to cleanup our data after the plugin was uninstalled
  54.         // this is important to make sure secret keys & other data won't remain in the database
  55.         Manager::create()->updateConfig($this->getConnection(), []);
  56.     }
  57. }