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 24, 2024
1 parent acb9d94 commit 20559d6
Show file tree
Hide file tree
Showing 50 changed files with 451 additions and 173 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;
}
36 changes: 18 additions & 18 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,52 +17,51 @@ 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
{
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
56 changes: 39 additions & 17 deletions tests/Factory/AbstractPsObjectModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace MyParcelNL\PrestaShop\Tests\Factory;

use MyParcelNL\Pdk\Tests\Factory\Contract\FactoryInterface;
use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithActive;
use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithSoftDeletes;
use MyParcelNL\PrestaShop\Tests\Factory\Concern\WithTimestamps;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\PsFactoryInterface;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\PsObjectModelFactoryInterface;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithSoftDeletes;
use MyParcelNL\PrestaShop\Tests\Factory\Contract\WithTimestamps;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModel;
use MyParcelNL\PrestaShop\Tests\Mock\MockPsObjectModels;
use MyParcelNL\Sdk\src\Support\Str;
Expand All @@ -26,12 +27,12 @@ abstract class AbstractPsObjectModelFactory extends AbstractPsModelFactory imple
/**
* @var PsObjectModelFactoryInterface[]
*/
private $additionalModelsToStore = [];
private array $additionalModelsToStore = [];

/**
* @var null|int
*/
private $id;
private ?int $id;

/**
* @param null|int $id
Expand Down Expand Up @@ -73,7 +74,7 @@ public function store(): ObjectModel
protected function addAttribute(string $attribute, $value, array $attributes = []): AbstractPsFactory
{
if (Str::startsWith($attribute, 'id_')) {
return $this->withModel(Str::after($attribute, 'id_'), $value, $attributes);
return $this->withModel($attribute, $value, $attributes);
}

if ($value instanceof PsObjectModelFactoryInterface || $value instanceof MockPsObjectModel) {
Expand Down Expand Up @@ -101,9 +102,23 @@ protected function createDefault(): FactoryInterface
$factory->withDeleted(false);
}

if ($factory instanceof WithActive) {
$factory->withActive(true);
}

return $factory;
}

/**
* @param string $key
*
* @return string
*/
protected function createIdKey(string $key): string
{
return sprintf('id_%s', Str::snake($key));
}

/**
* @param string $class
* @param array $attributes
Expand Down Expand Up @@ -155,24 +170,25 @@ protected function save($model): void
* @param string $key
* @param int|ObjectModel|PsObjectModelFactoryInterface $input
* @param array $attributes
* @param null|string $keyOverride
*
* @return $this
* @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException
*/
protected function withModel(string $key, $input, array $attributes = []): self
protected function withModel(string $key, $input, array $attributes = [], ?string $keyOverride = null): self
{
if (is_int($input)) {
$class = Str::after($key, 'id_');
$existingModel = MockPsObjectModels::get($class, $input);

if ($existingModel) {
return $this->withModel($class, $existingModel, $attributes);
return $this->withModel($class, $existingModel, $attributes, $keyOverride);
}

/** @var PsObjectModelFactoryInterface $factory */
$factory = psFactory(Str::studly($class), $input);

return $this->withModel($class, $factory, $attributes);
return $this->withModel($class, $factory, $attributes, $keyOverride);
}

if ($input instanceof PsFactoryInterface) {
Expand All @@ -182,25 +198,31 @@ protected function withModel(string $key, $input, array $attributes = []): self
->with($attributes)
->make();

return $this->withModel($key, $model);
return $this->withModel($key, $model, [], $keyOverride);
}

$idKey = sprintf('id_%s', Str::snake($key));
$idKey = $this->createIdKey($keyOverride ?? $key);

return $this->with([$idKey => $input->id]);
}

/**
* @param string $key
* @param int|ObjectModel|PsObjectModelFactoryInterface $input
* @param array $attributes
* @param string $foreignKey
* @template Model of ObjectModel
* @template Instance of Model
* @param class-string<Model> $class
* @param string $key
* @param int|Instance|PsObjectModelFactoryInterface<Instance> $input
* @param array $attributes
*
* @return $this
* @throws \MyParcelNL\Pdk\Tests\Factory\Exception\InvalidFactoryException
*/
protected function withRelation(string $key, $input, array $attributes, string $foreignKey): self
{
return $this->withModel($key, $input, array_replace($attributes, [$foreignKey => $this->getId()]));
protected function withRelation(
string $class,
string $key,
$input,
array $attributes
): self {
return $this->withModel($class, $input, $attributes, $key);
}
}
10 changes: 10 additions & 0 deletions tests/Factory/Concern/WithActive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace MyParcelNL\PrestaShop\Tests\Factory\Concern;

/**
* @method $this withActive(bool $active)
*/
interface WithActive { }
Loading

0 comments on commit 20559d6

Please sign in to comment.