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(orders): export correct recipient name #277

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
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;
}
63 changes: 33 additions & 30 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,72 +17,74 @@ final class PsAddressAdapter
public const ADDRESS_TYPE_SHIPPING = 'shipping';
public const ADDRESS_TYPE_BILLING = 'billing';

private PsObjectModelServiceInterface $psObjectModelService;

/**
* @param \MyParcelNL\PrestaShop\Contract\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);
$state = $country->iso_code === Platform::get('localCountry')
? null : new State($address->id_state);
? null
: $this->psObjectModelService->getWithFallback(State::class, $address->id_state);

return array_merge([
'cc' => $country->iso_code,
'city' => $address->city,
'address1' => $address->address1,
'address2' => $address->address2,
'postalCode' => $address->postcode,
'person' => trim(sprintf('%s %s', $address->firstname, $address->lastname)),
'phone' => $address->phone,
],
return array_merge(
[
'cc' => $country->iso_code,
'city' => $address->city,
'address1' => $address->address1,
'address2' => $address->address2,
'postalCode' => $address->postcode,
'person' => trim(sprintf('%s %s', $address->firstname, $address->lastname)),
'phone' => $address->phone,
],
$state ? [
'region' => $state->name,
'state' => $state->iso_code,
] : []);
] : []
);
}

/**
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