<?php

namespace Vipa\JournalBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

/**
 * ArticleFileRepository
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ArticleFileRepository extends EntityRepository
{
    /**
     * Get Article full text file.
     * @param  integer $articleId
     * @return ArticleFile[]
     * @throws NoResultException
     */
    public function getArticleFullTextFiles($articleId)
    {
        try {
            $article = $this
                ->getEntityManager()
                ->getRepository('VipaJournalBundle:Article')
                ->find($articleId);

            if (!$article) {
                throw new NoResultException();
            }

            $q = $this
                ->createQueryBuilder('a')
                ->select('a')
                ->where('a.article = :article AND a.type= 0')
                ->setParameter('article', $article)
                ->getQuery();

            // The Query::getResult() method throws an exception
            // if there is no record matching the criteria.
            return $q->getResult();
        } catch (NoResultException $e) {
            $message = sprintf('There is no full text file for this article.');
            throw new NoResultException($message, 0, $e);
        }
    }
}
