custom/plugins/MagmodulesWebshopnl/src/MagmodulesWebshopnl.php line 18

  1. <?php
  2. declare(strict_types=1);
  3. namespace MagmodulesWebshopnl;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. use Shopware\Core\Framework\Plugin\Util\PluginIdProvider;
  14. use Shopware\Core\Framework\Uuid\Uuid;
  15. class MagmodulesWebshopnl extends Plugin
  16. {
  17.     const SHIPPING_METHOD_NAME 'webshopnl';
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         parent::install($installContext);
  21.         $this->addConfigurationToken();
  22.         $this->addPaymentMethod($installContext->getContext());
  23.         $this->addShippingMethod($installContext->getContext());
  24.     }
  25.     /**
  26.      * @param  UninstallContext  $uninstallContext
  27.      *
  28.      * @return void
  29.      */
  30.     public function uninstall(UninstallContext $uninstallContext): void
  31.     {
  32.         /* Keep UserData? Then do nothing here */
  33.         if ($uninstallContext->keepUserData()) {
  34.             return;
  35.         }
  36.         /**
  37.          * @var Connection $connection
  38.          */
  39.         $connection $this->container->get(Connection::class);
  40.         $connection->executeStatement(
  41.             'DELETE FROM system_config WHERE configuration_key LIKE :domain',
  42.             [
  43.                 'domain' => '%MagmodulesWebshopnl.settings%',
  44.             ]
  45.         );
  46.         $connection->executeStatement('DROP TABLE IF EXISTS `magmodules_wevshopnl_product_feed`');
  47.         $connection->executeStatement('DROP TABLE IF EXISTS `magmodules_webshopnl_order`');
  48.     }
  49.     public function addShippingMethod(Context $context)
  50.     {
  51.         /**
  52.          * @var EntityRepository $shippingMethodRepository
  53.          */
  54.         $shippingMethodRepository $this->container->get('shipping_method.repository');
  55.         $shippingMethodRepository->upsert([
  56.             $this->createDataPayload($shippingMethodRepository$context),
  57.         ], $context);
  58.     }
  59.     private function addRule($rulesRepositoryContext $context): string
  60.     {
  61.         $ruleIdInsert Uuid::randomHex();
  62.         $data = [
  63.             'id' => $ruleIdInsert,
  64.             'name' => 'All customers',
  65.             'priority' => 1,
  66.             'invalid' => false,
  67.         ];
  68.         $rulesRepository->upsert([$data], $context);
  69.         return $ruleIdInsert;
  70.     }
  71.     public function createDataPayload($shippingMethodRepository$context): array
  72.     {
  73.         $rulesRepository $this->container->get('rule.repository');
  74.         $ruleId $rulesRepository->searchIds(
  75.             (new Criteria())->addFilter(new EqualsFilter('name''All customers')),
  76.             $context
  77.         )->firstId();
  78.         if (!$ruleId) {
  79.             $ruleId $rulesRepository->searchIds(
  80.                 (new Criteria())->setLimit(1),
  81.                 $context
  82.             )->firstId();
  83.         }
  84.         if ($ruleId === null) {
  85.             $ruleId $this->addRule($rulesRepository$context);
  86.         }
  87.         $deliveryTimeRepository $this->container->get('delivery_time.repository');
  88.         $deliveryTimeEntityId $deliveryTimeRepository->searchIds(
  89.             (new Criteria())->setLimit(1),
  90.             $context
  91.         )->firstId();
  92.         if ($deliveryTimeEntityId === null) {
  93.             $deliveryTimeEntityId $this->addDeliveryTime($deliveryTimeRepository$context);
  94.         }
  95.         $id $this->getExistingShippingMethodId($shippingMethodRepository$context);
  96.         return [
  97.             'id' => $id ?? Uuid::randomHex(),
  98.             'translations' => [
  99.                 'en-GB' => [
  100.                     'name' => self::SHIPPING_METHOD_NAME,
  101.                 ],
  102.                 'nl-NL' => [
  103.                     'name' => self::SHIPPING_METHOD_NAME,
  104.                 ],
  105.                 'de-DE' => [
  106.                     'name' => self::SHIPPING_METHOD_NAME,
  107.                 ],
  108.             ],
  109.             'active' => false,
  110.             'availabilityRuleId' => $ruleId,
  111.             'deliveryTimeId' => $deliveryTimeEntityId,
  112.             'prices' => [
  113.                 [
  114.                     'price' => 0,
  115.                     'currencyId' => Defaults::CURRENCY,
  116.                     'calculation' => 0,
  117.                     'quantityStart' => 0,
  118.                     'currencyPrice' => [
  119.                         [
  120.                             'currencyId' => Defaults::CURRENCY,
  121.                             'net' => 0,
  122.                             'gross' => 0,
  123.                             'linked' => false,
  124.                         ],
  125.                     ],
  126.                 ],
  127.             ],
  128.         ];
  129.     }
  130.     private function getExistingShippingMethodId($shippingMethodRepository$context): ?string
  131.     {
  132.         $criteria = (new Criteria())
  133.             ->addFilter(new EqualsFilter('name'self::SHIPPING_METHOD_NAME));
  134.         return $shippingMethodRepository->searchIds($criteria$context)->firstId();
  135.     }
  136.     private function addDeliveryTime($deliveryTimeRepositoryContext $context): string
  137.     {
  138.         $deliveryIdInsert Uuid::randomHex();
  139.         $deliveryTimeRepository->upsert([
  140.             [
  141.                 'id' => $deliveryIdInsert,
  142.                 'name' => 'All customers',
  143.                 'min' => 1,
  144.                 'max' => 5,
  145.                 'unit' => 'day',
  146.             ],
  147.         ], $context);
  148.         return $deliveryIdInsert;
  149.     }
  150.     public function addPaymentMethod(Context $context): void
  151.     {
  152.         $paymentMethodExists $this->getPaymentMethodId($context);
  153.         /* Payment method exists already, no need to continue here */
  154.         if ($paymentMethodExists) {
  155.             return;
  156.         }
  157.         /**
  158.          * @var PluginIdProvider $pluginIdProvider
  159.          */
  160.         $pluginIdProvider $this->container->get(PluginIdProvider::class);
  161.         $pluginId $pluginIdProvider->getPluginIdByBaseClass(static::class, $context);
  162.         $webshopnlData = [
  163.             'name' => 'webshopnl',
  164.             'pluginId' => $pluginId,
  165.             'afterOrderEnabled' => false,
  166.             'translations' => [
  167.                 'de-DE' => [
  168.                     'name' => 'webshopnl',
  169.                     'description' => 'webshopnl',
  170.                 ],
  171.                 'en-GB' => [
  172.                     'name' => 'webshopnl',
  173.                     'description' => 'webshopnl',
  174.                 ],
  175.             ],
  176.         ];
  177.         /**
  178.          * @var EntityRepository $paymentRepository
  179.          */
  180.         $paymentRepository $this->container->get('payment_method.repository');
  181.         $paymentRepository->create([$webshopnlData], $context);
  182.     }
  183.     private function getPaymentMethodId(Context $context): ?string
  184.     {
  185.         /**
  186.          * @var EntityRepository $paymentRepository
  187.          */
  188.         $paymentRepository $this->container->get('payment_method.repository');
  189.         /* Fetch ID for update */
  190.         $paymentCriteria = (new Criteria())->addFilter(new EqualsFilter('name''webshopnl'));
  191.         $paymentIds $paymentRepository->searchIds($paymentCriteria$context);
  192.         if ($paymentIds->getTotal() === 0) {
  193.             return null;
  194.         }
  195.         return $paymentIds->getIds()[0];
  196.     }
  197.     private function addConfigurationToken()
  198.     {
  199.         $config $this->container->get('Shopware\Core\System\SystemConfig\SystemConfigService');
  200.         $token $this->getRandomStringRand(25);
  201.         $config->set('MagmodulesWebshopnl.settings.integrationToken'$token);
  202.     }
  203.     function getRandomStringRand($length)
  204.     {
  205.         $stringSpace '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  206.         $stringLength strlen($stringSpace);
  207.         $randomString '';
  208.         for ($i 0$i $length$i++) {
  209.             $randomString $randomString $stringSpace[rand(0$stringLength 1)];
  210.         }
  211.         return $randomString;
  212.     }
  213. }