Skip to content

Commit

Permalink
fix: improve entities and carrier logic
Browse files Browse the repository at this point in the history
  • Loading branch information
EdieLemoine committed Sep 14, 2023
1 parent 7714efd commit b61d197
Show file tree
Hide file tree
Showing 58 changed files with 1,137 additions and 653 deletions.
7 changes: 7 additions & 0 deletions config/pdk.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MyParcelNL\Pdk\Api\Contract\ClientAdapterInterface;
use MyParcelNL\Pdk\App\Account\Contract\PdkAccountRepositoryInterface;
use MyParcelNL\Pdk\App\Action\Backend\Account\UpdateAccountAction;
use MyParcelNL\Pdk\App\Api\Contract\BackendEndpointServiceInterface;
use MyParcelNL\Pdk\App\Api\Contract\FrontendEndpointServiceInterface;
use MyParcelNL\Pdk\App\Cart\Contract\PdkCartRepositoryInterface;
Expand All @@ -29,6 +30,7 @@
use MyParcelNL\PrestaShop\Contract\PsCarrierServiceInterface;
use MyParcelNL\PrestaShop\Contract\PsOrderServiceInterface;
use MyParcelNL\PrestaShop\Pdk\Account\Repository\PsPdkAccountRepository;
use MyParcelNL\PrestaShop\Pdk\Action\Backend\Account\PsUpdateAccountAction;
use MyParcelNL\PrestaShop\Pdk\Api\Adapter\Guzzle7ClientAdapter;
use MyParcelNL\PrestaShop\Pdk\Api\Service\PsBackendEndpointService;
use MyParcelNL\PrestaShop\Pdk\Api\Service\PsFrontendEndpointService;
Expand Down Expand Up @@ -95,6 +97,11 @@
PdkWebhookServiceInterface::class => get(PsWebhookService::class),
PdkWebhooksRepositoryInterface::class => get(PsWebhooksRepository::class),

/**
* Actions
*/
UpdateAccountAction::class => get(PsUpdateAccountAction::class),

/**
* Miscellaneous
*/
Expand Down
89 changes: 31 additions & 58 deletions src/Carrier/Service/CarrierBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@

use Carrier as PsCarrier;
use Context;
use Db;
use Group;
use Language as PsLanguage;
use MyParcelNL\Pdk\Base\Support\Arr;
use MyParcelNL\Pdk\Carrier\Model\Carrier;
use MyParcelNL\Pdk\Facade\Language;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\PrestaShop\Database\Table;
use MyParcelNL\PrestaShop\Repository\PsCarrierMappingRepository;
use ObjectModel;
use RangePrice;
Expand Down Expand Up @@ -53,37 +51,35 @@ public function __construct(Carrier $myParcelCarrier)
}

/**
* @return void
* @return \Carrier
* @throws \Doctrine\ORM\ORMException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function create(): void
public function create(): PsCarrier
{
$this->psCarrier = $this->createCarrier();
$this->createCarrier();

$this->addGroups();
$this->addRanges();
$this->addZones();

$this->psCarrier->update();

$this->addCarrierMapping();

return $this->psCarrier;
}

/**
* @throws \Doctrine\ORM\ORMException
*/
private function addCarrierMapping(): void
{
$this->carrierMappingRepository->updateOrCreate(
[
'myparcelCarrier' => $this->myParcelCarrier->externalIdentifier,
],
[
'idCarrier' => (int) $this->psCarrier->id,
]
);
$values = [
'carrierId' => (int) $this->psCarrier->id,
'myparcelCarrier' => $this->myParcelCarrier->externalIdentifier,
];

$this->carrierMappingRepository->updateOrCreate($values, $values);
}

