src/EventSubscriber/EasyAdminSubscriber.php line 60
<?phpnamespace App\EventSubscriber;use App\Entity\AdditionalCompanyInformation;use App\Entity\Joker;use App\Entity\LotNumber;use App\Entity\Products;use App\Entity\Rebate;use App\Entity\User;use Doctrine\ORM\EntityManagerInterface;use EasyCorp\Bundle\EasyAdminBundle\Event\AbstractLifecycleEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;class EasyAdminSubscriber implements EventSubscriberInterface{private EntityManagerInterface $manager;private UserPasswordHasherInterface $userPasswordHasher;public function __construct(EntityManagerInterface $manager, UserPasswordHasherInterface $userPasswordHasher){$this->manager = $manager;$this->userPasswordHasher = $userPasswordHasher;}public static function getSubscribedEvents(): array{return [AfterEntityPersistedEvent::class => [['product_setOnlineStock'], ['user_addRelations'], ['rebate_setRebateProducts']],AfterEntityUpdatedEvent::class => [['product_setOnlineStock'], ['rebate_setRebateProducts']],BeforeEntityPersistedEvent::class => ['user_setUserPassword'],];}public function user_setUserPassword(AbstractLifecycleEvent $event){$user = $event->getEntityInstance();if (!($user instanceof User)) {return;}$pass = User::generatePassword();$user->setGeneratePasswordRecovery($pass);$user->setPassword($this->userPasswordHasher->hashPassword($user,$pass));return $user;}public function user_addRelations(AbstractLifecycleEvent $event){$user = $event->getEntityInstance();if (!($user instanceof User)) {return;}$this->manager->getRepository(AdditionalCompanyInformation::class)->saveDefault($user);$this->manager->getRepository(Joker::class)->saveDefault($user);}public function product_setOnlineStock(AbstractLifecycleEvent $event){$product = $event->getEntityInstance();if (!($product instanceof Products)) {return;}$this->manager->getRepository(LotNumber::class)->setOnlineStock($product);}public function rebate_setRebateProducts(AbstractLifecycleEvent $event){$rebate = $event->getEntityInstance();if (!($rebate instanceof Rebate)) {return;}if (count($rebate->getSpecificProducts()) > 0) {$rebate->setExcludedProducts([]);$specific = $rebate->getSpecificProducts();$normalRebates = $this->manager->getRepository(Rebate::class)->getActiveNormalRebates();foreach ($normalRebates as $rebate) {$excluded = $rebate->getExcludedProducts();$rebate->setExcludedProducts(array_unique([...$excluded, ...$specific]));}} else {$specificRebates = $this->manager->getRepository(Rebate::class)->getActiveSpecificRebates();$specificExcluded = array_unique(array_reduce($specificRebates, function ($acc, $rebate) {$specific = $rebate->getSpecificProducts();if ($specific) {array_push($acc, ...$specific);}return $acc;}, []));$excluded = $rebate->getExcludedProducts();$rebate->setExcludedProducts(array_unique([...$excluded, ...$specificExcluded]));$rebate->setSpecificProducts([]);}$this->manager->flush();}}