custom/plugins/TigerConnect/src/TigerConnect.php line 23

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace TigerMedia\TigerConnect;
  3. use Exception;
  4. use Shopware\Core\Checkout\Order\OrderDefinition;
  5. use Shopware\Core\Framework\Context;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  9. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. use Symfony\Component\Config\FileLocator;
  13. use Symfony\Component\Config\Loader\DelegatingLoader;
  14. use Symfony\Component\Config\Loader\LoaderResolver;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
  17. use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  19. use TigerMedia\Base\Core\Framework\TigerPlugin;
  20. class TigerConnect extends TigerPlugin
  21. {
  22.     const CONFIG_PREFIX 'TigerConnect.config.';
  23.     public function executeComposerCommands(): bool
  24.     {
  25.         return true;
  26.     }
  27.     /**
  28.      * @throws Exception
  29.      */
  30.     public function build(ContainerBuilder $container): void
  31.     {
  32.         parent::build($container);
  33.         $locator = new FileLocator('Resources/config');
  34.         $resolver = new LoaderResolver([
  35.             new YamlFileLoader($container$locator),
  36.             new GlobFileLoader($container$locator),
  37.             new DirectoryLoader($container$locator)
  38.         ]);
  39.         $configLoader = new DelegatingLoader($resolver);
  40.         $configDirectory rtrim($this->getPath(), '/') . '/Resources/config';
  41.         $configLoader->load($configDirectory '/{packages}/*.yaml''glob');
  42.     }
  43.     public function activate(ActivateContext $activateContext): void
  44.     {
  45.         $this->createCustomFieldSets($this->container->get('custom_field_set.repository'), $activateContext);
  46.         parent::activate($activateContext);
  47.     }
  48.     public function uninstall(UninstallContext $uninstallContext): void
  49.     {
  50.         $this->removeCustomFieldSets(
  51.             $this->container->get('custom_field_set.repository'),
  52.             Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
  53.             $uninstallContext->getContext()
  54.         );
  55.         parent::uninstall($uninstallContext);
  56.     }
  57.     public function deactivate(DeactivateContext $deactivateContext): void
  58.     {
  59.         $this->removeCustomFieldSets(
  60.             $this->container->get('custom_field_set.repository'),
  61.             Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
  62.             $deactivateContext->getContext()
  63.         );
  64.         parent::deactivate($deactivateContext);
  65.     }
  66.     /**
  67.      * @param EntityRepository $customFieldSetRepository
  68.      * @param ActivateContext $activateContext
  69.      * @return void
  70.      */
  71.     private function createCustomFieldSets(EntityRepository $customFieldSetRepositoryActivateContext $activateContext): void
  72.     {
  73.         $this->removeCustomFieldSets(
  74.             $customFieldSetRepository,
  75.             Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
  76.             $activateContext->getContext()
  77.         );
  78.         $customFieldSetRepository->upsert([
  79.             [
  80.                 'id'     => Uuid::fromStringToHex('TigerConnectCustomFieldSet'),
  81.                 'name'   => 'tiger_connect_custom_field_set',
  82.                 'active' => true,
  83.                 'config' => [
  84.                     'label' => [
  85.                         'en-GB' => 'TigerConnect'
  86.                     ]
  87.                 ],
  88.                 'customFields' => $this->getCustomFields(),
  89.                 'relations' => [
  90.                     [
  91.                         'id'         => Uuid::randomHex(),
  92.                         'entityName' => OrderDefinition::ENTITY_NAME
  93.                     ]
  94.                 ]
  95.             ]
  96.         ], $activateContext->getContext());
  97.     }
  98.     /**
  99.      * @param EntityRepository $customFieldSetRepository
  100.      * @param string $customFieldSetId
  101.      * @param Context $context
  102.      * @return void
  103.      */
  104.     private function removeCustomFieldSets(EntityRepository $customFieldSetRepositorystring $customFieldSetIdContext $context): void
  105.     {
  106.         $customFieldSetRepository->delete([
  107.             ['id' => $customFieldSetId]
  108.         ], $context);
  109.     }
  110.     /**
  111.      * @return mixed[]
  112.      */
  113.     private function getCustomFields(): array
  114.     {
  115.         return [
  116.             [
  117.                 'id'     => Uuid::fromStringToHex('TigerConnectProcessed'),
  118.                 'name'   => 'tiger_connect_custom_field_set_processed',
  119.                 'type'   => CustomFieldTypes::BOOL,
  120.                 'config' => [
  121.                     'label' => [
  122.                         'en-GB' => 'Order Processed'
  123.                     ],
  124.                     'componentName'       => 'sw-field',
  125.                     'customFieldPosition' => 0
  126.                 ]
  127.             ],
  128.             [
  129.                 'id'     => Uuid::fromStringToHex('TigerConnectErpOrderNumber'),
  130.                 'name'   => 'tiger_connect_custom_field_set_erp_order_number',
  131.                 'type'   => CustomFieldTypes::TEXT,
  132.                 'config' => [
  133.                     'label' => [
  134.                         'en-GB' => 'ERP Order Number'
  135.                     ],
  136.                     'componentName'       => 'sw-field',
  137.                     'customFieldPosition' => 1
  138.                 ]
  139.             ]
  140.         ];
  141.     }
  142. }