/**
Expand All @@ -107,7 +103,11 @@ private function addRanges(): void
{
/** @var RangeWeight|RangePrice $objectClass */
foreach (self::RANGE_CLASSES as $objectClass) {
$this->deleteExistingRanges($objectClass);
$hasExistingRanges = $objectClass::getRanges($this->psCarrier->id);

if ($hasExistingRanges) {
continue;
}

$instance = new $objectClass();

Expand All @@ -124,13 +124,13 @@ private function addRanges(): void
*/
private function addZones(): void
{
$existingZones = Arr::pluck($this->psCarrier->getZones(), 'id_zone');
$existingZones = $this->psCarrier->getZones();

foreach (Zone::getZones() as $zone) {
if (in_array($zone['id_zone'], $existingZones, true)) {
continue;
}
if ($existingZones) {
return;
}

foreach (Zone::getZones() as $zone) {
if ($this->psCarrier->addZone($zone['id_zone'])) {
continue;
}
Expand All @@ -140,11 +140,11 @@ private function addZones(): void
}

/**
* @return PsCarrier
* @return void
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
private function createCarrier(): PsCarrier
private function createCarrier(): void
{
/** @var \MyParcelNL $module */
$module = Pdk::get('moduleInstance');
Expand All @@ -162,15 +162,13 @@ private function createCarrier(): PsCarrier
$psCarrier->shipping_external = true;
$psCarrier->shipping_method = 2;

// TODO: add logo

foreach (PsLanguage::getLanguages() as $lang) {
$psCarrier->delay[$lang['id_lang']] = Language::translate('carrier_delivery_time', $lang['iso_code']);
}

$this->updateOrAdd($psCarrier, (bool) $psCarrier->id);

return $psCarrier;
$this->psCarrier = $psCarrier;
}

/**
Expand All @@ -180,27 +178,7 @@ private function createCarrierIdReference(): int
{
$carrierId = str_pad((string) $this->myParcelCarrier->id, 3, '0');

return (int) ($carrierId . $this->myParcelCarrier->externalIdentifier);
}

/**
* @param class-string<RangeWeight|RangePrice> $objectClass
*
* @return void
*/
private function deleteExistingRanges(string $objectClass): void
{
$existing = $objectClass::getRanges($this->psCarrier->id);

if (! $existing) {
return;
}

$definition = $objectClass::getDefinition($objectClass);
$table = Table::withPrefix($definition['table']);

Db::getInstance()
->execute("DELETE FROM `$table` WHERE `id_carrier` = {$this->psCarrier->id}");
return (int) ($carrierId . $this->myParcelCarrier->subscriptionId);
}

/**
Expand All @@ -211,18 +189,13 @@ private function getExistingPsCarrier(): ?PsCarrier
$mapping = $this->carrierMappingRepository
->findOneBy(['myparcelCarrier' => $this->myParcelCarrier->externalIdentifier]);

return $mapping ? new PsCarrier($mapping->idCarrier) : null;
}
if ($mapping) {
return new PsCarrier($mapping->getCarrierId());
}

/**
* @param int $id
* @param string $class
*
* @return \ObjectModel
*/
private function getOrCreateModel(int $id, string $class): ObjectModel
{
return new $class($id) ?? new $class();
$existingCarrier = PsCarrier::getCarrierByReference($this->createCarrierIdReference());

return $existingCarrier ?: null;
}

/**
Expand Down
14 changes: 4 additions & 10 deletions src/Contract/PsCarrierServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace MyParcelNL\PrestaShop\Contract;

use Carrier as PsCarrier;
use MyParcelNL\Pdk\Base\Support\Collection;
use MyParcelNL\Pdk\Carrier\Collection\CarrierCollection;
use MyParcelNL\Pdk\Carrier\Model\Carrier;

Expand All @@ -13,21 +14,14 @@ interface PsCarrierServiceInterface
/**
* @param \MyParcelNL\Pdk\Carrier\Collection\CarrierCollection $carriers
*
* @return void
*/
public function createOrUpdateCarriers(CarrierCollection $carriers): void;

/**
* @return void
* @return \MyParcelNL\Pdk\Base\Support\Collection
*/
public function deleteCarriers(): void;
public function createOrUpdateCarriers(CarrierCollection $carriers): Collection;

/**
* @param $carriers
*
* @return void
*/
public function deleteUnusedCarriers($carriers): void;
public function disableCarriers(): void;

/**
* @param int|PsCarrier $input
Expand Down
14 changes: 1 addition & 13 deletions src/Controller/PdkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function index(): Response
return new Response($e->getMessage(), 400);
}

$this->flushEntityManager();
EntityManager::flush();

return $response;
}
Expand All @@ -52,16 +52,4 @@ protected function createNormalizedRequest(): Request

return $request;
}

/**
* @return void
*/
private function flushEntityManager(): void
{
if (! EntityManager::isOpen()) {
return;
}

EntityManager::flush();
}
}
18 changes: 8 additions & 10 deletions src/Database/AbstractDatabaseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Db;
use MyParcelNL\Pdk\App\Installer\Contract\MigrationInterface;
use MyParcelNL\Pdk\Facade\Logger;
use MyParcelNL\PrestaShop\Database\Sql\Contract\SqlBuilderInterface;

abstract class AbstractDatabaseMigration implements MigrationInterface
{
Expand All @@ -16,22 +17,19 @@ public function getVersion(): string
}

/**
* @param string $sql
* @param string|\MyParcelNL\PrestaShop\Database\Sql\Contract\SqlBuilderInterface $sql
*
* @return void
*/
protected function execute(string $sql): void
protected function execute($sql): void
{
$replacedSql = strtr($sql, [
'{ENGINE}' => _MYSQL_ENGINE_,
]);

$trimmedSql = str_replace("\n", ' ', $replacedSql);
$trimmedSql = trim(preg_replace('/\s+/m', ' ', $trimmedSql));
if ($sql instanceof SqlBuilderInterface) {
$sql = $sql->build();
}

Db::getInstance(_PS_USE_SQL_SLAVE_)
->execute($trimmedSql);
->execute($sql);

Logger::debug('Query executed', ['class' => static::class, 'sql' => $trimmedSql]);
Logger::debug('Query executed', ['class' => static::class, 'sql' => $sql]);
}
}
26 changes: 10 additions & 16 deletions src/Database/CreateCarrierMappingTableDatabaseMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,23 @@

