<commit_before><?php
/**
 * Created by PhpStorm.
 * User: loic
 * Date: 09/03/2016
 * Time: 09:33
 */

namespace AppBundle\Repository;

use AppBundle\Entity\Product;
use AppBundle\Utils\DateCalculator;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class ProductRepository extends BaseProductRepository
{
    /**
     * @param $localeCode
     * @param bool $onlyPublished
     * @param array $criteria
     *
     * @return QueryBuilder
     */
    public function createListQueryBuilder($localeCode, $onlyPublished=true, array $criteria = [])
    {
        $queryBuilder = $this->createQueryBuilder('o');

        $queryBuilder
            ->addSelect('translation')
            ->addSelect('variant')
            ->addSelect('image')
            ->addSelect('mainTaxon')
            ->leftJoin('o.translations', 'translation', Join::WITH, 'translation.locale = :localeCode')
            ->leftJoin('o.variants', 'variant', Join::WITH, 'variant.position = 0')
            ->leftJoin('variant.images', 'image', Join::WITH, 'image.main = :main')
            ->leftJoin('o.mainTaxon', 'mainTaxon')
            ->addGroupBy('o.id')
            ->setParameter('localeCode', $localeCode)
            ->setParameter('main', true);

        if ($onlyPublished) {
            $queryBuilder
                ->andWhere('o.status = :published')
                ->setParameter('published', Product::PUBLISHED);
        }

        if (!empty($criteria['releasedAtFrom'])) {
            $dateCalculator = new DateCalculator();
            $queryBuilder
                ->andWhere($queryBuilder->expr()->gte('variant.releasedAt', ':releasedAtFrom'))
                ->setParameter('releasedAtFrom', $dateCalculator->getDay($criteria['releasedAtFrom']));
        }

        if (!empty($criteria['releasedAtTo'])) {
            $dateCalculator = new DateCalculator();
            $queryBuilder
                ->andWhere($queryBuilder->expr()->lte('variant.releasedAt', ':releasedAtTo'))
                ->setParameter('releasedAtTo', $dateCalculator->getDay($criteria['releasedAtTo']));
        }

        return $queryBuilder;
    }

    /**
     * {@inheritdoc}
     */
    public function createQueryBuilderWithLocaleCode($localeCode)
    {
        $queryBuilder = $this->createQueryBuilder('o');

        $queryBuilder
            ->addSelect('translation')
            ->leftJoin('o.translations', 'translation')
            ->andWhere('translation.locale = :localeCode')
            ->setParameter('localeCode', $localeCode);

        return $queryBuilder;
    }

    /**
     * @return QueryBuilder
     */
    protected function getQueryBuilder()
    {
        return parent::createQueryBuilder('o')
            ->select('o, option, variant, image, translation, mainTaxon')
            ->leftJoin('o.translations', 'translation')
            ->leftJoin('o.options', 'option')
            ->leftJoin('o.variants', 'variant')
            ->leftJoin('variant.images', 'image')
            ->leftJoin('o.mainTaxon', 'mainTaxon');
    }

    /**
     * @param string $slug
     *
     * @return ProductInterface|null
     */
    public function findOneBySlug($slug)
    {
        return $this->createQueryBuilder('o')
            ->leftJoin('o.translations', 'translation')
            ->andWhere('o.enabled = true')
            ->andWhere('translation.slug = :slug')
            ->setParameter('slug', $slug)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * {@inheritdoc}
     */
    public function findOneById($id)
    {
        return $this->getQueryBuilder()<commit_msg>fix main image on partial product list<commit_after><?php
/**
 * Created by PhpStorm.
 * User: loic
 * Date: 09/03/2016
 * Time: 09:33
 */

namespace AppBundle\Repository;

use AppBundle\Entity\Product;
use AppBundle\Utils\DateCalculator;
use Doctrine\ORM\Query\Expr\Join;
use Doctrine\ORM\QueryBuilder;
use Pagerfanta\Adapter\DoctrineORMAdapter;
use Pagerfanta\Pagerfanta;
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository as BaseProductRepository;
use Sylius\Component\Product\Model\ProductInterface;
use Sylius\Component\Taxonomy\Model\TaxonInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class ProductRepository extends BaseProductRepository
{
    /**
     * @param $localeCode
     * @param bool $onlyPublished
     * @param array $criteria
     *
     * @return QueryBuilder
     */
    public function createListQueryBuilder($localeCode, $onlyPublished=true, array $criteria = [])
    {
        $queryBuilder = $this->createQueryBuilder('o');

        $queryBuilder
            ->addSelect('translation')
            ->addSelect('variant')
            ->addSelect('image')
            ->addSelect('mainTaxon')
            ->leftJoin('o.translations', 'translation', Join::WITH, 'translation.locale = :localeCode')
            ->leftJoin('o.variants', 'variant', Join::WITH, 'variant.position = 0')
            ->leftJoin('variant.images', 'image', Join::WITH, 'image.main = :main')
            ->leftJoin('o.mainTaxon', 'mainTaxon')
            ->addGroupBy('o.id')
            ->setParameter('localeCode', $localeCode)
            ->setParameter('main', true);

        if ($onlyPublished) {
            $queryBuilder
                ->andWhere('o.status = :published')
                ->setParameter('published', Product::PUBLISHED);
        }

        if (!empty($criteria['releasedAtFrom'])) {
            $dateCalculator = new DateCalculator();
            $queryBuilder
                ->andWhere($queryBuilder->expr()->gte('variant.releasedAt', ':releasedAtFrom'))
                ->setParameter('releasedAtFrom', $dateCalculator->getDay($criteria['releasedAtFrom']));
        }

        if (!empty($criteria['releasedAtTo'])) {
            $dateCalculator = new DateCalculator();
            $queryBuilder
                ->andWhere($queryBuilder->expr()->lte('variant.releasedAt', ':releasedAtTo'))
                ->setParameter('releasedAtTo', $dateCalculator->getDay($criteria['releasedAtTo']));
        }

        return $queryBuilder;
    }

    /**
     * {@inheritdoc}
     */
    public function createQueryBuilderWithLocaleCode($localeCode)
    {
        $queryBuilder = $this->createQueryBuilder('o');

        $queryBuilder
            ->addSelect('translation')
            ->leftJoin('o.translations', 'translation')
            ->andWhere('translation.locale = :localeCode')
            ->setParameter('localeCode', $localeCode);

        return $queryBuilder;
    }

    /**
     * @return QueryBuilder
     */
    protected function getQueryBuilder()
    {
        return parent::createQueryBuilder('o')
            ->select('o, option, variant, image, translation, mainTaxon')
            ->leftJoin('o.translations', 'translation')
            ->leftJoin('o.options', 'option')
            ->leftJoin('o.variants', 'variant')
            ->leftJoin('variant.images', 'image', Join::WITH, 'image.main = :main')
            ->leftJoin('o.mainTaxon', 'mainTaxon')
            ->setParameter('main', true);
    }

    /**
     * @param string $slug
     *
     * @return ProductInterface|null
     */
    public function findOneBySlug($slug)
    {
        return $this->createQueryBuilder('o')
            ->leftJoin('o.translations', 'translation')
            ->andWhere('o.enabled = true')
            ->andWhere('translation.slug = :slug')
            ->setParameter('slug', $slug)
            ->getQuery()
            ->getOneOrNullResult();
    }

    /**
     * {@inheritdoc}
     */
    public function findOneById($id)
    {
        return $this->getQueryBuilder()
{'fraction_non_alphanumeric': 0.15103378719112456, 'fraction_numerical': 0.0032778618255168935, 'mean_word_length': 2.496253856324372, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6880217', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2184, 'n_words': 366}