<?php
namespace Harmonizely\EventListener;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
use Harmonizely\Model\User;
use Harmonizely\Service\Simplypay\Contract\MerchantApiClientFactoryInterface;
use Harmonizely\Service\Simplypay\Contract\SubscriptionHelperServiceInterface;
use Harmonizely\Service\Simplypay\DTO\Merchant\Request\ApplyFreeSubscriptionRequestDTO;
use Harmonizely\Service\Simplypay\DTO\Merchant\Request\CustomerRequestDTO;
use Harmonizely\Service\Simplypay\SimplypayClientException;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ApplyFreeSubscriptionListener implements EventSubscriberInterface
{
/**
* Subscription service
*
* @var SubscriptionHelperServiceInterface
*/
private SubscriptionHelperServiceInterface $subscriptionService;
/**
* Client factory
*
* @var MerchantApiClientFactoryInterface
*/
private MerchantApiClientFactoryInterface $clientFactory;
/**
* Logger interface
*
* @var LoggerInterface
*/
private LoggerInterface $logger;
/**
* Constructor
*
* @param SubscriptionHelperServiceInterface $subscriptionService
* @param MerchantApiClientFactoryInterface $clientFactory
* @param LoggerInterface $logger
*/
public function __construct(
SubscriptionHelperServiceInterface $subscriptionService,
MerchantApiClientFactoryInterface $clientFactory,
LoggerInterface $logger
)
{
$this->subscriptionService = $subscriptionService;
$this->clientFactory = $clientFactory;
$this->logger = $logger;
}
/**
* {@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
)
{
$client = $this->clientFactory->build(
$this->subscriptionService->getToken(),
$this->subscriptionService->getSecret(),
$this->subscriptionService->getMerchant()
);
try {
$user = $filterUserResponseEvent->getUser();
if (!($user instanceof User)) {
$this->logger->error(
'Could not apply default free subscription, because invalid user provided'
);
return;
}
$phone = '';
if ($user->getPhoneNumber()) {
$phone = $user->getPhoneNumber()->getCountryCode() . $user->getPhoneNumber()->getNationalNumber();
}
$customer = new CustomerRequestDTO(
$this->subscriptionService->encodeUserId($user->getSubscriptionUserId()),
$user->getFullName(), $user->getEmail(), $phone, $user->getCountryId(),
'', $user->getAddress(), $user->getCity(), $user->getZip(),
$user->getReferral(), \DateTimeImmutable::createFromMutable($user->getCreatedAt()),
!$user->getUserEmailUnsubscribe()
);
$request = new ApplyFreeSubscriptionRequestDTO(
$customer, $this->subscriptionService->getFreePlanCode(), null
);
$client->applyFreeSubscription(
$this->subscriptionService->getProject(), $request
);
} catch (SimplypayClientException $err) {
$this->logger->error('Could not apply default free subscription', [
'error' => $err->getMessage(),
'errors' => $err->getErrors(),
'user' => $filterUserResponseEvent->getUser()->getId(),
]);
}
}
}