<?php

namespace Club\MessageBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * MessageRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class MessageRepository extends EntityRepository
{
    public function getCount($type=null)
    {
        return count($this->getQueryBuilder($type)->getQuery()->getResult());
    }

    public function getWithPagination($offset = 0, $limit = 0, $type=null)
    {
        return $this->getQueryBuilder($type)
            ->setFirstResult($offset)
            ->setMaxResults($limit)
            ->getQuery()
            ->getResult();
    }

    private function getQueryBuilder($type=null)
    {
        $qb = $this
            ->createQueryBuilder('m')
            ->orderBy('m.id','DESC');

        if ($type == 'archive') {
            $qb->where('m.processed = true');
        }

        return $qb;
    }

    public function getAllReady()
    {
        return $this->createQueryBuilder('m')
            ->where('m.ready = 1')
            ->andWhere('m.processed = 0')
            ->getQuery()
            ->getResult();
    }

    public function getDrafts()
    {
        return $this->createQueryBuilder('m')
            ->where('m.processed = false')
            ->orderBy('m.id', 'DESC')
            ->getQuery()
            ->getResult();
    }
}
