Files
schedule/src/Command/AppInitCommand.php
2025-09-30 21:24:37 +02:00

146 lines
4.6 KiB
PHP
Executable File

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Input\ArrayInput;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Id\AssignedGenerator;
use App\Entity\User;
use App\Entity\Nature;
use App\Entity\Domaine;
class AppInitCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
public function __construct(ContainerInterface $container,EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:AppInit')
->setDescription('Init Data for App')
->setHelp('This command Init Data App')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getProjectDir()."/../var/log/";
$output->writeln('APP = Default Data');
// Création des natures par défaut
$this->insertNature(-200,"Congés",true);
$this->insertNature(-190,"Temps Partiel",false);
$this->insertNature(-100,"Non Travaillé",false);
$this->insertNature(-90,"Non Facturable",false);
$this->insertNature(-80,"Prestation",false);
// Création domaine par défaut
$this->insertDomaine(-100,"Administratif","Administratif");
// Création du compte admin si non existant
$this->insertUser("admin","admin");
$output->writeln('');
$this->em->flush();
return 0;
}
protected function insertNature($id,$name,$isvacation) {
$metadata = $this->em->getClassMetaData('App:Nature');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
$entity = $this->em->getRepository('App:Nature')->find($id);
if(!$entity) {
$entity = new Nature;
$entity->setId($id);
$entity->setName($name);
$entity->setIsvacation($isvacation);
$this->em->persist($entity);
$this->em->flush();
}
}
protected function insertDomaine($id,$category,$name) {
$metadata = $this->em->getClassMetaData('App:Domaine');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
$entity = $this->em->getRepository('App:Domaine')->find($id);
if(!$entity) {
$entity = new Domaine;
$entity->setId($id);
$entity->setName($name);
$entity->setCategory($category);
$this->em->persist($entity);
$this->em->flush();
}
}
protected function insertUser() {
$metadata = $this->em->getClassMetaData('App:User');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
// Job Récupératoin la table de référence des articles
// Toute les 1mn
$entity = $this->em->getRepository('App:User')->findOneBy(["username"=>"admin"]);
if(!$entity) {
$this->writelnred('Création du compte admin par défaut avec password admin - Veuillez modifier votre password admin après connexion');
$entity = new User;
$entity->setId(0);
$entity->setUsername("admin");
$entity->setPassword("admin");
$entity->setFirstname("schedule");
$entity->setLastname("admin");
$entity->setEmail("admin@noreply@com");
$entity->setRoles(["ROLE_ADMIN"]);
$entity->setAvatar("admin.jpg");
$this->em->persist($entity);
}
// On flush
$this->em->flush();
}
private function writelnred($string) {
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string) {
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}