Skip to content

Commit

Permalink
Add notification eligibility checker that checks the order actually h…
Browse files Browse the repository at this point in the history
…as items
  • Loading branch information
loevgaard committed Oct 8, 2024
1 parent 3a7dfe3 commit 0108942
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/EligibilityChecker/EligibilityCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,20 @@

namespace Setono\SyliusAbandonedCartPlugin\EligibilityChecker;

use Webmozart\Assert\Assert;

final class EligibilityCheck
{
public bool $eligible;

/** @var list<string> */
public array $reasons;

/**
* @param string|list<string> $reasons
*/
public function __construct(bool $eligible, $reasons = [])
public function __construct(public bool $eligible, array|string $reasons = [])
{
if (is_string($reasons)) {
$reasons = [$reasons];
}

/** @psalm-suppress RedundantConditionGivenDocblockType */
Assert::isArray($reasons);

$this->eligible = $eligible;
$this->reasons = $reasons;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusAbandonedCartPlugin\EligibilityChecker;

use Setono\SyliusAbandonedCartPlugin\Model\NotificationInterface;

final class OrderHasItemsNotificationEligibilityChecker implements NotificationEligibilityCheckerInterface
{
public function check(NotificationInterface $notification): EligibilityCheck
{
$order = $notification->getCart();
if (null === $order) {
return new EligibilityCheck(false, 'The order is not set');
}

if ($order->getItems()->isEmpty()) {
return new EligibilityCheck(false, 'The order has no items');
}

return new EligibilityCheck(true);
}
}
5 changes: 5 additions & 0 deletions src/Resources/config/services/eligibility_checker.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,10 @@
<service id="setono_sylius_abandoned_cart.notification_eligibility_checker.composite"
class="Setono\SyliusAbandonedCartPlugin\EligibilityChecker\CompositeNotificationEligibilityChecker">
</service>

<service id="setono_sylius_abandoned_cart.notification_eligibility_checker.order_has_items"
class="Setono\SyliusAbandonedCartPlugin\EligibilityChecker\OrderHasItemsNotificationEligibilityChecker">
<tag name="setono_sylius_abandoned_cart.notification_eligibility_checker"/>
</service>
</services>
</container>

0 comments on commit 0108942

Please sign in to comment.