Commit 40fe4ced authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

EntitySourceInjector: possibilité de spécifier la classe d'entité SOurce à utiliser.

parent c02bc2f4
......@@ -21,6 +21,22 @@ use UnicaenImport\Entity\Db\Source;
*/
class EntitySourceInjector implements EventSubscriber
{
/**
* @var string
*/
protected $sourceEntityClass = Source::class;
/**
* @param string $sourceEntityClass
* @return self
*/
public function setSourceEntityClass(string $sourceEntityClass): self
{
$this->sourceEntityClass = $sourceEntityClass;
return $this;
}
/**
* @var Source
*/
......@@ -73,7 +89,7 @@ class EntitySourceInjector implements EventSubscriber
// Injection de la source
/** @var Source $source */
$source = $args->getEntityManager()->getRepository(Source::class)->findOneBy([
$source = $args->getEntityManager()->getRepository($this->sourceEntityClass)->findOneBy([
'code' => $this->sourceCode
]);
$entity->setSource($source);
......
......@@ -2,31 +2,42 @@
namespace UnicaenImport\ORM\Event\Listeners;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use UnicaenImport\Entity\Db\Source;
use Zend\ServiceManager\ServiceLocatorInterface as ContainerInterface;
final class EntitySourceInjectorFactory implements FactoryInterface
final class EntitySourceInjectorFactory
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $container
* @return EntitySourceInjector
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container)
{
$sourceCode = $this->getDefaultSourceCode($serviceLocator);
$config = $container->get('Config');
$sourceEntityClass = $this->getSourceEntityClass($config) ?: Source::class;
$sourceCode = $this->getDefaultSourceCode($config);
$service = new EntitySourceInjector();
$service->setSourceEntityClass($sourceEntityClass);
$service->setSourceCode($sourceCode);
return $service;
}
private function getDefaultSourceCode(ServiceLocatorInterface $serviceLocator)
private function getSourceEntityClass(array $config)
{
$config = $serviceLocator->get('Config');
if (! isset($config['unicaen-import']['entity_source_injector']['source_entity_class'])) {
return null;
}
return $config['unicaen-import']['entity_source_injector']['source_entity_class'];
}
private function getDefaultSourceCode(array $config)
{
if (! isset($config['unicaen-import']['entity_source_injector']['source_code'])) {
return null;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment