src/AppBundle/EventListener/PublishListener.php line 54

Open in your IDE?
  1. <?php
  2. /**
  3.  *
  4.  * Copyright (c) 2017. The Cocktail
  5.  *
  6.  * This file is part of the Movistar Originales package.
  7.  *
  8.  */
  9. namespace AppBundle\EventListener;
  10. use AppBundle\Api\ContentCreator;
  11. use eZ\Publish\API\Repository\ContentTypeService;
  12. use eZ\Publish\Core\Helper\FieldHelper;
  13. use eZ\Publish\Core\MVC\Symfony\Event\SignalEvent;
  14. use eZ\Publish\Core\MVC\Symfony\MVCEvents;
  15. use eZ\Publish\API\Repository\ContentService;
  16. use eZ\Publish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. class PublishListener implements EventSubscriberInterface
  19. {
  20.     const CONTAINS_BLOCK = ['serie','page','hub','article','season','episode'];
  21.     protected $contentService;
  22.     protected $contentTypeService;
  23.     protected $contentCreator;
  24.     protected $fieldHelper;
  25.     public function __construct(
  26.         ContentService $contentService,
  27.         ContentTypeService $contentTypeService,
  28.         ContentCreator $contentCreator,
  29.         FieldHelper $fieldHelper
  30.     ) {
  31.         $this->contentService $contentService;
  32.         $this->contentTypeService $contentTypeService;
  33.         $this->contentCreator $contentCreator;
  34.         $this->fieldHelper $fieldHelper;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return array(
  39.             MVCEvents::API_SIGNAL => 'createContentOnPublish'
  40.         );
  41.     }
  42.     public function createContentOnPublish(SignalEvent $event)
  43.     {
  44.         $signal $event->getSignal();
  45.         if (!$signal instanceof PublishVersionSignal || $signal->versionNo 1) {
  46.             return;
  47.         }
  48.         $content $this->contentService->loadContent($signal->contentIdnull$signal->versionNo);
  49.         $contentType $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
  50.         $identifier $contentType->identifier;
  51.         if (in_array($identifierself::CONTAINS_BLOCK)) {
  52.             $this->contentCreator->createSubitemsByName($content->contentInfo"Bloques");
  53.         }
  54.         if ($identifier === 'serie') {
  55.             if (!$this->fieldHelper->isFieldEmpty($content'endpoint')) {
  56.                 $this->contentCreator->createSubpage($content->contentInfo"capitulos");
  57.             }
  58.         }
  59.     }
  60. }