<?php
/**
*
* Copyright (c) 2017. The Cocktail
*
* This file is part of the Movistar Originales package.
*
*/
namespace AppBundle\Repository;
use AppBundle\Exception\ContentNotFoundException;
use AppBundle\QueryType\BlockQueryType;
use eZ\Publish\API\Repository\ContentService;
use eZ\Publish\API\Repository\ContentTypeService;
use eZ\Publish\API\Repository\SearchService;
class BlockRepository
{
/**
* @var SearchService
*/
protected $searchService;
/**
* @var ContentService
*/
protected $contentService;
/**
* @var ContentTypeService
*/
protected $contentTypeService;
/**
* @var BlockQueryType
*/
protected $blockQueryType;
public function __construct(
SearchService $searchService,
ContentService $contentService,
ContentTypeService $contentTypeService,
BlockQueryType $blockQueryType
) {
$this->searchService = $searchService;
$this->contentService = $contentService;
$this->contentTypeService = $contentTypeService;
$this->blockQueryType = $blockQueryType;
}
/**
/**
* Busca los bloques dentro de la carpeta bloques del locationid recibido
*
* @param integer $locationId
* @param bool $container
*
* @return \eZ\Publish\API\Repository\Values\Content\Content[]
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function loadBlocksByParentLocation(int $locationId, bool $container = false)
{
$parameters = ['locationId' => $locationId, 'container' => $container];
try {
$locationQuery = $this->blockQueryType->getQuery($parameters);
} catch (ContentNotFoundException $exception) {
return [];
}
$locationSearchResults = $this->searchService->findLocations($locationQuery);
$blocks = [];
foreach ($locationSearchResults->searchHits as $hit) {
$blocks[] = $this->contentService->loadContent($hit->valueObject->contentId);
}
return $blocks;
}
/**
* Busca los bloques dentro de la carpeta bloques del locationid recibido con tipo de bloque especificado
*
* @param integer $locationId
* @param string $renderer
* @param bool $container
* @return array
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function loadBlocksByParentLocationAndRenderer(int $locationId, string $renderer, bool $container = false)
{
$parameters = ['locationId' => $locationId, 'container' => $container];
$locationQuery = $this->blockQueryType->getQueryByRenderer($renderer, $parameters);
$locationSearchResults = $this->searchService->findLocations($locationQuery);
$blocks = [];
foreach ($locationSearchResults->searchHits as $hit) {
$content = $this->contentService->loadContent($hit->valueObject->contentId);
$blocks[] = $content;
}
return $blocks;
}
}