<?php
/**
*
* Copyright (c) 2017. The Cocktail
*
* This file is part of the Movistar Originales package.
*
*/
namespace AppBundle\EventListener;
use AppBundle\Api\ContentCreator;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\Core\Helper\FieldHelper;
use eZ\Publish\Core\MVC\Symfony\Event\SignalEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PublishListener implements EventSubscriberInterface
{
const CONTAINS_BLOCK = ['serie','page','hub','article','season','episode'];
protected $contentService;
protected $contentTypeService;
protected $contentCreator;
protected $fieldHelper;
public function __construct(
ContentService $contentService,
ContentTypeService $contentTypeService,
ContentCreator $contentCreator,
FieldHelper $fieldHelper
) {
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
$this->contentCreator = $contentCreator;
$this->fieldHelper = $fieldHelper;
}
public static function getSubscribedEvents()
{
return array(
MVCEvents::API_SIGNAL => 'createContentOnPublish'
);
}
public function createContentOnPublish(SignalEvent $event)
{
$signal = $event->getSignal();
if (!$signal instanceof PublishVersionSignal || $signal->versionNo > 1) {
return;
}
$content = $this->contentService->loadContent($signal->contentId, null, $signal->versionNo);
$contentType = $this->contentTypeService->loadContentType($content->contentInfo->contentTypeId);
$identifier = $contentType->identifier;
if (in_array($identifier, self::CONTAINS_BLOCK)) {
$this->contentCreator->createSubitemsByName($content->contentInfo, "Bloques");
}
if ($identifier === 'serie') {
if (!$this->fieldHelper->isFieldEmpty($content, 'endpoint')) {
$this->contentCreator->createSubpage($content->contentInfo, "capitulos");
}
}
}
}