Skip to content

Commit

Permalink
fix(orders): export correct recipient name
Browse files Browse the repository at this point in the history
Needs #276
  • Loading branch information
EdieLemoine committed Oct 9, 2024
1 parent ea026d1 commit 36ea5de
Show file tree
Hide file tree
Showing 50 changed files with 453 additions and 175 deletions.
64 changes: 40 additions & 24 deletions src/Contract/PsObjectModelServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,77 @@ interface PsObjectModelServiceInterface
public function add(ObjectModel $model): bool;

/**
* @template T of ObjectModel
* @param T|class-string<T> $class
* @param null|int|T $input
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param int|Instance $input
*
* @return T
* @return Model
*/
public function create(string $class, $input = null): ObjectModel;

/**
* @template T of ObjectModel
* @param T|class-string<T> $class
* @param int|T $input
* @param bool $soft
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param int|Instance $input
* @param bool $soft
*
* @return bool
*/
public function delete(string $class, $input, bool $soft = false): bool;

/**
* @template T of ObjectModel
* @param T|class-string<T> $class
* @param (int|T)[]|\MyParcelNL\Pdk\Base\Support\Collection<int|T>|\PrestaShopCollection $input
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param (int|Instance)[]|\MyParcelNL\Pdk\Base\Support\Collection|\PrestaShopCollection $input
* @param bool $soft
*
* @return bool
*/
public function deleteMany(string $class, $input, bool $soft = false): bool;

/**
* @template T of ObjectModel
* @param T|class-string<T> $class
* @param int|T $input
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param int|Instance $input
*
* @return bool
*/
public function exists(string $class, $input): bool;

/**
* @template T of ObjectModel
* @param T|class-string<T> $class
* @param int|T $input
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param int|Instance $input
*
* @return null|T
* @return null|Model
*/
public function get(string $class, $input): ?ObjectModel;

/**
* @template T of ObjectModel
* @param string $class
* @param int|T $input
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param int|Instance $input
*
* @return null|int
*/
public function getId(string $class, $input): ?int;

/**
* @template Model of \ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param int|Instance $input
*
* @return Model
*/
public function getWithFallback(string $class, $input): ObjectModel;

/**
* @param \ObjectModel $model
*
Expand All @@ -83,11 +99,11 @@ public function getId(string $class, $input): ?int;
public function update(ObjectModel $model): bool;

/**
* @template T of ObjectModel
* @param \T $model
* @template Model of \ObjectModel
* @param Model $model
* @param null|bool $existing
*
* @return \T
* @return Model
*/
public function updateOrAdd(ObjectModel $model, ?bool $existing = null): ObjectModel;
}
40 changes: 20 additions & 20 deletions src/Pdk/Base/Adapter/PsAddressAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Country;
use Customer;
use MyParcelNL\Pdk\Facade\Platform;
use MyParcelNL\PrestaShop\Contract\PsObjectModelServiceInterface;
use Order;
use State;

Expand All @@ -16,56 +17,55 @@ final class PsAddressAdapter
public const ADDRESS_TYPE_SHIPPING = 'shipping';
public const ADDRESS_TYPE_BILLING = 'billing';

/**
* @var \MyParcelNL\PrestaShop\Contract\PsObjectModelServiceInterface
*/
private PsObjectModelServiceInterface $psObjectModelService;

public function __construct(PsObjectModelServiceInterface $psObjectModelService)
{
$this->psObjectModelService = $psObjectModelService;
}

/**
* @param \Address|int|null $address
*
* @return array
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function fromAddress($address): array
{
if (! $address instanceof Address) {
$address = new Address((int) $address);
}
$model = $this->psObjectModelService->getWithFallback(Address::class, $address);

return $this->createFromAddress($address);
return $this->createFromAddress($model);
}

/**
* @param int|string|\Order $order
* @param string $addressType
*
* @return array
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function fromOrder($order, string $addressType = self::ADDRESS_TYPE_SHIPPING): array
{
if (! $order instanceof Order) {
$order = new Order((int) $order);
}

$addressId = $addressType === self::ADDRESS_TYPE_SHIPPING
$orderModel = $this->psObjectModelService->getWithFallback(Order::class, $order);
$addressId = $addressType === self::ADDRESS_TYPE_SHIPPING
? $order->id_address_delivery
: $order->id_address_invoice;

$address = new Address($addressId);
$customer = new Customer($order->id_customer);
$address = $this->psObjectModelService->getWithFallback(Address::class, $addressId);
$customer = $this->psObjectModelService->getWithFallback(Customer::class, $orderModel->id_customer);

return $this->createFromCustomer($customer) + $this->createFromAddress($address);
return array_replace($this->createFromCustomer($customer), $this->createFromAddress($address));
}

/**
* @param \Address $address
*
* @return array
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
private function createFromAddress(Address $address): array
{
$country = new Country($address->id_country);
$country = $this->psObjectModelService->getWithFallback(Country::class, $address->id_country);

return [
'cc' => $country->iso_code,
Expand All @@ -77,7 +77,7 @@ private function createFromAddress(Address $address): array
'phone' => $address->phone,
'region' => $country->iso_code === Platform::get('localCountry')
? null
: (new State($address->id_state))->name,
: $this->psObjectModelService->getWithFallback(State::class, $address->id_state)->name,
];
}

Expand Down
7 changes: 6 additions & 1 deletion src/Service/PsObjectModelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function create(string $class, $input = null): ObjectModel
}

/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function delete(string $class, $input, bool $soft = false): bool
{
Expand Down Expand Up @@ -122,6 +122,11 @@ public function getId(string $class, $input): ?int
return $input ? (int) $input : null;
}

public function getWithFallback(string $class, $input): ObjectModel
{
return $this->get($class, $input) ?? $this->create($class);
}

/**
* @param \ObjectModel $model
*
Expand Down
28 changes: 14 additions & 14 deletions tests/Factory/AbstractPsModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,20 @@ abstract class AbstractPsModelFactory extends AbstractPsFactory
/**
* @var array<string, T>
*/
private $cache = [];
private array $cache = [];

/**
* @param string $class
* @param array $attributes
*
* @return T
*/
abstract protected function createObject(string $class, array $attributes);

/**
* @return class-string<T>
*/
abstract protected function getClass(): string;

/**
* @return T
Expand Down Expand Up @@ -47,19 +60,6 @@ public function store()
return $model;
}

/**
* @param string $class
* @param array $attributes
*
* @return T
*/
abstract protected function createObject(string $class, array $attributes);

/**
* @return class-string<T>
*/
abstract protected function getClass(): string;

/**
* @return int
*/
Expand Down
Loading

0 comments on commit 36ea5de

Please sign in to comment.