vendor/cnge/common-bundle/NotifiableDiscovery.php line 38

Open in your IDE?
  1. <?php
  2. namespace Cnge\CommonBundle;
  3. use Doctrine\Common\Annotations\Reader;
  4. use Doctrine\Common\Util\ClassUtils;
  5. use Doctrine\ORM\EntityManager;
  6. use Metadata\ClassMetadata;
  7. class NotifiableDiscovery {
  8.     
  9.     /**
  10.      * @var Reader
  11.      */
  12.     private $annotationReader;
  13.     /**
  14.      * @var EntityManager
  15.      */
  16.     private $em;
  17.     /**
  18.      * @var array
  19.      */
  20.     private $notifiables = [];
  21.     /**
  22.      * WorkerDiscovery constructor.
  23.      *
  24.      * @param EntityManager $em
  25.      * @param Reader        $annotationReader
  26.      *
  27.      * @throws \InvalidArgumentException
  28.      */
  29.     public function __construct(EntityManager $emReader $annotationReader) {
  30.         $this->annotationReader $annotationReader;
  31.         $this->em $em;
  32.         $this->discoverNotifiables();
  33.     }
  34.     /**
  35.      * Returns all the workers
  36.      * @throws \InvalidArgumentException
  37.      */
  38.     public function getNotifiables() {
  39.         return $this->notifiables;
  40.     }
  41.     /**
  42.      * @param NotifiableInterface $notifiable
  43.      *
  44.      * @return string|null
  45.      */
  46.     public function getNotifiableName(NotifiableInterface $notifiable) {
  47.         // fixes the case when the notifiable is a proxy
  48.         $class ClassUtils::getRealClass(get_class($notifiable));
  49.         $annotation $this->annotationReader->getClassAnnotation(new \ReflectionClass($class), 'Cnge\CommonBundle\Annotation\Notifiable');
  50.         if ($annotation) {
  51.             return $annotation->getName();
  52.         }
  53.         return null;
  54.     }
  55.     /**
  56.      * Discovers workers
  57.      * @throws \InvalidArgumentException
  58.      */
  59.     private function discoverNotifiables() {
  60.         /** @var ClassMetadata[] $entities */
  61.         $entities $this->em->getMetadataFactory()->getAllMetadata();
  62.         foreach ($entities as $entity) {
  63.             $class $entity->name;
  64.             $annotation $this->annotationReader->getClassAnnotation(new \ReflectionClass($class), 'Cnge\CommonBundle\Annotation\Notifiable');
  65.             if ($annotation) {
  66.                 $this->notifiables[$annotation->getName()] = [
  67.                     'class' => $entity->name,
  68.                     'annotation' => $annotation,
  69.                     'identifiers' => $entity->getIdentifier()
  70.                 ];
  71.             }
  72.         }
  73.     }
  74. }