<?php

namespace ArcaSolutions\WebBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NonUniqueResultException;

/**
 * ContentRepository
 *
 * This class was generated by the PhpStorm "Php Annotations" Plugin. Add your own custom
 * repository methods below.
 */
class ContentRepository extends EntityRepository
{
    /**
     * @param $type
     * @param bool $allField
     * @return mixed
     * @throws \Exception
     */
    public function getContent($type, $allField = false)
    {
        try {
            $qb = $this->createQueryBuilder('s')
                ->where('s.type = :type')
                ->setParameter(':type', $type)
                ->getQuery()
                ->getOneOrNullResult();

            $content = $allField ? $qb : $qb->getContent();
        } catch (NonUniqueResultException $e) {
            throw new \Exception(sprintf('Content %s not found', $type));
        }

        return $content;
    }
}
