<?php

namespace App\Repository;

use App\Entity\Product;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

/**
 * @method Product|null find($id, $lockMode = null, $lockVersion = null)
 * @method Product|null findOneBy(array $criteria, array $orderBy = null)
 * @method Product[]    findAll()
 * @method Product[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class ProductRepository extends ServiceEntityRepository
{
    use Pageable;

    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Product::class);
    }

    public function getFilteredProducts($keyword = "", $page)
    {
        $query = $this->getEntityManager()->createQuery('
            SELECT p
            FROM App\Entity\Product p
            WHERE p.name LIKE :keyword
            OR p.description LIKE :keyword
        ');
        $query->setParameter('keyword', "%$keyword%");

        return $this->paginate($query, $page);
    }
}
