<?php
namespace Harmonizely\EventListener;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
use Harmonizely\Model\User;
use Harmonizely\Service\Simplypay\Contract\ApplyFreeSubscriptionInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ApplyFreeSubscriptionListener implements EventSubscriberInterface
{
/**
* Logger interface
*
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* @var ApplyFreeSubscriptionInterface
*/
private ApplyFreeSubscriptionInterface $applyFreeSubscription;
/**
* Constructor
*
* @param LoggerInterface $logger
* @param ApplyFreeSubscriptionInterface $applyFreeSubscription
*/
public function __construct(
LoggerInterface $logger,
ApplyFreeSubscriptionInterface $applyFreeSubscription
)
{
$this->logger = $logger;
$this->applyFreeSubscription = $applyFreeSubscription;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array
{
return [
FOSUserEvents::REGISTRATION_CONFIRMED => 'confirmed',
];
}
/**
* On complete and confirm registration
*
* @param FilterUserResponseEvent $filterUserResponseEvent
* @param string $eventName
* @param EventDispatcherInterface $eventDispatcher
*/
public function confirmed(
FilterUserResponseEvent $filterUserResponseEvent,
string $eventName,
EventDispatcherInterface $eventDispatcher
)
{
$user = $filterUserResponseEvent->getUser();
if (!($user instanceof User)) {
$this->logger->error(
'Could not apply default free subscription, because invalid user provided'
);
return;
}
$this->applyFreeSubscription->perform($user);
}
}