diff --git a/Model/Resolver/CreditMemo/PrintCreditMemo.php b/Model/Resolver/CreditMemo/PrintCreditMemo.php index e2f84bc..de11ab4 100644 --- a/Model/Resolver/CreditMemo/PrintCreditMemo.php +++ b/Model/Resolver/CreditMemo/PrintCreditMemo.php @@ -2,6 +2,7 @@ namespace ScandiPWA\SalesGraphQl\Model\Resolver\CreditMemo; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -18,18 +19,23 @@ class PrintCreditMemo implements ResolverInterface /** * @var CreditmemoRepositoryInterface */ - protected $creditmemoRepository; + protected CreditmemoRepositoryInterface $creditmemoRepository; /** * @var OrderFormatter */ - protected $orderFormatter; + protected OrderFormatter $orderFormatter; /** * @var OrderViewAuthorizationInterface */ - protected $orderViewAuthorizationInterface; + protected OrderViewAuthorizationInterface $orderViewAuthorizationInterface; + /** + * @param CreditmemoRepositoryInterface $creditmemoRepository + * @param OrderFormatter $orderFormatter + * @param OrderViewAuthorizationInterface $orderViewAuthorizationInterface + */ public function __construct( CreditmemoRepositoryInterface $creditmemoRepository, OrderFormatter $orderFormatter, @@ -51,8 +57,13 @@ public function resolve( array $args = null ) { $customerId = $context->getUserId(); - $refund = $this->creditmemoRepository->get($args['refundId']); - $order = $refund->getOrder(); + + try { + $refund = $this->creditmemoRepository->get($args['refundId']); + $order = $refund->getOrder(); + } catch (NoSuchEntityException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } if (!$this->orderViewAuthorizationInterface->canView($order, $customerId)) { throw new GraphQlInputException(__('Current user is not allowed to print this order')); diff --git a/Model/Resolver/Invoice/PrintInvoice.php b/Model/Resolver/Invoice/PrintInvoice.php index 1d58de8..2da45bb 100644 --- a/Model/Resolver/Invoice/PrintInvoice.php +++ b/Model/Resolver/Invoice/PrintInvoice.php @@ -2,7 +2,7 @@ namespace ScandiPWA\SalesGraphQl\Model\Resolver\Invoice; -use Magento\Framework\Exception\LocalizedException; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -19,18 +19,23 @@ class PrintInvoice implements ResolverInterface /** * @var InvoiceRepositoryInterface */ - protected $invoiceRepository; + protected InvoiceRepositoryInterface $invoiceRepository; /** * @var OrderFormatter */ - protected $orderFormatter; + protected OrderFormatter $orderFormatter; /** * @var OrderViewAuthorizationInterface */ - protected $orderViewAuthorizationInterface; + protected OrderViewAuthorizationInterface $orderViewAuthorizationInterface; + /** + * @param InvoiceRepositoryInterface $invoiceRepository + * @param OrderFormatter $orderFormatter + * @param OrderViewAuthorizationInterface $orderViewAuthorizationInterface + */ public function __construct( InvoiceRepositoryInterface $invoiceRepository, OrderFormatter $orderFormatter, @@ -52,8 +57,13 @@ public function resolve( array $args = null ) { $customerId = $context->getUserId(); - $invoice = $this->invoiceRepository->get($args['invoiceId']); - $order = $invoice->getOrder(); + + try { + $invoice = $this->invoiceRepository->get($args['invoiceId']); + $order = $invoice->getOrder(); + } catch (NoSuchEntityException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } if (!$this->orderViewAuthorizationInterface->canView($order, $customerId)) { throw new GraphQlInputException(__('Current user is not allowed to print this order')); diff --git a/Model/Resolver/Invoices.php b/Model/Resolver/Invoices.php index 364db38..a66d8f9 100644 --- a/Model/Resolver/Invoices.php +++ b/Model/Resolver/Invoices.php @@ -9,7 +9,6 @@ use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Config\Element\Field; -use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\InvoiceInterface; @@ -41,7 +40,7 @@ public function resolve( /** @var InvoiceInterface $invoice */ foreach ($orderModel->getInvoiceCollection() as $invoice) { $invoices[] = [ - 'id' => base64_encode($invoice->getEntityId()), + 'id' => base64_encode((string) $invoice->getEntityId()), 'number' => $invoice['increment_id'], 'comments' => $this->getInvoiceComments($invoice), 'model' => $invoice, diff --git a/Model/Resolver/Shipment/PrintShipment.php b/Model/Resolver/Shipment/PrintShipment.php index bcf66c3..0fed421 100644 --- a/Model/Resolver/Shipment/PrintShipment.php +++ b/Model/Resolver/Shipment/PrintShipment.php @@ -2,6 +2,7 @@ namespace ScandiPWA\SalesGraphQl\Model\Resolver\Shipment; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Query\ResolverInterface; @@ -18,18 +19,23 @@ class PrintShipment implements ResolverInterface /** * @var ShipmentRepositoryInterface */ - protected $shipmentRepository; + protected ShipmentRepositoryInterface $shipmentRepository; /** * @var OrderFormatter */ - protected $orderFormatter; + protected OrderFormatter $orderFormatter; /** * @var OrderViewAuthorizationInterface */ - protected $orderViewAuthorizationInterface; + protected OrderViewAuthorizationInterface $orderViewAuthorizationInterface; + /** + * @param ShipmentRepositoryInterface $shipmentRepository + * @param OrderFormatter $orderFormatter + * @param OrderViewAuthorizationInterface $orderViewAuthorizationInterface + */ public function __construct( ShipmentRepositoryInterface $shipmentRepository, OrderFormatter $orderFormatter, @@ -51,8 +57,13 @@ public function resolve( array $args = null ) { $customerId = $context->getUserId(); - $shipment = $this->shipmentRepository->get($args['shipmentId']); - $order = $shipment->getOrder(); + + try { + $shipment = $this->shipmentRepository->get($args['shipmentId']); + $order = $shipment->getOrder(); + } catch (NoSuchEntityException $e) { + throw new GraphQlInputException(__($e->getMessage())); + } if (!$this->orderViewAuthorizationInterface->canView($order, $customerId)) { throw new GraphQlInputException(__('Current user is not allowed to print this order')); diff --git a/Model/Resolver/Shipments.php b/Model/Resolver/Shipments.php index 4ea8cea..bcc0f97 100644 --- a/Model/Resolver/Shipments.php +++ b/Model/Resolver/Shipments.php @@ -11,7 +11,7 @@ use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Sales\Api\Data\ShipmentInterface; -use Magento\Sales\Model\Order; +use Magento\Sales\Api\Data\OrderInterface; use Magento\SalesGraphQl\Model\Resolver\Shipments as BaseShipments; /** @@ -24,11 +24,11 @@ class Shipments extends BaseShipments */ public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) { - if (!isset($value['model']) && !($value['model'] instanceof Order)) { + if (!isset($value['model']) && !($value['model'] instanceof OrderInterface)) { throw new LocalizedException(__('"model" value should be specified')); } - /** @var Order $order */ + /** @var OrderInterface $order */ $order = $value['model']; $shipments = $order->getShipmentsCollection()->getItems(); @@ -41,7 +41,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value foreach ($shipments as $shipment) { $orderShipments[] = [ - 'id' => base64_encode($shipment->getEntityId()), + 'id' => base64_encode((string) $shipment->getEntityId()), 'number' => $shipment->getIncrementId(), 'comments' => $this->getShipmentComments($shipment), 'model' => $shipment, diff --git a/etc/schema.graphqls b/etc/schema.graphqls index 04a4fed..6fa855b 100644 --- a/etc/schema.graphqls +++ b/etc/schema.graphqls @@ -8,9 +8,9 @@ */ type Query { - orderByInvoice (invoiceId: Int): CustomerOrder @resolver(class: "ScandiPWA\\SalesGraphQl\\Model\\Resolver\\Invoice\\PrintInvoice") - orderByShipment (shipmentId: Int): CustomerOrder @resolver(class: "ScandiPWA\\SalesGraphQl\\Model\\Resolver\\Shipment\\PrintShipment") - orderByRefund (refundId: Int): CustomerOrder @resolver(class: "ScandiPWA\\SalesGraphQl\\Model\\Resolver\\CreditMemo\\PrintCreditMemo") + orderByInvoice (invoiceId: Int!): CustomerOrder @resolver(class: "ScandiPWA\\SalesGraphQl\\Model\\Resolver\\Invoice\\PrintInvoice") + orderByShipment (shipmentId: Int!): CustomerOrder @resolver(class: "ScandiPWA\\SalesGraphQl\\Model\\Resolver\\Shipment\\PrintShipment") + orderByRefund (refundId: Int!): CustomerOrder @resolver(class: "ScandiPWA\\SalesGraphQl\\Model\\Resolver\\CreditMemo\\PrintCreditMemo") } input CustomerOrdersFilterInput @doc(description: "Identifies the filter to use for filtering orders.") {