<?php
namespace App\Repository;
use App\Entity\Cart;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use App\Exception\CartException;
final class CartRepository
{
/**
* @var EntityRepository
*/
private $repository;
public function __construct(EntityManagerInterface $entityManager)
{
$this->repository = $entityManager->getRepository(Cart::class);
}
public function getAll($locale = 'pl') {
$q = $this->repository->createQueryBuilder('p');
return $q;
}
/**
* @return \Doctrine\Common\Persistence\ObjectRepository|EntityRepository
*/
public function getRepository() {
return $this->repository;
}
/**
* @param $session
* @return Cart
* @throws CartException
*/
public function find($session) {
/** @var $cart Cart */
$cart = $this->getRepository()->findOneBy([
'session' => $session,
]);
if ($cart instanceof Cart) {
return $cart;
}
throw new CartException("Cart not found: ".$session);
}
}