<?php
namespace Abas\AbasConnector\Subscribers\Extension;
use Abas\AbasConnector\Service\Component\AbasComponentFactory;
use Abas\AbasConnector\Service\Core\Enums\Duration;
use Abas\AbasConnector\Service\RestApi\Response\Content\ErpDataObject;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Entity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use function str_replace;
abstract class AbasExtensionSubscriber implements EventSubscriberInterface
{
/**
* @var CacheInterface
*/
private $cache;
/**
* @var AbasComponentFactory
*/
private $componentFactory;
/**
* @param CacheInterface $cache
* @param AbasComponentFactory $componentFactory
*/
public function __construct(CacheInterface $cache, AbasComponentFactory $componentFactory)
{
$this->cache = $cache;
$this->componentFactory = $componentFactory;
}
/**
* @param EntityLoadedEvent $event
*
* @return void
*/
public function onEntitiesLoaded(EntityLoadedEvent $event): void
{
if (!($event->getContext()->getSource() instanceof SalesChannelApiSource)) {
return;
}
$entities = $event->getEntities();
/** @var Entity $entity */
foreach ($entities as $entity) {
$abasObject = $this->getErpObject($entity);
$entity->addExtension('abas_object', $abasObject);
}
}
/**
* @param Entity $entity
*
* @return null|ErpDataObject
*/
protected function getErpObject(Entity $entity): ?ErpDataObject
{
$databaseAndGroup = $this->getDatabaseAndGroup($entity);
$id = $this->getSelectionIdentifier($entity);
if ($databaseAndGroup !== null && $id !== null) {
$key = str_replace(
['{', '}', '(', ')', '/', '\\', '@', ':'],
'',
'AbasObjectExtensionCache|' . $id
);
return $this->cache->get(
$key,
function (ItemInterface $item) use ($id, $databaseAndGroup) {
$item->expiresAfter(Duration::HOUR);
$component = $this->componentFactory->createDataComponent($databaseAndGroup);
$component->view($id);
return $component->getObject();
}
);
}
return null;
}
/**
* Return the abas identifier value (GUID or ID).
*
* @param Entity $entity
*
* @return string|null
*/
protected function getSelectionIdentifier(Entity $entity): ?string
{
$guid = $this->getCustomFieldValue($entity, 'abas_guid');
if ($guid !== null) {
return '$,,guid==' . $guid;
}
return $this->getCustomFieldValue($entity, 'abas_id');
}
/**
* Return the database and group for the entity.
*
* @param Entity $entity
*
* @return string|null
*/
protected function getDatabaseAndGroup(Entity $entity): ?string
{
return $this->getCustomFieldValue($entity, 'abas_database_and_group');
}
/**
* Get a customFields value
*
* @param Entity $entity
* @param string $key
*
* @return string|null
*/
protected function getCustomFieldValue(Entity $entity, string $key): ?string
{
if ($entity->has('customFields')) {
$customFields = $entity->get('customFields');
if ($customFields && isset($customFields[$key])) {
return $customFields[$key];
}
}
return null;
}
}