<?php
namespace App\EventSubscriber;
use DateTime;
use App\Entity\Staff;
use App\Entity\Business;
use App\Entity\Customer;
use App\Repository\CustomerRepository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Security;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use Symfony\Component\HttpFoundation\RedirectResponse;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class EntitySubscriber implements EventSubscriberInterface
{
private $encoder;
private $customer_repository;
private $user;
private $url;
public function __construct(UserPasswordHasherInterface $encoder, ManagerRegistry $registry, Security $security, AdminUrlGenerator $adminUrlGenerator)
{
$this->encoder = $encoder;
$this->customer_repository = new CustomerRepository($registry);
$this->user = $security->getUser();
$this->url = $adminUrlGenerator;
}
public function hashPassword($event)
{
$entity = $event->getEntityInstance();
if (property_exists($entity, "password")) {
$password = $this->encoder->hashPassword($entity, $entity->getPassword());
$entity->setPassword($password);
}
}
public function updatePassword($event)
{
$entity = $event->getEntityInstance();
if (property_exists($entity, "businessUser")) {
$user = $entity->getBusinessUser();
if ($user->newPassword) {
$password = $this->encoder->hashPassword($user, $user->newPassword);
$user->setPassword($password);
}
} else if (property_exists($entity, "newPassword")) {
if ($entity->newPassword) {
$password = $this->encoder->hashPassword($entity, $entity->newPassword);
$entity->setPassword($password);
}
}
}
public function setCreatedAt($event)
{
$entity = $event->getEntityInstance();
if (property_exists($entity, "date")) {
$now = new DateTime("now");
$entity->setDate($now);
}
}
public function setCustomerNumber($event)
{
$entity = $event->getEntityInstance();
if (!$entity instanceof Customer) {
return;
}
$customer_number = '';
for ($i = 0; $i < 12; $i++) {
$customer_number .= mt_rand(0, 9);
}
$customer_number = implode('-', str_split($customer_number, 3));
$customer = $this->customer_repository->findOneByCustomerNumber($customer_number);
if ($customer) {
return $this->setCustomerNumber($event);
} else {
$entity->setCustomerNumber($customer_number);
}
}
public function setStaffBusiness($event)
{
$entity = $event->getEntityInstance();
if (!$entity instanceof Staff) {
return;
}
if (in_array("ROLE_BUSINESS", $this->user->getRoles())) {
$business = $this->user->getBusiness();
$entity->setBusiness($business);
}
}
public function onPersistedEvent($event)
{
$this->checkBusinessUserStep($event);
$this->setCustomerNumber($event);
$this->setCreatedAt($event);
$this->hashPassword($event);
$this->setStaffBusiness($event);
}
public function onUpdatedEvent($event)
{
$this->setCreatedAt($event);
$this->updatePassword($event);
}
public function afterUpdatedEvent($event)
{
$this->checkBusinessUserStep($event);
}
public function checkBusinessUserStep($event)
{
$entity = $event->getEntityInstance();
if (!$entity instanceof Business)
return;
if (!$entity->getBusinessUser()->getId()) {
$url = $this->url
->setController(BusinessUserCrudController::class)
->setAction(Action::NEW)
->set('id_business', $entity->getId())
->generateUrl();
$event->setController(function () {
return new RedirectResponse($url);
});
}
}
public static function getSubscribedEvents()
{
return [
BeforeEntityUpdatedEvent::class => 'onUpdatedEvent',
BeforeEntityPersistedEvent::class => 'onPersistedEvent',
AfterEntityUpdatedEvent::class => 'afterUpdatedEvent'
];
}
}