public function before(DateTime $before, $user = null, $count = false)
    {
        // Validate and normalize input
        $before = $this->validateTimezone($before);
        $user = $this->validateUser($user);

        // Build the query builder
        $qb = $this->createQueryBuilder('a');

        if ($count) {
            $qb->select('count(a)');
        } else {
            $qb->select();
        }

        $qb->where('a.utcDate < :before')->setParameter('before', $before);

        // Pivot on user id if needed
        if ($user) {
            $qb->andWhere('a.userId=:user')->setParameter('user', $user);
        }

        if ($count) {
            try {
                return $qb->getQuery()->getSingleScalarResult();
            } catch (UnexpectedResultException $e) {
                return 0;
            }
        } else {
            // Return an iterator that contains all login attempts before a given date
            return $qb->getQuery()->iterate();
        }
    }