vendor/rawafed/commons-bundle/Service/ViewsCountListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace Rawafed\CommonsBundle\Service;
  3. use Doctrine\Common\Annotations\Reader;
  4. use Doctrine\ORM\EntityManager;
  5. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  6. class ViewsCountListener
  7. {
  8.     private $entityManager;
  9.     private $annotationReader;
  10.     public function __construct(Reader $annotationReaderEntityManager $entityManager)
  11.     {
  12.         $this->annotationReader $annotationReader;
  13.         $this->entityManager $entityManager;
  14.     }
  15.     public function onKernelController(ControllerEvent $event)
  16.     {
  17.         if ($event->isMasterRequest()) {
  18.             $request $event->getRequest();
  19.             $controller $event->getController();
  20.             if (!is_array($controller)) {
  21.                 return;
  22.             }
  23.             list($controllerObject$methodName) = $controller;
  24.             $refMethod = new \ReflectionMethod($controllerObject$methodName);
  25.             $annotations $this->annotationReader->getMethodAnnotations($refMethod);
  26.             foreach ($annotations as $annotation) {
  27.                 if ($annotation instanceof ViewsCount) {
  28.                     $attributes $request->attributes->all();
  29.                     foreach ($attributes as $name => $attribute) {
  30.                         if ($attribute instanceof \Rawafed\CommonsBundle\Entity\CountableInterface) {
  31.                             $__stats = array(
  32.                                 'type' => get_class($attribute),
  33.                                 'id' => $attribute->getId()
  34.                             );
  35.                             $field $annotation->getFieldName();
  36.                             if ($field != 'views_count') {
  37.                                 $__stats['field'] = $field;
  38.                             }
  39.                             $request->attributes->set('__stats'$__stats);
  40.                             break;
  41.                         }
  42.                     }
  43.                     break;
  44.                 }
  45.             }
  46.         }
  47.     }
  48. }