Skip to content

Commit

Permalink
Add missing docs, use class types, wrap load into try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksandrsKondratjevs committed Feb 14, 2022
1 parent fc2cada commit 98da9a3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 25 deletions.
21 changes: 16 additions & 5 deletions Model/Resolver/CreditMemo/PrintCreditMemo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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'));
Expand Down
22 changes: 16 additions & 6 deletions Model/Resolver/Invoice/PrintInvoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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'));
Expand Down
3 changes: 1 addition & 2 deletions Model/Resolver/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 16 additions & 5 deletions Model/Resolver/Shipment/PrintShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -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'));
Expand Down
8 changes: 4 additions & 4 deletions Model/Resolver/Shipments.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();

Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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.") {
Expand Down

0 comments on commit 98da9a3

Please sign in to comment.