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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Moorl\MerchantFinder;
  3. use Doctrine\DBAL\Connection;
  4. use MoorlFoundation\Core\Service\DataService;
  5. use MoorlFoundation\Core\Service\LocationServiceV2;
  6. use Shopware\Core\Framework\Migration\InheritanceUpdaterTrait;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. class MoorlMerchantFinder extends Plugin
  13. {
  14.     use InheritanceUpdaterTrait;
  15.     public const NAME 'MoorlMerchantFinder';
  16.     public const DATA_CREATED_AT '2003-03-03 03:03:10.000';
  17.     public const CMS_PAGE 'moorl_merchant';
  18.     public const CMS_PAGE_ID 'd294117e81a9dee634b92f190d7719a3';
  19.     public const CMS_PAGE_MERCHANT_DEFAULT_ID 'fa05048f53068a5ec29e2c9aea4e8c40';
  20.     public const PLUGIN_TABLES = [
  21.         'moorl_merchant',
  22.         'moorl_zipcode',
  23.         'moorl_merchant_category',
  24.         'moorl_merchant_product_manufacturer',
  25.         'moorl_merchant_tag',
  26.         'moorl_merchant_oh',
  27.         'moorl_merchant_translation',
  28.         'moorl_merchant_stock',
  29.         'moorl_merchant_customer',
  30.         'moorl_merchant_marker',
  31.         'moorl_merchant_area',
  32.         'moorl_merchant_sales_channel'
  33.     ];
  34.     public const SHOPWARE_TABLES = [
  35.         'media_folder',
  36.         'moorl_sorting',
  37.         'cms_page',
  38.         'cms_page_translation',
  39.         'cms_section',
  40.         'cms_block',
  41.         'category',
  42.         'category_translation',
  43.         'product',
  44.         'product_translation',
  45.         'product_category',
  46.         'product_visibility',
  47.         'seo_url_template'
  48.     ];
  49.     public const INHERITANCES = [
  50.         'product' => ['MoorlMerchants''MoorlMerchantStocks'],
  51.         'sales_channel' => ['MoorlMerchants'],
  52.         'customer' => ['MoorlMerchants'],
  53.         'order_line_item' => ['MoorlMerchantStock'],
  54.     ];
  55.     public function build(ContainerBuilder $container): void
  56.     {
  57.         if (class_exists(LocationServiceV2::class)) {
  58.             parent::build($container);
  59.         }
  60.     }
  61.     public function activate(ActivateContext $activateContext): void
  62.     {
  63.         parent::activate($activateContext);
  64.         $this->updateInheritances();
  65.         /* @var $dataService DataService */
  66.         $dataService $this->container->get(DataService::class);
  67.         $dataService->install(self::NAME);
  68.     }
  69.     public function update(UpdateContext $updateContext): void
  70.     {
  71.         parent::update($updateContext);
  72.         $this->updateInheritances();
  73.     }
  74.     public function uninstall(UninstallContext $uninstallContext): void
  75.     {
  76.         parent::uninstall($uninstallContext);
  77.         if ($uninstallContext->keepUserData()) {
  78.             return;
  79.         }
  80.         $this->uninstallTrait();
  81.     }
  82.     private function updateInheritances(): void
  83.     {
  84.         $connection $this->container->get(Connection::class);
  85.         foreach (self::INHERITANCES as $table => $propertyNames) {
  86.             foreach ($propertyNames as $propertyName) {
  87.                 try {
  88.                     $this->updateInheritance($connection$table$propertyName);
  89.                 } catch (\Exception $exception) {
  90.                     continue;
  91.                 }
  92.             }
  93.         }
  94.     }
  95.     private function uninstallTrait(): void
  96.     {
  97.         $connection $this->container->get(Connection::class);
  98.         foreach (self::PLUGIN_TABLES as $table) {
  99.             $sql sprintf('SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `%s`;'$table);
  100.             $connection->executeStatement ($sql);
  101.         }
  102.         foreach (array_reverse(self::SHOPWARE_TABLES) as $table) {
  103.             $sql sprintf("SET FOREIGN_KEY_CHECKS=0; DELETE FROM `%s` WHERE `created_at` = '%s';"$tableself::DATA_CREATED_AT);
  104.             try {
  105.                 $connection->executeStatement ($sql);
  106.             } catch (\Exception $exception) {
  107.                 continue;
  108.             }
  109.         }
  110.         foreach (self::INHERITANCES as $table => $propertyNames) {
  111.             foreach ($propertyNames as $propertyName) {
  112.                 $sql sprintf("ALTER TABLE `%s` DROP `%s`;"$table$propertyName);
  113.                 try {
  114.                     $connection->executeStatement ($sql);
  115.                 } catch (\Exception $exception) {
  116.                     continue;
  117.                 }
  118.             }
  119.         }
  120.     }
  121.     private static $_defaults = [
  122.         'allowedSearchCountryCodes' => ['de''at''ch'],
  123.         'nominatim' => 'https://nominatim.openstreetmap.org/search',
  124.     ];
  125.     public static function getDefault($key)
  126.     {
  127.         return static::$_defaults[$key];
  128.     }
  129. }