<?php
namespace Aqarmap\Bundle\UserBundle\EventListener;
use Aqarmap\Bundle\CreditBundle\Constant\PackageUserType;
use Aqarmap\Bundle\UserBundle\Constant\UserTypes;
use Aqarmap\Bundle\UserBundle\Event\UserPackageEvent;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserPackageSubscriber implements EventSubscriberInterface
{
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
public static function getSubscribedEvents(): array
{
return [
UserPackageEvent::CREATED_EVENT => 'updateUserTypeOnUserPackageCreation',
];
}
/**
* Update User Type based on the purchased package.
* This supposes to be useless, as private owner can't buy companies' packages and vice versa. But, it happens.
*/
public function updateUserTypeOnUserPackageCreation(UserPackageEvent $event): void
{
$userPackage = $event->getUserPackage();
$user = $userPackage->getUser();
if (PackageUserType::INDIVIDUAL == $userPackage->getPackage()->getUserType()) {
$user->setUserType(UserTypes::INDIVIDUAL);
} elseif (PackageUserType::SMALL_DEVELOPER == $userPackage->getPackage()->getUserType()) {
$user->setUserType(UserTypes::SMALL_DEVELOPER);
} elseif (PackageUserType::COMPOUND_DEVELOPER == $userPackage->getPackage()->getUserType()) {
$user->setUserType(UserTypes::COMPOUND_DEVELOPER);
}
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}