Skip to content

Commit

Permalink
fix(products): improve handling of nonexistent products
Browse files Browse the repository at this point in the history
Resolves #228
  • Loading branch information
EdieLemoine authored and myparcel-bot[bot] committed Dec 1, 2023
1 parent 349fbd6 commit ca8250a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
43 changes: 24 additions & 19 deletions src/Pdk/Order/Repository/PsPdkOrderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
use MyParcelNL\Pdk\App\Order\Model\PdkOrder;
use MyParcelNL\Pdk\App\Order\Repository\AbstractPdkOrderRepository;
use MyParcelNL\Pdk\Base\Contract\CurrencyServiceInterface;
use MyParcelNL\Pdk\Base\Contract\WeightServiceInterface;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Shipment\Model\Shipment;
use MyParcelNL\Pdk\Storage\MemoryCacheStorage;
use MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface;
use MyParcelNL\PrestaShop\Entity\MyparcelnlOrderShipment;
use MyParcelNL\PrestaShop\Pdk\Base\Adapter\PsAddressAdapter;
use MyParcelNL\PrestaShop\Repository\PsOrderShipmentRepository;
use MyParcelNL\PrestaShop\Service\PsProductService;
use Order as PsOrder;

final class PsPdkOrderRepository extends AbstractPdkOrderRepository
Expand Down Expand Up @@ -48,35 +48,35 @@ final class PsPdkOrderRepository extends AbstractPdkOrderRepository
private $psOrderShipmentRepository;

/**
* @var \MyParcelNL\Pdk\Base\Contract\WeightServiceInterface
* @var \MyParcelNL\PrestaShop\Service\PsProductService
*/
private $weightService;
private PsProductService $psProductService;

/**
* @param \MyParcelNL\Pdk\Storage\MemoryCacheStorage $storage
* @param \MyParcelNL\PrestaShop\Repository\PsOrderShipmentRepository $psOrderShipmentRepository
* @param \MyParcelNL\Pdk\Base\Contract\CurrencyServiceInterface $currencyService
* @param \MyParcelNL\Pdk\App\Order\Contract\PdkProductRepositoryInterface $productRepository
* @param \MyParcelNL\Pdk\Base\Contract\WeightServiceInterface $weightService
* @param \MyParcelNL\PrestaShop\Pdk\Base\Adapter\PsAddressAdapter $addressAdapter
* @param \MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface $psOrderService
* @param \MyParcelNL\PrestaShop\Service\PsProductService $psProductService
*/
public function __construct(
MemoryCacheStorage $storage,
PsOrderShipmentRepository $psOrderShipmentRepository,
CurrencyServiceInterface $currencyService,
PdkProductRepositoryInterface $productRepository,
WeightServiceInterface $weightService,
PsAddressAdapter $addressAdapter,
PsOrderServiceInterface $psOrderService
PsOrderServiceInterface $psOrderService,
PsProductService $psProductService
) {
parent::__construct($storage);
$this->psOrderShipmentRepository = $psOrderShipmentRepository;
$this->currencyService = $currencyService;
$this->productRepository = $productRepository;
$this->weightService = $weightService;
$this->addressAdapter = $addressAdapter;
$this->psOrderService = $psOrderService;
$this->psProductService = $psProductService;
}

/**
Expand All @@ -98,7 +98,7 @@ public function get($input): PdkOrder

return $this->retrieve((string) $psOrder->id, function () use ($psOrder) {
$orderData = $this->psOrderService->getOrderData($psOrder);
$orderProducts = $psOrder->getProducts() ?: [];
$orderProducts = new Collection($psOrder->getProducts() ?: []);

return new PdkOrder(
array_replace([
Expand Down Expand Up @@ -164,21 +164,26 @@ public function updateMany(PdkOrderCollection $collection): PdkOrderCollection
}

/**
* @param array $orderProducts
* @param \MyParcelNL\Pdk\Base\Support\Collection $orderProducts
*
* @return array|array[]
*/
protected function createOrderLines(array $orderProducts): array
protected function createOrderLines(Collection $orderProducts): array
{
return array_map(function (array $product) {
return [
'quantity' => $product['product_quantity'] ?? 1,
'price' => $this->currencyService->convertToCents($product['product_price'] ?? 0),
'priceAfterVat' => $this->currencyService->convertToCents($product['product_price_wt'] ?? 0),
'product' => $this->productRepository->getProduct($product['product_id']),
'vatRate' => $product['tax_rate'],
];
}, array_values($orderProducts));
return $orderProducts
->filter(function (array $product) {
return $this->psProductService->exists($product['id_product'] ?? 0);

Check warning on line 175 in src/Pdk/Order/Repository/PsPdkOrderRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Pdk/Order/Repository/PsPdkOrderRepository.php#L175

Added line #L175 was not covered by tests
})
->map(function (array $product) {
return [
'quantity' => $product['product_quantity'] ?? 1,
'price' => $this->currencyService->convertToCents($product['product_price'] ?? 0),
'priceAfterVat' => $this->currencyService->convertToCents($product['product_price_wt'] ?? 0),
'product' => $this->productRepository->getProduct($product['product_id'] ?? 0),
'vatRate' => $product['tax_rate'] ?? 0,
];

Check warning on line 184 in src/Pdk/Order/Repository/PsPdkOrderRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Pdk/Order/Repository/PsPdkOrderRepository.php#L178-L184

Added lines #L178 - L184 were not covered by tests
})
->toArrayWithoutNull();
}

