custom/plugins/nwdxxItRechtConnector6/src/nwdxxItRechtConnector6.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nwdxx\ItRechtConnector6;
  3. use Nwdxx\ItRechtConnector6\Exceptions\InstallException;
  4. use Shopware\Core\Framework\Api\Context\SystemSource;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. class nwdxxItRechtConnector6 extends Plugin
  15. {
  16.     public function install(InstallContext $context): void
  17.     {
  18.         $this->versionComparison($context->getCurrentShopwareVersion());
  19.         parent::install($context);
  20.         $this->createDefaultConfig(
  21.             'nwdxxItRechtConnector6.config.authToken',
  22.             Uuid::randomHex(),
  23.             true
  24.         );
  25.     }
  26.     public function update(UpdateContext $updateContext): void
  27.     {
  28.         $this->versionComparison($updateContext->getCurrentShopwareVersion());
  29.         parent::update($updateContext);
  30.     }
  31.     public function activate(ActivateContext $activateContext): void
  32.     {
  33.         $this->versionComparison($activateContext->getCurrentShopwareVersion());
  34.         parent::activate($activateContext);
  35.     }
  36.     private function versionComparison(string $shopVersion): void
  37.     {
  38.         $comparison version_compare($shopVersion'6.4.0''<');
  39.         if ($comparison)
  40.             throw new InstallException(
  41.                 'Your shop version is not compatible to the current plugin version.'
  42.             );
  43.     }
  44.     private function createDefaultConfig($key$value$renew false): void
  45.     {
  46.         $data = [
  47.             'configurationKey' => $key,
  48.             'configurationValue' => $value
  49.         ];
  50.         $defaultContext = new Context(new SystemSource());
  51.         /** @var EntityRepository $repository */
  52.         $repository $this->container->get('system_config.repository');
  53.         $criteria = new Criteria();
  54.         $criteria->addFilter(
  55.             new EqualsFilter('configurationKey'$key)
  56.         );
  57.         $config $repository->search($criteria$defaultContext);
  58.         if ($config->getTotal() === 0) {
  59.             $repository->create([$data], $defaultContext);
  60.             return;
  61.         }
  62.         if ($config->getTotal() > && $renew === true) {
  63.             $data['id'] = $config->first()->getId();
  64.             $repository->update([$data], $defaultContext);
  65.         }
  66.     }
  67. }