Skip to content

Commit 641c365

Browse files
committed
Rector & PHPStan Updates
Separate UnleashedAPI from Guzzle/Client as Guzzle/Client is a final class Update logging in UnleashedAPI with `Monolog::Level` Replace DataExtension with Extension Update tests
1 parent 491da0d commit 641c365

16 files changed

+360
-350
lines changed

_config/extensions.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ SilverShop\Page\ProductCategory:
1515
SilverShop\Model\Order:
1616
extensions:
1717
- AntonyThorpe\SilverShopUnleashed\Extension\UnleashedExtension
18-
- AntonyThorpe\SilverShopUnleashed\Extension\Order
18+
- AntonyThorpe\SilverShopUnleashed\Extension\OrderExtension
1919
SilverShop\Model\OrderItem:
2020
extensions:
2121
- AntonyThorpe\SilverShopUnleashed\Extension\UnleashedExtension
22-
- AntonyThorpe\SilverShopUnleashed\Extension\OrderItem
22+
- AntonyThorpe\SilverShopUnleashed\Extension\OrderItemExtension
2323
SilverShop\Model\Modifiers\OrderModifier:
2424
extensions:
2525
- AntonyThorpe\SilverShopUnleashed\Extension\UnleashedExtension
26-
- AntonyThorpe\SilverShopUnleashed\Extension\OrderModifier
26+
- AntonyThorpe\SilverShopUnleashed\Extension\OrderModifierExtension

composer.json

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
{
2-
"name": "antonythorpe/silvershop-unleashed",
3-
"description": "Silvershop submodule that integrates with Unleashed Software Inventory Management",
4-
"type": "silverstripe-vendormodule",
5-
"keywords": ["Silverstripe", "Silvershop", "eCommerce", "Unleashed Software", "inventory management"],
6-
"require": {
2+
"name": "antonythorpe/silvershop-unleashed",
3+
"description": "Silvershop submodule that integrates with Unleashed Software Inventory Management",
4+
"type": "silverstripe-vendormodule",
5+
"keywords": [
6+
"Silverstripe",
7+
"Silvershop",
8+
"eCommerce",
9+
"Unleashed Software",
10+
"inventory management"
11+
],
12+
"require": {
713
"php": "^8.1",
8-
"antonythorpe/consumer": "^4.0.1",
9-
"silvershop/core": "dev-main",
14+
"antonythorpe/consumer": "^4.0.1",
15+
"silvershop/core": "^3.2.0",
1016
"silverstripe/cms": "^5"
11-
},
12-
"require-dev": {
13-
"phpunit/phpunit": "^9.5",
17+
},
18+
"require-dev": {
19+
"phpunit/phpunit": "^9.5",
1420
"silvershop/shipping": "dev-main"
15-
},
21+
},
1622
"autoload": {
1723
"psr-4": {
1824
"AntonyThorpe\\SilverShopUnleashed\\": "src/"
@@ -24,9 +30,9 @@
2430
}
2531
},
2632
"extra": {
27-
"branch-alias": {
28-
"dev-master": "5.*-dev"
29-
}
30-
},
31-
"license": "MIT"
33+
"branch-alias": {
34+
"dev-master": "5.*-dev"
35+
}
36+
},
37+
"license": "MIT"
3238
}

src/BulkLoader/ProductBulkLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ class ProductBulkLoader extends BulkLoader
4141
];
4242

4343
/**
44-
* Specify a colsure to be run on every imported record to set other records
45-
* @param Product $obj The placeholder
44+
* Specify a closure to be run on every imported record to set other records
45+
* @param Product $product The placeholder
4646
* @param array $record A row from the external API
4747
*/
48-
public function setOtherProperties(Product &$obj, array $record): void
48+
public function setOtherProperties(Product &$product, array $record): void
4949
{
5050
if ($record['Obsolete']) {
51-
$obj->AllowPurchase = false;
51+
$product->AllowPurchase = false;
5252
}
5353
}
5454
}
Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
namespace AntonyThorpe\SilverShopUnleashed\Extension;
44

5+
use SilverStripe\Core\Extension;
56
use DateTime;
67
use SilverStripe\Security\Member;
78
use SilverStripe\ORM\FieldType\DBDatetime;
8-
use SilverStripe\ORM\DataExtension;
9-
use SilverStripe\ORM\DataObject;
109
use SilverShop\Extension\ShopConfigExtension;
1110
use SilverShop\Model\Address;
11+
use SilverShop\Model\Order;
1212
use AntonyThorpe\SilverShopUnleashed\UnleashedAPI;
1313
use AntonyThorpe\SilverShopUnleashed\Defaults;
1414
use AntonyThorpe\SilverShopUnleashed\Utils;
1515

