From 7c499aa0bdd198ca786c7ca236ea0df60a1d6a8f Mon Sep 17 00:00:00 2001 From: Tyrone Tudehope Date: Tue, 5 Nov 2024 10:34:31 +0200 Subject: [PATCH] More improvements to models to that we can add logic later - Removed unused imports and commented-out code in Plugin.php - Changed public properties to private - Added getter and setter methods for encapsulated properties in models - Updated Xml service to use new property accessors --- src/Plugin.php | 44 +------- src/events/OrderEvent.php | 2 +- src/models/Address.php | 120 ++++++++++++++++++++-- src/models/Customer.php | 36 ++++++- src/models/Item.php | 118 +++++++++++++++++++-- src/models/Option.php | 24 ++++- src/models/Order.php | 210 ++++++++++++++++++++++++++++++++------ src/models/Orders.php | 32 +++++- src/services/Xml.php | 8 +- 9 files changed, 487 insertions(+), 107 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 6aede6d..4e4fcb1 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -7,7 +7,6 @@ use craft\events\RegisterUrlRulesEvent; use craft\events\RegisterUserPermissionsEvent; use craft\services\UserPermissions; -use craft\web\Application; use craft\web\UrlManager; use fostercommerce\shipstationconnect\models\Settings; use fostercommerce\shipstationconnect\services\Xml; @@ -26,44 +25,16 @@ class Plugin extends \craft\base\Plugin public string $schemaVersion = '1.0.1'; - /** - * init. - * - * @author Unknown - * @since v0.0.1 - * @version v1.0.0 Monday, May 23rd, 2022. - * @access public - */ public function init(): void { parent::init(); $this->setComponents([ - 'xml' => \fostercommerce\shipstationconnect\services\Xml::class, + 'xml' => Xml::class, ]); Craft::$app->view->registerTwigExtension(new IsFieldTypeFilter()); - // Because Shipstation uses a querystring parameter of 'action' in their requests. - // This interferes with Craft's routing system. - // So we intercept the request and rename that parameter to ssaction IF the request is for one of this plugin's controllers - /* - Craft::$app->on(Application::EVENT_INIT, function() { - $request = Craft::$app->request; - if(!$request->isConsoleRequest){ - if(in_array('actions', $request->getSegments()) && in_array('shipstationconnect', $request->getSegments())) { - if(array_key_exists('action', $request->getQueryParams())) { - // rename array key to match the action name - $params = $request->getQueryParams(); - $params['ssaction'] = $params['action']; - unset($params['action']); - $request->setQueryParams($params); - } - }; - } - }); - */ - Event::on( UrlManager::class, UrlManager::EVENT_REGISTER_CP_URL_RULES, @@ -141,25 +112,16 @@ protected function settingsHtml(): ?string protected function beforeInstall(): void { - if (! Craft::$app->plugins->isPluginInstalled('commerce')) { - Craft::error(Craft::t( - 'shipstationconnect', - 'Failed to install. Craft Commerce is required.' - )); - // return false; - } - - if (! Craft::$app->plugins->isPluginEnabled('commerce')) { + if (! Craft::$app->plugins->isPluginInstalled('commerce') || ! Craft::$app->plugins->isPluginEnabled('commerce')) { Craft::error(Craft::t( 'shipstationconnect', 'Failed to install. Craft Commerce is required.' )); - // return false; } } protected function createSettingsModel(): ?Model { - return new \fostercommerce\shipstationconnect\models\Settings(); + return new Settings(); } } diff --git a/src/events/OrderEvent.php b/src/events/OrderEvent.php index 1df3e40..a0ad9a9 100644 --- a/src/events/OrderEvent.php +++ b/src/events/OrderEvent.php @@ -10,5 +10,5 @@ class OrderEvent extends Event /** * The order that has been transformed into a format ready to be exported to ShipStation */ - public Order $transformedOrder; + public Order $order; } diff --git a/src/models/Address.php b/src/models/Address.php index e447eb8..1e94d7a 100644 --- a/src/models/Address.php +++ b/src/models/Address.php @@ -12,43 +12,143 @@ class Address extends Base { #[Groups(['export'])] #[SerializedName('Company')] - public ?string $company = null; + private ?string $company = null; #[Groups(['export'])] #[SerializedName('Address1')] - public ?string $address1 = null; + private ?string $address1 = null; #[Groups(['export'])] #[SerializedName('Address2')] - public ?string $address2 = null; + private ?string $address2 = null; #[Groups(['export'])] #[SerializedName('City')] - public ?string $city = null; + private ?string $city = null; #[Groups(['export'])] #[SerializedName('State')] - public ?string $state = null; + private ?string $state = null; #[Groups(['export'])] #[SerializedName('PostalCode')] - public ?string $postalCode = null; + private ?string $postalCode = null; #[Groups(['export'])] #[SerializedName('Country')] - public string $country; + private string $country; #[Groups(['export'])] #[SerializedName('Name')] - public string $name; + private string $name; #[Groups(['export'])] #[SerializedName('Phone')] - public ?string $phone = null; + private ?string $phone = null; #[Groups(['export'])] #[SerializedName('Email')] - public string $email; + private string $email; + + public function getCompany(): ?string + { + return $this->company; + } + + public function setCompany(?string $company): void + { + $this->company = $company; + } + + public function getAddress1(): ?string + { + return $this->address1; + } + + public function setAddress1(?string $address1): void + { + $this->address1 = $address1; + } + + public function getAddress2(): ?string + { + return $this->address2; + } + + public function setAddress2(?string $address2): void + { + $this->address2 = $address2; + } + + public function getCity(): ?string + { + return $this->city; + } + + public function setCity(?string $city): void + { + $this->city = $city; + } + + public function getState(): ?string + { + return $this->state; + } + + public function setState(?string $state): void + { + $this->state = $state; + } + + public function getPostalCode(): ?string + { + return $this->postalCode; + } + + public function setPostalCode(?string $postalCode): void + { + $this->postalCode = $postalCode; + } + + public function getCountry(): string + { + return $this->country; + } + + public function setCountry(string $country): void + { + $this->country = $country; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getPhone(): ?string + { + return $this->phone; + } + + public function setPhone(?string $phone): void + { + $this->phone = $phone; + } + + public function getEmail(): string + { + return $this->email; + } + + public function setEmail(string $email): void + { + $this->email = $email; + } public static function fromCommerceAddress(CommerceOrder $commerceOrder, ?CraftAddress $commerceAddress): ?self { diff --git a/src/models/Customer.php b/src/models/Customer.php index 0cad0ac..462ca54 100644 --- a/src/models/Customer.php +++ b/src/models/Customer.php @@ -13,15 +13,45 @@ class Customer extends Base { #[Groups(['export'])] #[SerializedName('CustomerCode')] - public string $customerCode; + private string $customerCode; #[Groups(['export'])] #[SerializedName('BillTo')] - public ?Address $billToAddress = null; + private ?Address $billToAddress = null; #[Groups(['export'])] #[SerializedName('ShipTo')] - public ?Address $shipToAddress = null; + private ?Address $shipToAddress = null; + + public function getCustomerCode(): string + { + return $this->customerCode; + } + + public function setCustomerCode(string $customerCode): void + { + $this->customerCode = $customerCode; + } + + public function getBillToAddress(): ?Address + { + return $this->billToAddress; + } + + public function setBillToAddress(?Address $billToAddress): void + { + $this->billToAddress = $billToAddress; + } + + public function getShipToAddress(): ?Address + { + return $this->shipToAddress; + } + + public function setShipToAddress(?Address $shipToAddress): void + { + $this->shipToAddress = $shipToAddress; + } /** * @return array> diff --git a/src/models/Item.php b/src/models/Item.php index b8c7b9c..52915b7 100644 --- a/src/models/Item.php +++ b/src/models/Item.php @@ -19,48 +19,144 @@ class Item extends Base { #[Groups(['export'])] #[SerializedName('SKU')] - public string $sku; + private string $sku; #[Groups(['export'])] #[SerializedName('Name')] - public string $name; + private string $name; #[Groups(['export'])] #[SerializedName('Weight')] - public float $weight; + private float $weight; #[Groups(['export'])] #[SerializedName('Quantity')] - public int $quantity; + private int $quantity; #[Groups(['export'])] #[SerializedName('UnitPrice')] - public float $unitPrice; + private float $unitPrice; #[Groups(['export'])] #[SerializedName('ImageUrl')] - public string $imageUrl = ''; + private ?string $imageUrl = ''; #[Groups(['export'])] #[SerializedName('WeightUnits')] - public string $weightUnits = ''; + private string $weightUnits = ''; #[Groups(['export'])] #[SerializedName('Adjustment')] - public bool $adjustment = false; + private bool $adjustment = false; /** * @var Option[] */ #[Groups(['export'])] #[SerializedName('Options')] - public array $options = []; + private array $options = []; - public static function asAdjustment(float $totalDiscount): self + public function getSku(): string + { + return $this->sku; + } + + public function setSku(string $sku): void + { + $this->sku = $sku; + } + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getWeight(): float + { + return $this->weight; + } + + public function setWeight(float $weight): void + { + $this->weight = $weight; + } + + public function getQuantity(): int + { + return $this->quantity; + } + + public function setQuantity(int $quantity): void + { + $this->quantity = $quantity; + } + + public function getUnitPrice(): float + { + return $this->unitPrice; + } + + public function setUnitPrice(float $unitPrice): void + { + $this->unitPrice = $unitPrice; + } + + public function getImageUrl(): ?string + { + return $this->imageUrl; + } + + public function setImageUrl(?string $imageUrl): void + { + $this->imageUrl = $imageUrl; + } + + public function getWeightUnits(): string + { + return $this->weightUnits; + } + + public function setWeightUnits(string $weightUnits): void + { + $this->weightUnits = $weightUnits; + } + + public function isAdjustment(): bool + { + return $this->adjustment; + } + + public function setAdjustment(bool $adjustment): void + { + $this->adjustment = $adjustment; + } + + /** + * @return Option[] + */ + public function getOptions(): array + { + return $this->options; + } + + /** + * @param Option[] $options + */ + public function setOptions(array $options): void + { + $this->options = $options; + } + + public static function asAdjustment(string $name, float $totalDiscount): self { return new self([ 'sku' => '', - 'name' => 'couponCode', + 'name' => trim($name), 'quantity' => 1, 'unitPrice' => round($totalDiscount, 2), 'adjustment' => true, diff --git a/src/models/Option.php b/src/models/Option.php index 1713640..9fcadcd 100644 --- a/src/models/Option.php +++ b/src/models/Option.php @@ -9,9 +9,29 @@ class Option extends Base { #[Groups(['export'])] #[SerializedName('Name')] - public string $name; + private string $name; #[Groups(['export'])] #[SerializedName('Value')] - public string $value; + private string $value; + + public function getName(): string + { + return $this->name; + } + + public function setName(string $name): void + { + $this->name = $name; + } + + public function getValue(): string + { + return $this->value; + } + + public function setValue(string $value): void + { + $this->value = $value; + } } diff --git a/src/models/Order.php b/src/models/Order.php index f7a5015..cd39740 100644 --- a/src/models/Order.php +++ b/src/models/Order.php @@ -25,40 +25,40 @@ class Order extends Base #[Groups(['export'])] #[SerializedName('OrderID')] - public int $orderId; + private int $orderId; #[Groups(['export'])] #[SerializedName('OrderNumber')] - public string $orderNumber; + private string $orderNumber; #[Groups(['export'])] #[SerializedName('OrderStatus')] - public ?string $orderStatus = null; + private ?string $orderStatus = null; #[Groups(['export'])] #[SerializedName('OrderTotal')] - public float $orderTotal; + private float $orderTotal; #[Groups(['export'])] #[SerializedName('TaxAmount')] - public float $taxAmount; + private float $taxAmount; #[Groups(['export'])] #[SerializedName('ShippingAmount')] - public float $shippingAmount; + private float $shippingAmount; #[Groups(['export'])] #[SerializedName('LastModified')] #[Type("DateTime<'n/j/Y H:m'>")] - public ?\DateTime $lastModifiedDate = null; + private ?\DateTime $lastModifiedDate = null; #[Groups(['export'])] #[SerializedName('PaymentMethod')] - public ?string $paymentMethod = null; + private ?string $paymentMethod = null; #[Groups(['export'])] #[SerializedName('ShippingMethod')] - public string $shippingMethod; + private ?string $shippingMethod = null; /** * @var Item[] @@ -66,27 +66,27 @@ class Order extends Base #[Groups(['export'])] #[SerializedName('Items')] #[XmlList(entry: 'Item')] - public array $items; + private array $items; #[Groups(['export'])] #[SerializedName('Customer')] - public ?Customer $customer = null; + private ?Customer $customer = null; #[Groups(['export'])] #[SerializedName('InternalNotes')] - public string $internalNotes = ''; + private string $internalNotes = ''; #[Groups(['export'])] #[SerializedName('Gift')] - public bool $gift = false; - - #[Exclude] - public CommerceOrder $parentOrder; + private bool $gift = false; #[Groups(['export'])] #[SerializedName('OrderDate')] #[Type("DateTime<'n/j/Y H:m'>")] - public ?\DateTime $orderDate = null; + private ?\DateTime $orderDate = null; + + #[Exclude] + private CommerceOrder $parent; #[Groups(['export'])] #[SerializedName('CustomField1')] @@ -108,6 +108,160 @@ class Order extends Base #[SerializedName('GiftMessage')] private string $giftMessage = ''; + public function getOrderId(): int + { + return $this->orderId; + } + + public function setOrderId(int $orderId): void + { + $this->orderId = $orderId; + } + + public function getOrderNumber(): string + { + return $this->orderNumber; + } + + public function setOrderNumber(string $orderNumber): void + { + $this->orderNumber = $orderNumber; + } + + public function getOrderStatus(): ?string + { + return $this->orderStatus; + } + + public function setOrderStatus(?string $orderStatus): void + { + $this->orderStatus = $orderStatus; + } + + public function getOrderTotal(): float + { + return $this->orderTotal; + } + + public function setOrderTotal(float $orderTotal): void + { + $this->orderTotal = $orderTotal; + } + + public function getTaxAmount(): float + { + return $this->taxAmount; + } + + public function setTaxAmount(float $taxAmount): void + { + $this->taxAmount = $taxAmount; + } + + public function getShippingAmount(): float + { + return $this->shippingAmount; + } + + public function setShippingAmount(float $shippingAmount): void + { + $this->shippingAmount = $shippingAmount; + } + + public function getLastModifiedDate(): ?\DateTime + { + return $this->lastModifiedDate; + } + + public function setLastModifiedDate(?\DateTime $lastModifiedDate): void + { + $this->lastModifiedDate = $lastModifiedDate; + } + + public function getPaymentMethod(): ?string + { + return $this->paymentMethod; + } + + public function setPaymentMethod(?string $paymentMethod): void + { + $this->paymentMethod = $paymentMethod; + } + + public function getShippingMethod(): ?string + { + return $this->shippingMethod; + } + + public function setShippingMethod(?string $shippingMethod): void + { + $this->shippingMethod = $shippingMethod; + } + + /** + * @return Item[] + */ + public function getItems(): array + { + return $this->items; + } + + /** + * @param Item[] $items + */ + public function setItems(array $items): void + { + $this->items = $items; + } + + public function getCustomer(): ?Customer + { + return $this->customer; + } + + public function setCustomer(?Customer $customer): void + { + $this->customer = $customer; + } + + /** + * @throws \JsonException + */ + public function getInternalNotes(): string + { + return substr(htmlspecialchars(static::asString($this->internalNotes)), 0, self::LONG_FIELD_LIMIT); + } + + public function setInternalNotes(string $internalNotes): void + { + $this->internalNotes = $internalNotes; + } + + public function isGift(): bool + { + return $this->gift; + } + + public function setGift(bool $gift): void + { + $this->gift = $gift; + } + + public function getOrderDate(): ?\DateTime + { + return $this->orderDate; + } + + public function setOrderDate(?\DateTime $orderDate): void + { + $this->orderDate = $orderDate; + } + + public function getParent(): CommerceOrder + { + return $this->parent; + } + public function setCustomField1(string $customField1): void { $this->customField1 = $customField1; @@ -147,19 +301,6 @@ public function getCustomField3(): string return substr(htmlspecialchars(static::asString($this->customField3)), 0, self::SHORT_FIELD_LIMIT); } - public function setInternalNotes(string $internalNotes): void - { - $this->internalNotes = $internalNotes; - } - - /** - * @throws \JsonException - */ - public function getInternalNotes(): string - { - return substr(htmlspecialchars(static::asString($this->internalNotes)), 0, self::LONG_FIELD_LIMIT); - } - public function setCustomerNotes(string $customerNotes): void { $this->customerNotes = $customerNotes; @@ -219,7 +360,7 @@ public static function fromCommerceOrder(CommerceOrder $commerceOrder): self // Include a discount as a line item if there is one. $totalDiscount = $commerceOrder->getTotalDiscount(); if ($totalDiscount !== 0.0) { - $items[] = Item::asAdjustment($totalDiscount); + $items[] = Item::asAdjustment('couponCode', $totalDiscount); } return new self([ @@ -235,7 +376,12 @@ public static function fromCommerceOrder(CommerceOrder $commerceOrder): self 'shippingMethod' => $commerceOrder->shippingMethodHandle, 'items' => $items, 'customer' => Customer::fromCommerceOrder($commerceOrder), - 'parentOrder' => $commerceOrder, + 'parent' => $commerceOrder, ]); } + + protected function setParent(CommerceOrder $parent): void + { + $this->parent = $parent; + } } diff --git a/src/models/Orders.php b/src/models/Orders.php index af7309f..20f9391 100644 --- a/src/models/Orders.php +++ b/src/models/Orders.php @@ -14,14 +14,40 @@ class Orders extends Model { #[Groups(['export'])] #[XmlAttribute] - public int $pages; + private int $pages; /** * @var Order[] */ #[Groups(['export'])] #[XmlList(inline: true, entry: 'Order')] - public array $orders = []; + private array $orders = []; + + public function getPages(): int + { + return $this->pages; + } + + public function setPages(int $pages): void + { + $this->pages = $pages; + } + + /** + * @return Order[] + */ + public function getOrders(): array + { + return $this->orders; + } + + /** + * @param Order[] $orders + */ + public function setOrders(array $orders): void + { + $this->orders = $orders; + } /** * @param Collection $orders @@ -30,7 +56,7 @@ public static function fromCollection(Collection $orders, int $pages): self { return new self([ 'pages' => $pages, - 'orders' => $orders, + 'orders' => $orders->toArray(), ]); } } diff --git a/src/services/Xml.php b/src/services/Xml.php index 1323252..c794704 100644 --- a/src/services/Xml.php +++ b/src/services/Xml.php @@ -30,12 +30,12 @@ public function generateXml(array $commerceOrders, int $pageCount): string /** @var Collection $failed */ [$orders, $failed] = collect($commerceOrders) ->map(Order::fromCommerceOrder(...)) - ->map(static function ($order): Order { + ->map(static function (Order $order): Order { $orderEvent = new OrderEvent([ - 'transformedOrder' => $order, + 'order' => $order, ]); Event::trigger(static::class, self::ORDER_EVENT, $orderEvent); - return $orderEvent->transformedOrder; + return $orderEvent->order; }) ->reduceSpread(static function (Collection $orders, Collection $failed, Order $order): array { /** @var Collection $orders */ @@ -61,7 +61,7 @@ public function generateXml(array $commerceOrders, int $pageCount): string $attribute = key($firstErrrors); $value = reset($firstErrrors[$attribute]); - throw new \RuntimeException("Invalid Order ID {$firstFailedOrder->orderId}: {$attribute} - {$value}"); + throw new \RuntimeException("Invalid Order ID {$firstFailedOrder->getOrderId()}: {$attribute} - {$value}"); } $serializer = SerializerBuilder::create()->build();