Skip to content

Commit

Permalink
Fix after new comments
Browse files Browse the repository at this point in the history
  • Loading branch information
boherm committed Feb 7, 2024
1 parent c4f68ae commit 236e4c7
Show file tree
Hide file tree
Showing 25 changed files with 175 additions and 399 deletions.
2 changes: 1 addition & 1 deletion admin-dev/themes/new-theme/js/pages/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* vCopyright since 2007 PrestaShop SA and Contributors
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
Expand Down
62 changes: 39 additions & 23 deletions src/Adapter/Cart/CommandHandler/BulkDeleteCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,67 @@
use PrestaShop\PrestaShop\Adapter\Cart\Repository\CartRepository;
use PrestaShop\PrestaShop\Adapter\Order\Repository\OrderRepository;
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\AbstractBulkCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Cart\Command\BulkDeleteCartCommand;
use PrestaShop\PrestaShop\Core\Domain\Cart\CommandHandler\BulkDeleteCartHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\BulkCartException;
use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\CannotDeleteOrderedCartException;
use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\CartException;
use PrestaShop\PrestaShop\Core\Domain\Cart\ValueObject\CartId;
use PrestaShop\PrestaShop\Core\Domain\Exception\BulkCommandExceptionInterface;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderNotFoundException;
use PrestaShop\PrestaShop\Core\Exception\CoreException;

