Spaces:
No application file
No application file
File size: 2,006 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<?php
declare(strict_types=1);
namespace Mautic\IntegrationsBundle\Sync\SyncDataExchange\Internal;
use Mautic\IntegrationsBundle\Event\InternalObjectEvent;
use Mautic\IntegrationsBundle\IntegrationEvents;
use Mautic\IntegrationsBundle\Sync\Exception\ObjectNotFoundException;
use Mautic\IntegrationsBundle\Sync\SyncDataExchange\Internal\Object\ObjectInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ObjectProvider
{
/**
* Cached internal objects.
*
* @var ObjectInterface[]
*/
private array $objects = [];
public function __construct(
private EventDispatcherInterface $dispatcher
) {
}
/**
* @throws ObjectNotFoundException
*/
public function getObjectByName(string $name): ObjectInterface
{
$this->collectObjects();
foreach ($this->objects as $object) {
if ($object->getName() === $name) {
return $object;
}
}
throw new ObjectNotFoundException("Internal object '{$name}' was not found");
}
/**
* @throws ObjectNotFoundException
*/
public function getObjectByEntityName(string $entityName): ObjectInterface
{
$this->collectObjects();
foreach ($this->objects as $object) {
if ($object->getEntityName() === $entityName) {
return $object;
}
}
throw new ObjectNotFoundException("Internal object was not found for entity '{$entityName}'");
}
/**
* Dispatches an event to collect all internal objects.
* It caches the objects to a local property so it won't dispatch every time but only once.
*/
private function collectObjects(): void
{
if (empty($this->objects)) {
$event = new InternalObjectEvent();
$this->dispatcher->dispatch($event, IntegrationEvents::INTEGRATION_COLLECT_INTERNAL_OBJECTS);
$this->objects = $event->getObjects();
}
}
}
|