Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

5 years ago
  1. <?php
  2. namespace Zitec\RuleEngineBundle\Service;
  3. use Doctrine\ORM\EntityManager;
  4. use Zitec\RuleEngineBundle\DoctrineBehaviors\PostconditionInterface;
  5. use Zitec\RuleEngineBundle\DoctrineBehaviors\PreconditionInterface;
  6. use Zitec\RuleEngineBundle\DoctrineBehaviors\RuleInterface;
  7. /**
  8. * Class RuleEngineOrchestrator
  9. *
  10. * @package Zitec\RuleEngineBundle\Service
  11. */
  12. class Orchestrator
  13. {
  14. /**
  15. * @var EntityManager
  16. */
  17. protected $entityManager;
  18. /**
  19. * @var RuleConditionsManager[]
  20. */
  21. protected $conditionsManagers;
  22. /**
  23. * RuleEngineOrchestrator constructor.
  24. *
  25. * @param EntityManager $entityManager
  26. */
  27. public function __construct(EntityManager $entityManager)
  28. {
  29. $this->entityManager = $entityManager;
  30. }
  31. /**
  32. * @param string $entityName
  33. * @param RuleConditionsManager $conditionsManager
  34. */
  35. public function setEntityConditionsManager(string $entityName, RuleConditionsManager $conditionsManager)
  36. {
  37. $entityClassName = $this->entityManager->getMetadataFactory()->getMetadataFor($entityName)->getName();
  38. $this->conditionsManagers[$entityClassName] = $conditionsManager;
  39. }
  40. /**
  41. * @param RuleInterface|PreconditionInterface|PostconditionInterface $entity
  42. * @return RuleConditionsManager
  43. */
  44. public function getConditionsManagerForEntity( $entity)
  45. {
  46. $entityClassName = get_class($entity);
  47. if (!isset($this->conditionsManagers[$entityClassName])) {
  48. throw new \InvalidArgumentException(sprintf('No conditions manager defined for entity %s', $entityClassName));
  49. }
  50. return $this->conditionsManagers[$entityClassName];
  51. }
  52. }