maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId; $this->cartRepository = $cartRepository; } /** * Get cart for user * * @param string $cartHash * @param int|null $userId * @return Quote * @throws GraphQlAuthorizationException * @throws GraphQlNoSuchEntityException */ public function execute(string $cartHash, ?int $userId): Quote { try { $cartId = $this->maskedQuoteIdToQuoteId->execute($cartHash); } catch (NoSuchEntityException $exception) { throw new GraphQlNoSuchEntityException( __('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash]) ); } try { /** @var Quote $cart */ $cart = $this->cartRepository->get($cartId); } catch (NoSuchEntityException $e) { throw new GraphQlNoSuchEntityException( __('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $cartHash]) ); } $customerId = (int)$cart->getCustomerId(); /* Guest cart, allow operations */ if (!$customerId) { return $cart; } if ($customerId !== $userId) { throw new GraphQlAuthorizationException( __( 'The current user cannot perform operations on cart "%masked_cart_id"', ['masked_cart_id' => $cartHash] ) ); } return $cart; } }