Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(products): improve handling of nonexistent products #229

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@

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 @@
}

/**
* @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 @@
*/
private $currencyService;

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

/**
* @var \MyParcelNL\PrestaShop\Repository\PsProductSettingsRepository
*/
Expand All @@ -37,17 +45,20 @@
* @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 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 @@
*
* @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;
}
}