| Total Complexity | 5 |
| Total Lines | 39 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | use function assert; |
||
| 13 | |||
| 14 | final class DoctrineCartRepository implements CartRepository |
||
| 15 | { |
||
| 16 | 10 | public function __construct(private EntityManager $entityManger) |
|
|
|
|||
| 17 | { |
||
| 18 | 10 | } |
|
| 19 | 10 | ||
| 20 | public function add(Cart $cart): void |
||
| 21 | 7 | { |
|
| 22 | $this->entityManger->persist($cart); |
||
| 23 | 7 | } |
|
| 24 | 7 | ||
| 25 | public function get(string $id): Cart |
||
| 26 | 9 | { |
|
| 27 | $queryBuilder = $this->entityManger->createQueryBuilder(); |
||
| 28 | 9 | $queryBuilder |
|
| 29 | ->select('cart, items') |
||
| 30 | 9 | ->from(Cart::class, 'cart') |
|
| 31 | 9 | ->leftJoin('cart.items', 'items') |
|
| 32 | 9 | ->where('cart.id = :id') |
|
| 33 | 9 | ->setParameter(':id', $id); |
|
| 34 | 9 | ||
| 35 | $query = $queryBuilder->getQuery(); |
||
| 36 | 9 | ||
| 37 | try { |
||
| 38 | $cart = $query->getSingleResult(); |
||
| 39 | 9 | assert($cart instanceof Cart); |
|
| 40 | 7 | return $cart; |
|
| 41 | 7 | } catch (NoResultException) { |
|
| 42 | 3 | throw new CartNotFoundException(); |
|
| 43 | 3 | } |
|
| 44 | } |
||
| 45 | |||
| 46 | public function remove(string $id): void |
||
| 47 | 3 | { |
|
| 48 | $cart = $this->get($id); |
||
| 49 | 3 | $this->entityManger->remove($cart); |
|
| 50 | 2 | } |
|
| 51 | } |
||
| 52 |