custom/plugins/SwagCustomizedProducts/src/SwagCustomizedProducts.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CustomizedProducts;
  8. use Doctrine\DBAL\Connection;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  12. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  15. use Swag\CustomizedProducts\Util\Lifecycle\Uninstaller;
  16. class SwagCustomizedProducts extends Plugin
  17. {
  18.     public const CURRENT_API_VERSION 3;
  19.     private const SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_READ_PRIVILEGE 'swag_customized_products_template:read';
  20.     public function activate(ActivateContext $activateContext): void
  21.     {
  22.         parent::activate($activateContext);
  23.         $this->addCustomPrivileges();
  24.     }
  25.     public function deactivate(DeactivateContext $deactivateContext): void
  26.     {
  27.         parent::deactivate($deactivateContext);
  28.         $this->removeCustomPrivileges();
  29.     }
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         if ($uninstallContext->keepUserData()) {
  33.             return;
  34.         }
  35.         /** @var EntityRepositoryInterface $mediaFolderRepository */
  36.         $mediaFolderRepository $this->container->get('media_folder.repository');
  37.         /** @var EntityRepositoryInterface $mediaRepository */
  38.         $mediaRepository $this->container->get('media.repository');
  39.         /** @var EntityRepositoryInterface $mediaDefaultFolderRepository */
  40.         $mediaDefaultFolderRepository $this->container->get('media_default_folder.repository');
  41.         /** @var EntityRepositoryInterface $mediaFolderConfigRepository */
  42.         $mediaFolderConfigRepository $this->container->get('media_folder_configuration.repository');
  43.         /** @var Connection $connection */
  44.         $connection $this->container->get(Connection::class);
  45.         $uninstaller = new Uninstaller(
  46.             $mediaFolderRepository,
  47.             $mediaRepository,
  48.             $mediaDefaultFolderRepository,
  49.             $mediaFolderConfigRepository,
  50.             $connection
  51.         );
  52.         $uninstaller->uninstall($uninstallContext->getContext());
  53.     }
  54.     public function update(UpdateContext $updateContext): void
  55.     {
  56.         parent::update($updateContext);
  57.         $this->addCustomPrivileges();
  58.     }
  59.     public function enrichPrivileges(): array
  60.     {
  61.         return [
  62.             'product.viewer' => [
  63.                 self::SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_READ_PRIVILEGE,
  64.             ],
  65.         ];
  66.     }
  67.     private function addCustomPrivileges(): void
  68.     {
  69.         // If either the old behaviour does not exists or the new one already does return
  70.         if (!\method_exists($this'addPrivileges') || !\method_exists(parent::class, 'enrichPrivileges')) {
  71.             return;
  72.         }
  73.         $this->addPrivileges(
  74.             'product.viewer',
  75.             [
  76.                 self::SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_READ_PRIVILEGE,
  77.             ]
  78.         );
  79.     }
  80.     private function removeCustomPrivileges(): void
  81.     {
  82.         if (!\method_exists($this'removePrivileges')) {
  83.             return;
  84.         }
  85.         $this->removePrivileges([
  86.             self::SWAG_CUSTOMIZED_PRODUCTS_TEMPLATE_READ_PRIVILEGE,
  87.         ]);
  88.     }
  89. }