namespace MyParcelNL\PrestaShop\Database;

use MyParcelNL\PrestaShop\Database\Sql\CreateTableSqlBuilder;
use MyParcelNL\PrestaShop\Database\Sql\DropTableSqlBuilder;
use MyParcelNL\PrestaShop\Entity\MyparcelnlCarrierMapping;

final class CreateCarrierMappingTableDatabaseMigration extends AbstractDatabaseMigration
{
public function down(): void
{
$table = $this->getTable();
$this->execute("DROP TABLE IF EXISTS `$table`");
$this->execute(new DropTableSqlBuilder($this->getTable()));
}

public function up(): void
{
$table = $this->getTable();
$sql = <<<SQL
CREATE TABLE IF NOT EXISTS `$table` (
`id` INT AUTO_INCREMENT NOT NULL,
`id_carrier` INT NOT NULL,
`myparcel_carrier` VARCHAR(32) NOT NULL,
`created` DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL,
`updated` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL,
UNIQUE INDEX UNIQ_B515571199A4586C (`id_carrier`),
UNIQUE INDEX UNIQ_B5155711A4D607B2 (`myparcel_carrier`),
PRIMARY KEY (`id`)
) ENGINE={ENGINE} DEFAULT CHARSET=utf8;
SQL;
$sql = (new CreateTableSqlBuilder($this->getTable()))
->id('carrier_id')
->column('myparcel_carrier', 'VARCHAR(32)')
->primary(['carrier_id', 'myparcel_carrier']);

$this->execute($sql);
}
Expand All @@ -36,6 +30,6 @@ public function up(): void
*/
private function getTable(): string
{
return Table::withPrefix(Table::TABLE_CARRIER_MAPPING);
return MyparcelnlCarrierMapping::getTable();
}
}
Loading

0 comments on commit b61d197

Please sign in to comment.