16-
class Order extends DataExtension
16+
17+
/**
18+
* @property ?string $OrderSentToUnleashed
19+
* @extends Extension<Order&static>
20+
*/
21+
class OrderExtension extends Extension
1722
{
1823
/**
1924
* Record when an order is sent to Unleashed
@@ -28,7 +33,6 @@ class Order extends DataExtension
2833
*/
2934
public function onBeforeWrite(): void
3035
{
31-
parent::onBeforeWrite();
3236
if (!$this->getOwner()->getField("Guid")) {
3337
$this->getOwner()->Guid = Utils::createGuid();
3438
}
@@ -44,14 +48,17 @@ public function getAddressName(array|Address $address): string
4448
if ($address['StreetAddress2']) {
4549
$address_name .= ' ' . $address['StreetAddress2'];
4650
}
51+
4752
$address_name .= ' ' . $address['City'];
4853
} else {
4954
$address_name = $address->Address;
5055
if ($address->AddressLine2) {
5156
$address_name .= ' ' . $address->AddressLine2;
5257
}
58+
5359
$address_name .= ' ' . $address->City;
5460
}
61+
5562
return $address_name;
5663
}
5764

@@ -65,16 +72,17 @@ public function matchCustomerAddress(array $items, array|Address $shipping_addre
6572
if ($address['AddressType'] != "Physical" && isset($items[0]['Addresses'][1])) {
6673
$address = $items[0]['Addresses'][1];
6774
}
75+
6876
return strtoupper($this->getAddressName($shipping_address)) === strtoupper($this->getAddressName($address));
6977
}
7078

