getBasketId(); return $this->repository->find($basketId) ?? new Basket($basketId, []); } public function add(ProductId $productId, int $qty = 1): void { $basketProduct = $this->productRepository->getBasketProduct($productId, $qty); $basket = $this->getBasket(); $basket->addOrUpdate($basketProduct); $this->repository->persist($basket); } public function remove(ProductId $productId, int $qty = 1): void { $basket = $this->getBasket(); $basket->remove($productId, $qty); $this->repository->persist($basket); } public function removeLine(ProductId $productId): void { $basket = $this->getBasket(); $basket->removeLine($productId); $this->repository->persist($basket); } public function revalidateBasket(Basket $basket): Basket { $refreshedItems = []; foreach ($basket->items() as $item) { $refreshedItems[] = $this->productRepository->getBasketProduct($item->id, $item->qty); } $refreshedBasket = new Basket($basket->id, $refreshedItems); $this->repository->persist($refreshedBasket); return $refreshedBasket; } public function empty(): void { $this->repository->delete($this->getBasket()); } private function getBasketId(): BasketId { $sessionId = $this->sessionService->get(); return new BasketId($sessionId->value); } }