/**
* Deletes cart in bulk action using legacy object model
*/
#[AsCommandHandler]
final class BulkDeleteCartHandler implements BulkDeleteCartHandlerInterface
class BulkDeleteCartHandler extends AbstractBulkCommandHandler implements BulkDeleteCartHandlerInterface
{
/**
* @var CartRepository
*/
private $cartRepository;
public function __construct(
protected readonly CartRepository $cartRepository,
protected readonly OrderRepository $orderRepository
) {
}

/**
* @var OrderRepository
* {@inheritdoc}
*
* @throws CartException
* @throws CoreException
*/
private $orderRepository;
public function handle(BulkDeleteCartCommand $command): void
{
$this->handleBulkAction($command->getCartIds(), CartException::class);
}

/**
* @param CartRepository $cartRepository
*/
public function __construct(CartRepository $cartRepository, OrderRepository $orderRepository)
protected function buildBulkException(array $caughtExceptions): BulkCommandExceptionInterface
{
$this->cartRepository = $cartRepository;
$this->orderRepository = $orderRepository;
return new BulkCartException(
$caughtExceptions,
'Errors occurred during Alias bulk delete action',
);
}

/**
* {@inheritdoc}
* @param CartId $id
* @param mixed $command
*
* @throws CartException
* @throws CoreException
* @return void
*/
public function handle(BulkDeleteCartCommand $command): void
protected function handleSingleAction(mixed $id, mixed $command): void
{
foreach ($command->getCartIds() as $cartId) {
$order = $this->orderRepository->findByCartId($cartId);
if ($order) {
throw new CannotDeleteOrderedCartException(sprintf('Cart "%s" with order cannot be deleted.', $cartId->getValue()));
}
$this->cartRepository->delete($cartId);
try {
$this->orderRepository->getByCartId($id);
throw new CannotDeleteOrderedCartException(sprintf('Cart "%s" with order cannot be deleted.', $id->getValue()));
} catch (OrderNotFoundException $e) {
// Cart is not linked to any order, we can safely delete it
$this->cartRepository->delete($id);
}
}

protected function supports($id): bool
{
return $id instanceof CartId;
}
}
32 changes: 11 additions & 21 deletions src/Adapter/Cart/CommandHandler/DeleteCartHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,19 @@
use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\CannotDeleteCartException;
use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\CannotDeleteOrderedCartException;
use PrestaShop\PrestaShop\Core\Domain\Cart\Exception\CartException;
use PrestaShop\PrestaShop\Core\Domain\Order\Exception\OrderNotFoundException;
use PrestaShop\PrestaShop\Core\Exception\CoreException;

/**
* Handles deletion of cart using legacy object model
*/
#[AsCommandHandler]
final class DeleteCartHandler extends AbstractCartHandler implements DeleteCartHandlerInterface
class DeleteCartHandler extends AbstractCartHandler implements DeleteCartHandlerInterface
{
/**
* @var CartRepository
*/
private $cartRepository;

/**
* @var OrderRepository
*/
private $orderRepository;

/**
* @param CartRepository $cartRepository
*/
public function __construct(CartRepository $cartRepository, OrderRepository $orderRepository)
{
$this->cartRepository = $cartRepository;
$this->orderRepository = $orderRepository;
public function __construct(
protected readonly CartRepository $cartRepository,
protected readonly OrderRepository $orderRepository
) {
}

/**
Expand All @@ -74,10 +62,12 @@ public function __construct(CartRepository $cartRepository, OrderRepository $ord
*/
public function handle(DeleteCartCommand $command): void
{
$order = $this->orderRepository->findByCartId($command->getCartId());
if ($order) {
try {
$this->orderRepository->getByCartId($command->getCartId());
throw new CannotDeleteOrderedCartException(sprintf('Cart "%s" with order cannot be deleted.', $command->getCartId()->getValue()));
} catch (OrderNotFoundException $e) {
// Cart is not linked to any order, we can safely delete it
$this->cartRepository->delete($command->getCartId());
}
$this->cartRepository->delete($command->getCartId());
}
}
62 changes: 14 additions & 48 deletions src/Adapter/Kpi/AbandonedCartKpi.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
use HelperKpi;
use PrestaShop\PrestaShop\Adapter\LegacyContext;
use PrestaShop\PrestaShop\Core\ConfigurationInterface;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Domain\Cart\CartStatus;
use PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagSettings;
use PrestaShop\PrestaShop\Core\FeatureFlag\FeatureFlagStateCheckerInterface;
use PrestaShop\PrestaShop\Core\Kpi\KpiInterface;
use PrestaShopBundle\Entity\Repository\FeatureFlagRepository;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -41,58 +42,22 @@
*/
final class AbandonedCartKpi implements KpiInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @var LegacyContext
*/
private $contextAdapter;

/**
* @var UrlGeneratorInterface
*/
private $router;

/**
* @var FeatureFlagRepository
*/
private $featureFlag;

/**
* @param TranslatorInterface $translator
* @param ConfigurationInterface $configuration
* @param LegacyContext $contextAdapter
* @param UrlGeneratorInterface $router
* @param FeatureFlagRepository $featureFlag
*/
public function __construct(
TranslatorInterface $translator,
ConfigurationInterface $configuration,
LegacyContext $contextAdapter,
UrlGeneratorInterface $router,
FeatureFlagRepository $featureFlag
private readonly LegacyContext $contextAdapter,
private readonly TranslatorInterface $translator,
private readonly ConfigurationInterface $configuration,
private readonly LanguageContext $languageContext,
private readonly UrlGeneratorInterface $router,
private readonly FeatureFlagStateCheckerInterface $flagStateChecker,
) {
$this->translator = $translator;
$this->configuration = $configuration;
$this->contextAdapter = $contextAdapter;
$this->router = $router;
$this->featureFlag = $featureFlag;
}

/**
* {@inheritdoc}
*/
public function render()
{
$dateFormat = $this->contextAdapter->getLanguage()->date_format_lite;
$dateFormat = $this->languageContext->getDateFormat();

$helper = new HelperKpi();
$helper->id = 'box-carts';
Expand All @@ -103,14 +68,15 @@ public function render()
'%date1%' => date($dateFormat, strtotime('-2 day')),
'%date2%' => date($dateFormat, strtotime('-1 day')),
], 'Admin.Orderscustomers.Feature');
$helper->href = $this->contextAdapter->getAdminLink('AdminCarts', true, [
'action' => 'filterOnlyAbandonedCarts',
]);

if ($this->featureFlag->isEnabled(FeatureFlagSettings::FEATURE_FLAG_CARTS)) {
if ($this->flagStateChecker->isEnabled(FeatureFlagSettings::FEATURE_FLAG_CARTS)) {
$helper->href = $this->router->generate('admin_carts_index', [
'cart[filters][status]' => CartStatus::ABANDONED_CART,
]);
} else {
$helper->href = $this->contextAdapter->getAdminLink('AdminCarts', true, [
'action' => 'filterOnlyAbandonedCarts',
]);
}

if ($this->configuration->get('ABANDONED_CARTS') !== false) {
Expand Down
29 changes: 3 additions & 26 deletions src/Adapter/Kpi/AverageOrderValueKpi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,11 @@
*/
final class AverageOrderValueKpi implements KpiInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var ShopConfigurationInterface
*/
private $configuration;

/**
* @var LegacyContext
*/
private $contextAdapter;

/**
* @param TranslatorInterface $translator
* @param ShopConfigurationInterface $configuration
* @param LegacyContext $contextAdapter
*/
public function __construct(
TranslatorInterface $translator,
ShopConfigurationInterface $configuration,
LegacyContext $contextAdapter
private readonly TranslatorInterface $translator,
private readonly ShopConfigurationInterface $configuration,
private readonly LegacyContext $contextAdapter
) {
$this->translator = $translator;
$this->configuration = $configuration;
$this->contextAdapter = $contextAdapter;
}

/**
Expand Down
29 changes: 3 additions & 26 deletions src/Adapter/Kpi/ConversionRateKpi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,11 @@
*/
final class ConversionRateKpi implements KpiInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @var LegacyContext
*/
private $contextAdapter;

/**
* @param TranslatorInterface $translator
* @param ConfigurationInterface $configuration
* @param LegacyContext $contextAdapter
*/
public function __construct(
TranslatorInterface $translator,
ConfigurationInterface $configuration,
LegacyContext $contextAdapter
private readonly TranslatorInterface $translator,
private readonly ConfigurationInterface $configuration,
private readonly LegacyContext $contextAdapter
) {
$this->translator = $translator;
$this->configuration = $configuration;
$this->contextAdapter = $contextAdapter;
}

/**
Expand Down
29 changes: 3 additions & 26 deletions src/Adapter/Kpi/NetProfitPerVisitKpi.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,11 @@
*/
final class NetProfitPerVisitKpi implements KpiInterface
{
/**
* @var TranslatorInterface
*/
private $translator;

/**
* @var ConfigurationInterface
*/
private $configuration;

/**
* @var LegacyContext
*/
private $contextAdapter;

/**
* @param TranslatorInterface $translator
* @param ConfigurationInterface $configuration
* @param LegacyContext $contextAdapter
*/
public function __construct(
TranslatorInterface $translator,
ConfigurationInterface $configuration,
LegacyContext $contextAdapter
private readonly TranslatorInterface $translator,
private readonly ConfigurationInterface $configuration,
private readonly LegacyContext $contextAdapter
) {
$this->translator = $translator;
$this->configuration = $configuration;
$this->contextAdapter = $contextAdapter;
}

/**
Expand Down

0 comments on commit 236e4c7

Please sign in to comment.