7179
/**
7280
* add the address components to the body array
7381
* $type is either Postal or Physical
7482
*/
75-
public function setBodyAddress(array $body, DataObject $order, string $type): array
83+
public function setBodyAddress(array $body, Order $order, string $type): array
7684
{
77-
$countries = (array) ShopConfigExtension::config()->iso_3166_country_codes;
85+
$countries = (array) ShopConfigExtension::config()->get('iso_3166_country_codes');
7886

7987
if ($type === 'Postal') {
8088
$address = $order->BillingAddress();
@@ -128,7 +136,7 @@ public function setBodyAddress(array $body, DataObject $order, string $type): ar
128136
/**
129137
* Add the currency code to the body array
130138
*/
131-
public function setBodyCurrencyCode(array $body, DataObject $order): array
139+
public function setBodyCurrencyCode(array $body, Order $order): array
132140
{
133141
$body['Currency']['CurrencyCode'] = $order->Currency();
134142
return $body;
@@ -137,39 +145,41 @@ public function setBodyCurrencyCode(array $body, DataObject $order): array
137145
/**
138146
* Add the Customer Code/Name (use Company field of BillingAddress to allow for B2B eCommerce sites)
139147
*/
140-
public function setBodyCustomerCodeAndName(array $body, DataObject $order): array
148+
public function setBodyCustomerCodeAndName(array $body, Order $order): array
141149
{
142-
$billing_address = $order->BillingAddress();
143-
if ($billing_address->Company) {
150+
$address = $order->BillingAddress();
151+
if ($address->Company) {
144152
// use Organisation name
145-
$body['CustomerCode'] = $billing_address->Company;
146-
$body['CustomerName'] = $billing_address->Company;
153+
$body['CustomerCode'] = $address->Company;
154+
$body['CustomerName'] = $address->Company;
147155
} else {
148156
// use Contact full name instead
149157
$body['CustomerCode'] = $order->getName();
150158
$body['CustomerName'] = $order->getName();
151159
}
160+
152161
return $body;
153162
}
154163

155164
/**
156165
* Set Delivery Method and Delivery Name
157166
* Allow for the SilverShop Shipping module
158167
*/
159-
public function setBodyDeliveryMethodAndDeliveryName(array $body, DataObject $order, string $shipping_modifier_class_name): array
168+
public function setBodyDeliveryMethodAndDeliveryName(array $body, Order $order, string $shipping_modifier_class_name): array
160169
{
161170
$shipping_modifier = $order->getModifier($shipping_modifier_class_name);
162171
if (!empty($shipping_modifier)) {
163172
$body['DeliveryMethod'] = $shipping_modifier::config()->product_code;
164173
$body['DeliveryName'] = $shipping_modifier::config()->product_code;
165174
}
175+
166176
return $body;
167177
}
168178

169179
/**
170180
* Set Sales Order Lines
171181
*/
172-
public function setBodySalesOrderLines(array $body, DataObject $order, string $tax_modifier_class_name, int $rounding_precision): array
182+
public function setBodySalesOrderLines(array $body, Order $order, string $tax_modifier_class_name, int $rounding_precision): array
173183
{
174184
$line_number = 0;
175185

@@ -198,14 +208,15 @@ public function setBodySalesOrderLines(array $body, DataObject $order, string $t
198208
);
199209
$sales_order_line['LineTaxCode'] = $body['Tax']['TaxCode'];
200210
}
211+
201212
$body['SalesOrderLines'][] = $sales_order_line;
202213
}
203214

204215
// Add Modifiers that have a product_code
205216
foreach ($order->Modifiers()->sort('Sort')->getIterator() as $modifier) {
206217
$line_total = round(floatval($modifier->Amount), $rounding_precision);
207218

208-
if ($modifier::config()->product_code &&
219+
if ($modifier::config()->get('product_code') &&
209220
$modifier->Type !== 'Ignored' &&
210221
!empty($line_total)
211222
) {
@@ -218,7 +229,7 @@ public function setBodySalesOrderLines(array $body, DataObject $order, string $t
218229
'LineType' => null,
219230
'OrderQuantity' => 1,
220231
'Product' => [
221-
'ProductCode' => $modifier::config()->product_code,
232+
'ProductCode' => $modifier::config()->get('product_code'),
222233
],
223234
'UnitPrice' => round(floatval($modifier->Amount), $rounding_precision)
224235
];
@@ -230,16 +241,18 @@ public function setBodySalesOrderLines(array $body, DataObject $order, string $t
230241
);
231242
$sales_order_line['LineTaxCode'] = $body['Tax']['TaxCode'];
232243
}
244+
233245
$body['SalesOrderLines'][] = $sales_order_line;
234246
}
235247
}
248+
236249
return $body;
237250
}
238251

239252
/**
240253
* Set the Tax Codes
241254
*/
242-
public function setBodyTaxCode(array $body, DataObject $order, string $tax_modifier_class_name): array
255+
public function setBodyTaxCode(array $body, Order $order, string $tax_modifier_class_name): array
243256
{
244257
if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') {
245258
$tax_modifier = $order->getModifier($tax_modifier_class_name);
@@ -248,14 +261,15 @@ public function setBodyTaxCode(array $body, DataObject $order, string $tax_modif
248261
$body['Tax']['TaxCode'] = $tax_modifier::config()->tax_code;
249262
}
250263
}
264+
251265
return $body;
252266
}
253267

254268

255269
/**
256270
* Calculate the SubTotal and TaxTotal
257271
*/
258-
public function setBodySubTotalAndTax(array $body, DataObject $order, string $tax_modifier_class_name, int $rounding_precision): array
272+
public function setBodySubTotalAndTax(array $body, Order $order, string $tax_modifier_class_name, int $rounding_precision): array
259273
{
260274
if ($tax_modifier_class_name !== '' && $tax_modifier_class_name !== '0') {
261275
$tax_modifier = $order->getModifier($tax_modifier_class_name);
@@ -276,6 +290,7 @@ public function setBodySubTotalAndTax(array $body, DataObject $order, string $ta
276290
$rounding_precision
277291
);
278292
}
293+
279294
$body['TaxTotal'] = $tax_total;
280295
$body['SubTotal'] = $sub_total;
281296

@@ -290,6 +305,7 @@ public function setBodySubTotalAndTax(array $body, DataObject $order, string $ta
290305
} else {
291306
$body['SubTotal'] = round(floatval($order->Total()), $rounding_precision);
292307
}
308+
293309
return $body;
294310
}
295311

@@ -299,11 +315,10 @@ public function setBodySubTotalAndTax(array $body, DataObject $order, string $ta
299315
*/
300316
public function onAfterWrite(): void
301317
{
302-
parent::onAfterWrite();
303318
$config = $this->getOwner()->config();
304-
$defaults = Defaults::config();
319+
$configForClass = Defaults::config();
305320

306-
if ($defaults->get('send_sales_orders_to_unleashed')
321+
if ($configForClass->get('send_sales_orders_to_unleashed')
307322
&& $this->getOwner()->Status == 'Paid'
308323
&& !$this->getOwner()->OrderSentToUnleashed) {
309324
// Definitions
@@ -319,10 +334,10 @@ public function onAfterWrite(): void
319334
'Guid' => $order->Guid,
320335
'OrderDate' => $date_placed->format('Y-m-d\TH:i:s'),
321336
'OrderNumber' => $order->Reference,
322-
'OrderStatus' => $defaults->get('order_status'),
337+
'OrderStatus' => $configForClass->get('order_status'),
323338
'PaymentDueDate' => $date_paid->format('Y-m-d\TH:i:s'),
324-
'PaymentTerm' => $defaults->get('payment_term'),
325-
'PrintPackingSlipInsteadOfInvoice' => $defaults->get('print_packingslip_instead_of_invoice'),
339+
'PaymentTerm' => $configForClass->get('payment_term'),
340+
'PrintPackingSlipInsteadOfInvoice' => $configForClass->get('print_packingslip_instead_of_invoice'),
326341
'ReceivedDate' => $date_placed->format('Y-m-d\TH:i:s'),
327342
'SalesOrderLines' => [],
328343
'SellPriceTier' => ShopConfigExtension::current()->CustomerGroup()->Title,
@@ -335,38 +350,40 @@ public function onAfterWrite(): void
335350
$body = $this->setBodyAddress($body, $order, 'Physical');
336351
$body = $this->setBodyCurrencyCode($body, $order);
337352
$body = $this->setBodyCustomerCodeAndName($body, $order);
338-
$body = $this->setBodyDeliveryMethodAndDeliveryName($body, $order, $defaults->get('shipping_modifier_class_name'));
339-
$body = $this->setBodyTaxCode($body, $order, $defaults->get('tax_modifier_class_name'));
340-
$body = $this->setBodySalesOrderLines($body, $order, $defaults->get('tax_modifier_class_name'), $config->get('rounding_precision'));
341-
$body = $this->setBodySubTotalAndTax($body, $order, $defaults->get('tax_modifier_class_name'), $config->get('rounding_precision'));
353+
$body = $this->setBodyDeliveryMethodAndDeliveryName($body, $order, $configForClass->get('shipping_modifier_class_name'));
354+
$body = $this->setBodyTaxCode($body, $order, $configForClass->get('tax_modifier_class_name'));
355+
$body = $this->setBodySalesOrderLines($body, $order, $configForClass->get('tax_modifier_class_name'), $config->get('rounding_precision'));
356+
$body = $this->setBodySubTotalAndTax($body, $order, $configForClass->get('tax_modifier_class_name'), $config->get('rounding_precision'));
342357

343358
// Add optional defaults
344-
if ($defaults->get('created_by')) {
345-
$body['CreatedBy'] = $defaults->get('created_by');
359+
if ($configForClass->get('created_by')) {
360+
$body['CreatedBy'] = $configForClass->get('created_by');
346361
}
347362

348-
if ($defaults->get('customer_type')) {
349-
$body['CustomerType'] = $defaults->get('customer_type');
363+
if ($configForClass->get('customer_type')) {
364+
$body['CustomerType'] = $configForClass->get('customer_type');
350365
}
351366

352-
if ($defaults->get('sales_order_group')) {
353-
$body['SalesOrderGroup'] = $defaults->get('sales_order_group');
367+
if ($configForClass->get('sales_order_group')) {
368+
$body['SalesOrderGroup'] = $configForClass->get('sales_order_group');
354369
}
355370

356-
if ($defaults->get('source_id')) {
357-
$body['SourceId'] = $defaults->get('source_id');
371+
if ($configForClass->get('source_id')) {
372+
$body['SourceId'] = $configForClass->get('source_id');
358373
}
359374

360375
// add phone number if available
361-
if ($order->BillingAddress()->Phone) {
362-
$body['PhoneNumber'] = $order->BillingAddress()->Phone;
376+
$billing_address = $order->BillingAddress();
377+
if ($billing_address->exists() && $billing_address->Phone) {
378+
$body['PhoneNumber'] = $billing_address->Phone;
363379
}
364380

365381
// add required date
366382
$date_required = new DateTime($order->Paid);
367-
if ($defaults->get('expected_days_to_deliver')) {
368-
$date_required->modify('+' . $defaults->get('expected_days_to_deliver') . 'day');
383+
if ($configForClass->get('expected_days_to_deliver')) {
384+
$date_required->modify('+' . $configForClass->get('expected_days_to_deliver') . 'day');
369385
}
386+
370387
$body['RequiredDate'] = $date_required->format('Y-m-d\TH:i:s');
371388

372389
if ($order->Notes) {
@@ -416,6 +433,7 @@ public function onAfterWrite(): void
416433
if ($body['Comments']) {
417434
$body['Comments'] .= '. ';
418435
}
436+
419437
$body['Comments'] .= _t(
420438
'UnleashedAPI.addEmailToCustomerComment',
421439
'Add email to Customer: {email_address}',

0 commit comments

Comments
 (0)