<?php
/**
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
* @package LnbSubscription
* @author Michael Lämmlein <michael.laemmlein@liebscher-bracht.com>
* @copyright ©2023 Liebscher & Bracht
* @license http://www.opensource.org/licenses/mit-license.php MIT-License
* @version 1.0.0
* @since 21.03.23
*/
declare(strict_types=1);
namespace Lnb\Shopware6\LnbSubscription\Subscriber\Subscription;
use Lnb\Shopware6\LnbSubscription\Event\OrderCreatedEvent;
use Lnb\Shopware6\LnbSubscription\Event\SubscriptionCreatedEvent;
use Lnb\Shopware6\LnbSubscription\Service\Loyalty\SubscriptionLoyaltyUpdateService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
readonly class LoyaltyUpdateSubscriber implements EventSubscriberInterface
{
public function __construct(
private SubscriptionLoyaltyUpdateService $subscriptionLoyaltyUpdateService
) {
}
public static function getSubscribedEvents(): array
{
return [
SubscriptionCreatedEvent::class => [
['onInitialLoyalty'],
],
OrderCreatedEvent::class => [
['onUpdateNextLoyalty', -1],
],
];
}
public function onInitialLoyalty(SubscriptionCreatedEvent $event): void
{
$this->subscriptionLoyaltyUpdateService->updateNextLoyalty(
$event->getSubscriptionId(),
$event->getContext()
);
}
public function onUpdateNextLoyalty(OrderCreatedEvent $event): void
{
$this->subscriptionLoyaltyUpdateService->updateNextLoyalty(
$event->getSubscriptionOrderEntity()->getId(),
$event->getContext()
);
}
}