CheckoutAddressProvider.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace Webkul\BagistoApi\State;
  3. use ApiPlatform\Metadata\Operation;
  4. use ApiPlatform\State\ProviderInterface;
  5. use Illuminate\Support\Facades\Request;
  6. use Webkul\BagistoApi\Dto\CheckoutAddressOutput;
  7. use Webkul\BagistoApi\Exception\AuthenticationException;
  8. use Webkul\BagistoApi\Exception\ResourceNotFoundException;
  9. use Webkul\BagistoApi\Facades\CartTokenFacade;
  10. use Webkul\BagistoApi\Facades\TokenHeaderFacade;
  11. /**
  12. * Provides address data from BagistoApi queries.
  13. */
  14. class CheckoutAddressProvider implements ProviderInterface
  15. {
  16. public function __construct() {}
  17. /**
  18. * Provide address data from BagistoApi context.
  19. */
  20. public function provide(
  21. Operation $operation,
  22. array $uriVariables = [],
  23. array $context = []
  24. ): object|array|null {
  25. $request = Request::instance() ?? ($context['request'] ?? null);
  26. // Extract Bearer token from Authorization header
  27. $token = $request ? TokenHeaderFacade::getAuthorizationBearerToken($request) : null;
  28. if (! $token) {
  29. throw new AuthenticationException(__('bagistoapi::app.graphql.cart.authentication-required'));
  30. }
  31. $cart = CartTokenFacade::getCartByToken($token);
  32. if (! $cart) {
  33. throw new ResourceNotFoundException(__('bagistoapi::app.graphql.cart.invalid-token'));
  34. }
  35. return $this->buildAddressOutput($cart);
  36. }
  37. /**
  38. * Build address output from cart.
  39. */
  40. private function buildAddressOutput($cart): CheckoutAddressOutput
  41. {
  42. $output = new CheckoutAddressOutput;
  43. $output->id = $cart->id;
  44. $output->cartToken = $cart->guest_cart_token ?? $cart->customer_id;
  45. $output->customerId = $cart->customer_id;
  46. $billingAddress = $cart->billing_address;
  47. if ($billingAddress) {
  48. $output->billingFirstName = $billingAddress->first_name;
  49. $output->billingLastName = $billingAddress->last_name;
  50. $output->billingEmail = $billingAddress->email;
  51. $output->billingCompanyName = $billingAddress->company_name;
  52. $output->billingAddress = $billingAddress->address;
  53. $output->billingCountry = $billingAddress->country;
  54. $output->billingState = $billingAddress->state;
  55. $output->billingCity = $billingAddress->city;
  56. $output->billingPostcode = $billingAddress->postcode;
  57. $output->billingPhoneNumber = $billingAddress->phone;
  58. $output->billingAddressId = $billingAddress->id;
  59. }
  60. $shippingAddress = $cart->shipping_address;
  61. if ($shippingAddress) {
  62. $output->shippingFirstName = $shippingAddress->first_name;
  63. $output->shippingLastName = $shippingAddress->last_name;
  64. $output->shippingEmail = $shippingAddress->email;
  65. $output->shippingCompanyName = $shippingAddress->company_name;
  66. $output->shippingAddress = $shippingAddress->address;
  67. $output->shippingCountry = $shippingAddress->country;
  68. $output->shippingState = $shippingAddress->state;
  69. $output->shippingCity = $shippingAddress->city;
  70. $output->shippingPostcode = $shippingAddress->postcode;
  71. $output->shippingPhoneNumber = $shippingAddress->phone;
  72. $output->shippingAddressId = $shippingAddress->id;
  73. }
  74. $output->success = true;
  75. $output->message = __('bagistoapi::app.graphql.address.retrieved');
  76. return $output;
  77. }
  78. }