/**
  * Finds the subscription by user an year.
  *
  * @param UserInterface $user
  * @return \Ekyna\Bundle\SubscriptionBundle\Model\SubscriptionInterface|null
  */
 public function findOneByUserAndYear(UserInterface $user, $year)
 {
     Year::validate($year);
     $qb = $this->createQueryBuilder('s');
     $parameters = array('user' => $user, 'year' => $year);
     $qb->join('s.price', 'price')->join('price.pricing', 'pricing')->andWhere($qb->expr()->eq('s.user', ':user'))->andWhere($qb->expr()->eq('pricing.year', ':year'));
     return $qb->getQuery()->setMaxResults(1)->setParameters($parameters)->getOneOrNullResult();
 }
    /**
     * {@inheritdoc}
     */
    public function findPriceByUserAndYear(UserInterface $user, $year)
    {
        $year = Year::validate($year);
        $findPriceByUserAndYearDql = <<<DQL
SELECT p FROM %s p
JOIN p.pricing y
WHERE y.year = :year
AND p.group = :group
DQL;
        $dql = sprintf($findPriceByUserAndYearDql, $this->priceClass);
        return $this->em->createQuery($dql)->setMaxResults(1)->setParameter('year', $year)->setParameter('group', $user->getGroup())->getOneOrNullResult();
    }
 /**
  * Generates the subscription by user and year
  *
  * @param UserInterface $user
  * @param string        $year
  * @return \Ekyna\Bundle\SubscriptionBundle\Model\SubscriptionInterface|null
  */
 public function generateByUserAndYear(UserInterface $user, $year)
 {
     $year = Year::validate($year);
     $price = $this->pricingProvider->findPriceByUserAndYear($user, $year);
     if (!$price instanceof PriceInterface) {
         return null;
     }
     /** @var \Ekyna\Bundle\SubscriptionBundle\Model\SubscriptionInterface $subscription */
     $subscription = new $this->subscriptionClass();
     $subscription->setUser($user)->setPrice($price);
     return $this->createSubscription($subscription);
 }