Skip to content

Commit

Permalink
Fix after reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
boherm committed May 20, 2024
1 parent 1602dc6 commit 8cc3709
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 323 deletions.
2 changes: 1 addition & 1 deletion classes/Carrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class CarrierCore extends ObjectModel
/** @var string URL with a '@' for */
public $url;

/** @var string Delay needed to deliver customer */
/** @var string[]|string Delay needed to deliver customer */
public $delay;

/** @var bool Carrier statuts */
Expand Down
18 changes: 14 additions & 4 deletions src/Adapter/Carrier/CommandHandler/AddCarrierHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Carrier;
use PrestaShop\PrestaShop\Adapter\Carrier\AbstractCarrierHandler;
use PrestaShop\PrestaShop\Adapter\Carrier\Repository\CarrierRepository;
use PrestaShop\PrestaShop\Adapter\Carrier\Validate\CarrierValidator;
use PrestaShop\PrestaShop\Adapter\File\Uploader\CarrierLogoFileUploader;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Carrier\Command\AddCarrierCommand;
use PrestaShop\PrestaShop\Core\Domain\Carrier\CommandHandler\AddCarrierHandlerInterface;
Expand All @@ -43,7 +45,9 @@
class AddCarrierHandler extends AbstractCarrierHandler implements AddCarrierHandlerInterface
{
public function __construct(
private readonly CarrierRepository $carrierRepository
private readonly CarrierRepository $carrierRepository,
private readonly CarrierLogoFileUploader $carrierLogoFileUploader,
private readonly CarrierValidator $carrierValidator,
) {
}

Expand All @@ -58,11 +62,17 @@ public function handle(AddCarrierCommand $command): CarrierId
$carrier->url = $command->getTrackingUrl();
$carrier->position = $command->getPosition();
$carrier->active = $command->getActive();
// @phpstan-ignore-next-line
$carrier->delay = $command->getLocalizedDelay();

$id = $this->carrierRepository->add($carrier);
$this->carrierValidator->validate($carrier);

return $id;
$carrierId = $this->carrierRepository->add($carrier);

if ($command->getLogoPathName() !== null) {
$this->carrierValidator->validateLogoUpload($command->getLogoPathName());
$this->carrierLogoFileUploader->upload($command->getLogoPathName(), $carrierId->getValue());
}

return $carrierId;
}
}
64 changes: 0 additions & 64 deletions src/Adapter/Carrier/CommandHandler/DeleteCarrierLogoHandler.php

This file was deleted.

24 changes: 19 additions & 5 deletions src/Adapter/Carrier/CommandHandler/EditCarrierHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
use Carrier;
use PrestaShop\PrestaShop\Adapter\Carrier\AbstractCarrierHandler;
use PrestaShop\PrestaShop\Adapter\Carrier\Repository\CarrierRepository;
use PrestaShop\PrestaShop\Adapter\Carrier\Validate\CarrierValidator;
use PrestaShop\PrestaShop\Adapter\File\Uploader\CarrierLogoFileUploader;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Carrier\Command\EditCarrierCommand;
use PrestaShop\PrestaShop\Core\Domain\Carrier\CommandHandler\EditCarrierHandlerInterface;
Expand All @@ -43,7 +45,9 @@
class EditCarrierHandler extends AbstractCarrierHandler implements EditCarrierHandlerInterface
{
public function __construct(
private readonly CarrierRepository $carrierRepository
private readonly CarrierRepository $carrierRepository,
private readonly CarrierLogoFileUploader $carrierLogoFileUploader,
private readonly CarrierValidator $carrierValidator,
) {
}

Expand All @@ -52,7 +56,7 @@ public function __construct(
*/
public function handle(EditCarrierCommand $command): CarrierId
{
$carrier = new Carrier();
$carrier = $this->carrierRepository->get($command->getCarrierId());
if ($command->getName()) {
$carrier->name = $command->getName();
}
Expand All @@ -69,12 +73,22 @@ public function handle(EditCarrierCommand $command): CarrierId
$carrier->active = $command->getActive();
}
if ($command->getLocalizedDelay()) {
// @phpstan-ignore-next-line
$carrier->delay = $command->getLocalizedDelay();
}

$id = $this->carrierRepository->addNewVersion($command->getCarrierId(), $carrier);
$this->carrierValidator->validate($carrier);

return $id;
$carrierId = $this->carrierRepository->updateInNewVersion($command->getCarrierId(), $carrier);

if ($command->getLogoPathName() !== null) {
$this->carrierLogoFileUploader->deleteOldFile($carrierId->getValue());

if ($command->getLogoPathName() !== '') {
$this->carrierValidator->validateLogoUpload($command->getLogoPathName());
$this->carrierLogoFileUploader->upload($command->getLogoPathName(), $carrierId->getValue());
}
}

return $carrierId;
}
}
97 changes: 0 additions & 97 deletions src/Adapter/Carrier/CommandHandler/UploadCarrierLogoHandler.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function handle(GetCarrierForEditing $query): EditableCarrier
$carrier->url,
$carrier->position,
$carrier->active,
$carrier->delay, // @phpstan-ignore-line
$carrier->delay,
);
}
}
25 changes: 3 additions & 22 deletions src/Adapter/Carrier/Repository/CarrierRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
use PrestaShop\PrestaShop\Core\Domain\Carrier\ValueObject\CarrierId;
use PrestaShop\PrestaShop\Core\Exception\CoreException;
use PrestaShop\PrestaShop\Core\Repository\AbstractMultiShopObjectModelRepository;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
* Provides access to carrier data source
Expand Down Expand Up @@ -73,11 +72,12 @@ public function add(Carrier $carrier): CarrierId
return new CarrierId($carrierId);
}

public function addNewVersion(CarrierId $carrierId, Carrier $carrier): CarrierId
public function updateInNewVersion(CarrierId $carrierId, Carrier $carrier): CarrierId
{
// Get old carrier to softly delete it
/** @var Carrier $oldCarrier */
$oldCarrier = $this->get($carrierId);
/** @var Carrier $newCarrier */
$newCarrier = $oldCarrier->duplicateObject();
$oldCarrier->deleted = true;
$this->partiallyUpdateObjectModel($oldCarrier, ['deleted'], CannotUpdateCarrierException::class);
Expand Down Expand Up @@ -105,28 +105,9 @@ public function addNewVersion(CarrierId $carrierId, Carrier $carrier): CarrierId
$newCarrier->deleted = false; // just to be sure...

// Copy all others information like ranges, shops associated, ...
$newCarrier->copyCarrierData($carrierId->getValue()); // @phpstan-ignore-line
$newCarrier->copyCarrierData($carrierId->getValue());
$this->updateObjectModel($newCarrier, CannotUpdateCarrierException::class);

return new CarrierId($newCarrier->id);
}

public function deleteLogo(CarrierId $carrierId): void
{
$oldLogo = _PS_SHIP_IMG_DIR_ . '/' . $carrierId->getValue() . '.jpg';
if (file_exists($oldLogo)) {
unlink($oldLogo);
}

$oldTmpLogo = _PS_TMP_IMG_DIR_ . '/carrier_mini_' . $carrierId->getValue() . '.jpg';
if (file_exists($oldTmpLogo)) {
unlink($oldTmpLogo);
}
}

public function uploadLogo(CarrierId $carrierId, UploadedFile $uploadedFile): void
{
$this->deleteLogo($carrierId);
$uploadedFile->move(_PS_SHIP_IMG_DIR_, $carrierId->getValue() . '.jpg');
}
}
Loading

0 comments on commit 8cc3709

Please sign in to comment.