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

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