container = $container; $this->em = $em; } protected function configure() { $this ->setName('app:Clean') ->setDescription('Nettoyage des données obsolètes') ->setHelp('Nettoyage des données obsolètes') ; } protected function execute(InputInterface $input, OutputInterface $output) { $this->output = $output; $this->filesystem = new Filesystem(); $this->rootlog = $this->container->get('kernel')->getLogDir()."/"; $alias = $this->container->getParameter('appAlias'); $this->writelnred(''); $this->writelnred('== app:Clean'); $this->writelnred('=========================================================================================================='); $now=new \DateTime('now'); $directory=$this->container->get('kernel')->getProjectDir()."/public/uploads/avatar"; $this->writeln($directory); $files=[]; $fs = new Filesystem(); if($fs->exists($directory)) { $finder = new Finder(); $finder->in($directory)->files(); foreach (iterator_to_array($finder) as $file) { $name = $file->getRelativePathname(); if($name!="admin.jpg"&&$name!="noavatar.png"&&$name!="system.jpg") { $entity=$this->em->getRepository("App\Entity\User")->findBy(["avatar"=>$name]); if(!$entity) { $this->writeln($name); $url=$directory."/".$name; if($fs->exists($url)) { $fs->remove($url); } } } } } $fs = new Filesystem(); $users=$this->em->getRepository("App\Entity\User")->findAll(); foreach($users as $user) { if(!$fs->exists($directory."/".$user->getAvatar())) { $this->writeln($user->getUsername()); $user->setAvatar("noavatar.png"); $this->em->persist($user); $this->em->flush(); } } $this->writeln(''); return Command::SUCCESS; } private function writelnred($string) { $this->output->writeln(''.$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"); } }