/**
Expand Down
32 changes: 25 additions & 7 deletions src/Pdk/Product/Repository/PsPdkProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
namespace MyParcelNL\PrestaShop\Pdk\Product\Repository;

use Context;
use InvalidArgumentException;
use MyParcelNL\Pdk\App\Order\Collection\PdkProductCollection;
use MyParcelNL\Pdk\App\Order\Model\PdkProduct;
use MyParcelNL\Pdk\App\Order\Repository\AbstractPdkPdkProductRepository;
use MyParcelNL\Pdk\Base\Contract\CurrencyServiceInterface;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\Pdk\Settings\Model\ProductSettings;
use MyParcelNL\Pdk\Storage\Contract\StorageInterface;
use MyParcelNL\PrestaShop\Pdk\Base\Service\PsWeightService;
use MyParcelNL\PrestaShop\Repository\PsProductSettingsRepository;
use MyParcelNL\PrestaShop\Service\PsProductService;
use MyParcelNL\Sdk\src\Support\Arr;
use Product;
use Product as PsProduct;

class PsPdkProductRepository extends AbstractPdkPdkProductRepository
{
Expand All @@ -23,6 +26,11 @@ class PsPdkProductRepository extends AbstractPdkPdkProductRepository
*/
private $currencyService;

/**
* @var \MyParcelNL\PrestaShop\Service\PsProductService
*/
private PsProductService $psProductService;

/**
* @var \MyParcelNL\PrestaShop\Repository\PsProductSettingsRepository
*/
Expand All @@ -37,17 +45,20 @@ class PsPdkProductRepository extends AbstractPdkPdkProductRepository
* @param \MyParcelNL\Pdk\Storage\Contract\StorageInterface $storage
* @param \MyParcelNL\PrestaShop\Pdk\Base\Service\PsWeightService $weightService
* @param \MyParcelNL\PrestaShop\Repository\PsProductSettingsRepository $productSettingsRepository
* @param \MyParcelNL\PrestaShop\Service\PsProductService $psProductService
* @param \MyParcelNL\Pdk\Base\Contract\CurrencyServiceInterface $currencyService
*/
public function __construct(
StorageInterface $storage,
PsWeightService $weightService,
PsProductSettingsRepository $productSettingsRepository,
PsProductService $psProductService,
CurrencyServiceInterface $currencyService
) {
parent::__construct($storage);
$this->weightService = $weightService;
$this->psProductSettingsRepository = $productSettingsRepository;
$this->psProductService = $psProductService;
$this->currencyService = $currencyService;
}

Expand All @@ -58,21 +69,28 @@ public function __construct(
*/
public function getProduct($identifier): PdkProduct
{
if (! $this->psProductService->exists($identifier)) {
Logger::error("Product with id $identifier not found");
throw new InvalidArgumentException('Product not found');

Check warning on line 74 in src/Pdk/Product/Repository/PsPdkProductRepository.php

View check run for this annotation

Codecov / codecov/patch

src/Pdk/Product/Repository/PsPdkProductRepository.php#L73-L74

Added lines #L73 - L74 were not covered by tests
}

return $this->retrieve((string) $identifier, function () use ($identifier) {
$psProduct = new Product($identifier);
/** @var PsProduct $psProduct */
$psProduct = $this->psProductService->get($identifier);

$translate = static function (array $strings) {
return $strings[Context::getContext()->language->id] ?? $strings[1] ?? reset($strings);
return $strings[Context::getContext()->language->id] ?? $strings[1] ?? Arr::last($strings);
};

return new PdkProduct([
'externalIdentifier' => $psProduct->id,
'name' => $translate($psProduct->name),
'weight' => $this->weightService->convertToGrams($psProduct->weight),
'name' => $translate($psProduct->name ?? []),
'weight' => $this->weightService->convertToGrams($psProduct->weight ?? 0),
'settings' => $this->getProductSettings($identifier),
'isDeliverable' => $this->isDeliverable($psProduct),
'price' => [
'currency' => Context::getContext()->currency->iso_code,
'amount' => $this->currencyService->convertToCents($psProduct->price),
'amount' => $this->currencyService->convertToCents($psProduct->price ?? 0),
],
]);
});
Expand Down Expand Up @@ -138,7 +156,7 @@ protected function getKeyPrefix(): string
*
* @return bool
*/
private function isDeliverable(Product $psProduct): bool
private function isDeliverable(PsProduct $psProduct): bool
{
return $psProduct->available_for_order
&& $psProduct->active
Expand Down
15 changes: 15 additions & 0 deletions src/Service/PsProductService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Service;

use Product;

final class PsProductService extends PsSpecificObjectModelService
{
protected function getClass(): string
{
return Product::class;
}
}

0 comments on commit ca8250a

Please sign in to comment.