<?php

namespace UserBundle\Repository;

use Doctrine\ORM\EntityRepository;
use UserBundle\Entity\Subscription\AbstractSubscription;

/**
 * SubscriptionRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SubscriptionRepository extends EntityRepository
{

    /**
     * Unsubscribe all subscribers from specified by id plan.
     *
     * @param string $planId Plan id from which we should unsubscribe.
     *
     * @return AbstractSubscription[]
     */
    public function getForPlan($planId)
    {
        return $this->createQueryBuilder('Subscription')
            ->where('Subscription.plan = :plan')
            ->setParameter('plan', $planId)
            ->getQuery()
            ->getResult();
    }

    /**
     * Set search per days limit to zero.
     *
     * @return void
     */
    public function renewSearchLimits()
    {
        $this->createQueryBuilder('Subscription')
            ->update()
            ->set('Subscription.searchesPerDay', 0)
            ->getQuery()
            ->execute();
    }

    /**
     * @param integer $organization A Organization entity id.
     *
     * @return \Doctrine\ORM\QueryBuilder
     */
    public function getForOrganization($organization)
    {
        return $this->createQueryBuilder('Subscription')
            ->addSelect('Owner, partial Plan.{id, name}')
            ->join('Subscription.owner', 'Owner')
            ->join('Subscription.plan', 'Plan')
            ->where('Subscription.organization = :organization')
            ->setParameter('organization', $organization);
    }
}
