vendor/mgilet/notification-bundle/NotifiableDiscovery.php line 76

Open in your IDE?
  1. <?php
  2. namespace Mgilet\NotificationBundle;
  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.     {
  31.         $this->annotationReader $annotationReader;
  32.         $this->em $em;
  33.         $this->discoverNotifiables();
  34.     }
  35.     /**
  36.      * Returns all the workers
  37.      * @throws \InvalidArgumentException
  38.      */
  39.     public function getNotifiables()
  40.     {
  41.         return $this->notifiables;
  42.     }
  43.     /**
  44.      * @param NotifiableInterface $notifiable
  45.      *
  46.      * @return string|null
  47.      */
  48.     public function getNotifiableName(NotifiableInterface $notifiable)
  49.     {
  50.         // fixes the case when the notifiable is a proxy
  51.         $class ClassUtils::getRealClass(get_class($notifiable));
  52.         $annotation $this->annotationReader->getClassAnnotation(new \ReflectionClass($class), 'Mgilet\NotificationBundle\Annotation\Notifiable');
  53.         if ($annotation) {
  54.             return $annotation->getName();
  55.         }
  56.         return null;
  57.     }
  58.     /**
  59.      * Discovers workers
  60.      * @throws \InvalidArgumentException
  61.      */
  62.     private function discoverNotifiables()
  63.     {
  64.         /** @var ClassMetadata[] $entities */
  65.         $entities $this->em->getMetadataFactory()->getAllMetadata();
  66.         foreach ($entities as $entity) {
  67.             $class $entity->name;
  68.             $annotation $this->annotationReader->getClassAnnotation(new \ReflectionClass($class), 'Mgilet\NotificationBundle\Annotation\Notifiable');
  69.             if ($annotation) {
  70.                 $this->notifiables[$annotation->getName()] = [
  71.                     'class' => $entity->name,
  72.                     'annotation' => $annotation,
  73.                     'identifiers' => $entity->getIdentifier()
  74.                 ];
  75.             }
  76.         }
  77.     }
  78. }