|
9 | 9 |
|
10 | 10 | use Magento\Framework\Exception\NoSuchEntityException;
|
11 | 11 | use Magento\Framework\GraphQl\Config\Element\Field;
|
12 |
| -use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
13 | 12 | use Magento\Framework\GraphQl\Query\ResolverInterface;
|
14 | 13 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
15 | 14 | use Magento\QuoteGraphQl\Model\Cart\CreateEmptyCartForCustomer;
|
16 |
| -use Magento\Authorization\Model\UserContextInterface; |
| 15 | +use Magento\GraphQl\Model\Query\ContextInterface; |
| 16 | +use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException; |
17 | 17 | use Magento\Quote\Api\CartManagementInterface;
|
18 | 18 |
|
19 | 19 | /**
|
@@ -49,35 +49,20 @@ public function __construct(
|
49 | 49 | public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
|
50 | 50 | {
|
51 | 51 | $currentUserId = $context->getUserId();
|
52 |
| - $currentUserType = $context->getUserType(); |
53 |
| - $isCustomerLoggedIn = $this->isCustomer($currentUserId, $currentUserType); |
54 | 52 |
|
55 |
| - if ($isCustomerLoggedIn) { |
56 |
| - try { |
57 |
| - $cart = $this->cartManagement->getCartForCustomer($currentUserId); |
58 |
| - } catch (NoSuchEntityException $e) { |
59 |
| - $this->createEmptyCartForCustomer->execute($currentUserId, null); |
60 |
| - $cart = $this->cartManagement->getCartForCustomer($currentUserId); |
61 |
| - } |
62 |
| - |
63 |
| - } else { |
64 |
| - throw new GraphQlInputException(__('The request is allowed for logged in customer')); |
| 53 | + /** @var ContextInterface $context */ |
| 54 | + if (false === $context->getExtensionAttributes()->getIsCustomer()) { |
| 55 | + throw new GraphQlAuthorizationException(__('The request is allowed for logged in customer')); |
| 56 | + } |
| 57 | + try { |
| 58 | + $cart = $this->cartManagement->getCartForCustomer($currentUserId); |
| 59 | + } catch (NoSuchEntityException $e) { |
| 60 | + $this->createEmptyCartForCustomer->execute($currentUserId, null); |
| 61 | + $cart = $this->cartManagement->getCartForCustomer($currentUserId); |
65 | 62 | }
|
66 | 63 |
|
67 | 64 | return [
|
68 | 65 | 'model' => $cart
|
69 | 66 | ];
|
70 | 67 | }
|
71 |
| - |
72 |
| - /** |
73 |
| - * Check if current user is logged in |
74 |
| - * |
75 |
| - * @param int|null $customerId |
76 |
| - * @param int|null $customerType |
77 |
| - * @return bool |
78 |
| - */ |
79 |
| - private function isCustomer(?int $customerId, ?int $customerType): bool |
80 |
| - { |
81 |
| - return !empty($customerId) && !empty($customerType) && $customerType !== UserContextInterface::USER_TYPE_GUEST; |
82 |
| - } |
83 | 68 | }
|
0 commit comments