custom/static-plugins/AbasConnector/src/Subscribers/Extension/AbasExtensionSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. namespace Abas\AbasConnector\Subscribers\Extension;
  3. use Abas\AbasConnector\Service\Component\AbasComponentFactory;
  4. use Abas\AbasConnector\Service\Core\Enums\Duration;
  5. use Abas\AbasConnector\Service\RestApi\Response\Content\ErpDataObject;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Contracts\Cache\CacheInterface;
  11. use Symfony\Contracts\Cache\ItemInterface;
  12. use function str_replace;
  13. abstract class AbasExtensionSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var CacheInterface
  17.      */
  18.     private $cache;
  19.     /**
  20.      * @var AbasComponentFactory
  21.      */
  22.     private $componentFactory;
  23.     /**
  24.      * @param CacheInterface $cache
  25.      * @param AbasComponentFactory $componentFactory
  26.      */
  27.     public function __construct(CacheInterface $cacheAbasComponentFactory $componentFactory)
  28.     {
  29.         $this->cache $cache;
  30.         $this->componentFactory $componentFactory;
  31.     }
  32.     /**
  33.      * @param EntityLoadedEvent $event
  34.      *
  35.      * @return void
  36.      */
  37.     public function onEntitiesLoaded(EntityLoadedEvent $event): void
  38.     {
  39.         if (!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
  40.             return;
  41.         }
  42.         $entities $event->getEntities();
  43.         /** @var Entity $entity */
  44.         foreach ($entities as $entity) {
  45.             $abasObject $this->getErpObject($entity);
  46.             $entity->addExtension('abas_object'$abasObject);
  47.         }
  48.     }
  49.     /**
  50.      * @param Entity $entity
  51.      *
  52.      * @return null|ErpDataObject
  53.      */
  54.     protected function getErpObject(Entity $entity): ?ErpDataObject
  55.     {
  56.         $databaseAndGroup $this->getDatabaseAndGroup($entity);
  57.         $id $this->getSelectionIdentifier($entity);
  58.         if ($databaseAndGroup !== null && $id !== null) {
  59.             $key str_replace(
  60.                 ['{''}''('')''/''\\''@'':'],
  61.                 '',
  62.                 'AbasObjectExtensionCache|' $id
  63.             );
  64.             return $this->cache->get(
  65.                 $key,
  66.                 function (ItemInterface $item) use ($id$databaseAndGroup) {
  67.                     $item->expiresAfter(Duration::HOUR);
  68.                     $component $this->componentFactory->createDataComponent($databaseAndGroup);
  69.                     $component->view($id);
  70.                     return $component->getObject();
  71.                 }
  72.             );
  73.         }
  74.         return null;
  75.     }
  76.     /**
  77.      * Return the abas identifier value (GUID or ID).
  78.      *
  79.      * @param Entity $entity
  80.      *
  81.      * @return string|null
  82.      */
  83.     protected function getSelectionIdentifier(Entity $entity): ?string
  84.     {
  85.         $guid $this->getCustomFieldValue($entity'abas_guid');
  86.         if ($guid !== null) {
  87.             return '$,,guid==' $guid;
  88.         }
  89.         return $this->getCustomFieldValue($entity'abas_id');
  90.     }
  91.     /**
  92.      * Return the database and group for the entity.
  93.      *
  94.      * @param Entity $entity
  95.      *
  96.      * @return string|null
  97.      */
  98.     protected function getDatabaseAndGroup(Entity $entity): ?string
  99.     {
  100.         return $this->getCustomFieldValue($entity'abas_database_and_group');
  101.     }
  102.     /**
  103.      * Get a customFields value
  104.      *
  105.      * @param Entity $entity
  106.      * @param string $key
  107.      *
  108.      * @return string|null
  109.      */
  110.     protected function getCustomFieldValue(Entity $entitystring $key): ?string
  111.     {
  112.         if ($entity->has('customFields')) {
  113.             $customFields $entity->get('customFields');
  114.             if ($customFields && isset($customFields[$key])) {
  115.                 return $customFields[$key];
  116.             }
  117.         }
  118.         return null;
  119.     }
  120. }