ninefolio/src/Service/UserSubscriber.php

109 lines
3.5 KiB
PHP
Raw Normal View History

2024-10-26 12:08:33 +02:00
<?php
namespace App\Service;
use App\Entity\User;
use App\Entity\UserGroup;
use App\Entity\Category;
use App\Entity\Config;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Ramsey\Uuid\Uuid;
class UserSubscriber implements EventSubscriberInterface
{
private $em;
private $user;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function getSubscribedEvents(): array
{
return [
Events::postPersist,
Events::postUpdate,
];
}
public function postPersist(LifecycleEventArgs $args): void
{
$this->upinsertUser($args);
}
public function postUpdate(LifecycleEventArgs $args): void
{
$this->upinsertUser($args);
}
private function upinsertUser(LifecycleEventArgs $args): void {
$this->user = $args->getObject();
if (!$this->user instanceof User) {
return;
}
// On s'assure que le user a au moins une category d'illustration
$this->insertCategory();
// On initialise l'ensemble des config customizable
$this->insertConfig();
}
private function insertCategory(): void {
$user=$this->user;
$category=$entity = $this->em->getRepository('App:Category')->findOneBy(["user"=>$this->user]);
if(!$category) {
$category = new Category;
$category->setOrder(1);
$category->setName("Illustrations");
$category->setUsecategoryconfig(false);
$category->setAppthumbfilter(false);
$category->setAppthumbheight(0);
$category->setAppthumbwidth(0);
$category->setAppthumbfilter(false);
$category->setAppthumbfiltergrayscale(100);
$category->setAppthumbfilteropacity(100);
$category->setAppthumbfiltersepia(0);
$category->setUser($this->user);
$this->em->persist($category);
$this->em->flush();
$this->user=$user;
}
dump($this->user);
}
private function insertConfig(): void {
$user=$this->user;
$configs=$entity = $this->em->getRepository('App:Config')->findBy(["user"=>null,"customizable"=>true]);
foreach($configs as $config) {
$custo=$this->em->getRepository('App:Config')->findBy(["user"=>$user,"keyid"=>$config->getKeyid()]);
if(!$custo) {
$custo=new Config;
$custo->setKeyid($config->getKeyid());
$custo->setValue(null);
$custo->setDefvalue(null);
$custo->setCategory($config->getCategory());
$custo->setOrder($config->getOrder());
$custo->setTitle($config->getTitle());
$custo->setType($config->getType());
$custo->setVisible($config->getVisible());
$custo->setChangeable($config->getChangeable());
$custo->setRequired($config->getRequired());
$custo->setGrouped($config->getGrouped());
$custo->setHelp($config->getHelp());
$custo->setCustomizable($config->getCustomizable());
$custo->setUser($user);
$this->em->persist($custo);
$this->em->flush();
$this->user=$user;
}
}
}
}