<commit_before><?php

/*
 * This file is part of the OpenMiamMiam project.
 *
 * (c) Isics <<EMAIL>>
 *
 * This source file is subject to the AGPL v3 license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Isics\Bundle\OpenMiamMiamBundle\Entity\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Association;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Branch;
use Isics\Bundle\OpenMiamMiamBundle\Entity\BranchOccurrence;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Category;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Producer;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Product;

class ProductRepository extends EntityRepository
{
    /**
     * Finds products visible in a branch and a category
     *
     * @param Branch   $branch   Branch
     * @param Category $category Category
     *
     * @return array
     */
    public function findAllVisibleInBranchAndCategory(Branch $branch, Category $category)
    {
        return $this->createQueryBuilder('p')
            ->addSelect('pr')
            ->innerJoin('p.branches', 'b')
            ->innerJoin('p.producer', 'pr')
            ->where('p.availability != :availability')
            ->andWhere('b = :branch')
            ->andWhere('p.category = :category')
            ->addOrderBy('p.name')
            ->setParameter('availability', Product::AVAILABILITY_UNAVAILABLE)
            ->setParameter('branch', $branch)
            ->setParameter('category', $category)
            ->getQuery()
            ->getResult();
    }

    /**
     * Finds a product visible in a branch by its id
     *
     * @param integer $id     id
     * @param Branch  $branch Branch
     *
     * @return Product|null
     */
    public function findOneByIdAndVisibleInBranch($id, Branch $branch)
    {
        try {
            return $this->createQueryBuilder('p')
                ->addSelect('pr')
                ->innerJoin('p.branches', 'b')
                ->innerJoin('p.producer', 'pr')
                ->where('p.id = :id')
                ->andwhere('p.availability != :availability')
                ->andWhere('b = :branch')
                ->addOrderBy('p.name')
                ->setParameter('id', $id)
                ->setParameter('availability', Product::AVAILABILITY_UNAVAILABLE)
                ->setParameter('branch', $branch)
                ->getQuery()
                ->getSingleResult();

        } catch (\Doctrine\ORM\NoResultException $e) {

            return null;
        }
    }

    /**
     * Finds products of the moment of a branch
     *
     * @param BranchOccurrence $branchOccurrence Branch occurrence
     * @param integer          $limit            Limit
     *
     * @return array
     */
    public function findOfTheMomentForBranchOccurrence(BranchOccurrence $branchOccurrence, $limit = 3)
    {
        // Retrieves all products of the moment ids and producer ids
        $qb = $this->filterAvailableForBranchOccurrence($branchOccurrence);
        $productsIds = $qb
            ->select('p.id as product_id, pr.id as producer_id')
            ->innerJoin('p.branches', 'b', Expr\Join::WITH, $qb->expr()->eq('b', ':branch'))
            ->andWhere('p.isOfTheMoment = true')
            ->setParameter('branch', $branchOccurrence->getBranch())
            ->getQuery()
            ->getResult();

        if (empty($productsIds)) {
            return array();
        }

        // Groups products by producer
        $productsIdsByProducer = array();
        foreach ($productsIds as $productIds) {
            if (!array_key_exists($productIds['producer_id'], $productsIdsByProducer)) {
                $productsIdsByProducer[$productIds['producer_id']] = array();
            }
            $productsIdsByProducer[$productIds['producer_id']][] = $productIds['product_id'];
        }<commit_msg>Remove products of the moment that have no images<commit_after><?php

/*
 * This file is part of the OpenMiamMiam project.
 *
 * (c) Isics <<EMAIL>>
 *
 * This source file is subject to the AGPL v3 license that is bundled
 * with this source code in the file LICENSE.
 */

namespace Isics\Bundle\OpenMiamMiamBundle\Entity\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\QueryBuilder;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Association;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Branch;
use Isics\Bundle\OpenMiamMiamBundle\Entity\BranchOccurrence;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Category;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Producer;
use Isics\Bundle\OpenMiamMiamBundle\Entity\Product;

class ProductRepository extends EntityRepository
{
    /**
     * Finds products visible in a branch and a category
     *
     * @param Branch   $branch   Branch
     * @param Category $category Category
     *
     * @return array
     */
    public function findAllVisibleInBranchAndCategory(Branch $branch, Category $category)
    {
        return $this->createQueryBuilder('p')
            ->addSelect('pr')
            ->innerJoin('p.branches', 'b')
            ->innerJoin('p.producer', 'pr')
            ->where('p.availability != :availability')
            ->andWhere('b = :branch')
            ->andWhere('p.category = :category')
            ->addOrderBy('p.name')
            ->setParameter('availability', Product::AVAILABILITY_UNAVAILABLE)
            ->setParameter('branch', $branch)
            ->setParameter('category', $category)
            ->getQuery()
            ->getResult();
    }

    /**
     * Finds a product visible in a branch by its id
     *
     * @param integer $id     id
     * @param Branch  $branch Branch
     *
     * @return Product|null
     */
    public function findOneByIdAndVisibleInBranch($id, Branch $branch)
    {
        try {
            return $this->createQueryBuilder('p')
                ->addSelect('pr')
                ->innerJoin('p.branches', 'b')
                ->innerJoin('p.producer', 'pr')
                ->where('p.id = :id')
                ->andwhere('p.availability != :availability')
                ->andWhere('b = :branch')
                ->addOrderBy('p.name')
                ->setParameter('id', $id)
                ->setParameter('availability', Product::AVAILABILITY_UNAVAILABLE)
                ->setParameter('branch', $branch)
                ->getQuery()
                ->getSingleResult();

        } catch (\Doctrine\ORM\NoResultException $e) {

            return null;
        }
    }

    /**
     * Finds products of the moment of a branch
     *
     * @param BranchOccurrence $branchOccurrence Branch occurrence
     * @param integer          $limit            Limit
     *
     * @return array
     */
    public function findOfTheMomentForBranchOccurrence(BranchOccurrence $branchOccurrence, $limit = 3)
    {
        // Retrieves all products of the moment ids and producer ids
        $qb = $this->filterAvailableForBranchOccurrence($branchOccurrence);
        $productsIds = $qb
            ->select('p.id as product_id, pr.id as producer_id')
            ->innerJoin('p.branches', 'b', Expr\Join::WITH, $qb->expr()->eq('b', ':branch'))
            ->andWhere('p.isOfTheMoment = true')
            ->andWhere('p.image IS NOT NULL')
            ->setParameter('branch', $branchOccurrence->getBranch())
            ->getQuery()
            ->getResult();

        if (empty($productsIds)) {
            return array();
        }

        // Groups products by producer
        $productsIdsByProducer = array();
        foreach ($productsIds as $productIds) {
            if (!array_key_exists($productIds['producer_id'], $productsIdsByProducer)) {
                $productsIdsByProducer[$productIds['producer_id']] = array();
            }
            $productsIdsByProducer[$productIds['producer_id']][] = $productIds['product_id'];
        }
{'fraction_non_alphanumeric': 0.12711436505932847, 'fraction_numerical': 0.0005049229992426155, 'mean_word_length': 2.280745341614907, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 80, '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': '2499654', 'n_tokens_mistral': 2268, 'n_tokens_neox': 2228, 'n_words': 480}