setName('Core:PurgeServer') ->setDescription('Purge Server not updated') ->setHelp('This command Purge the obsolete Server') ->addArgument('cronid', InputArgument::OPTIONAL, 'ID Cron Job') ->addArgument('lastchance', InputArgument::OPTIONAL, 'Lastchance to run the cron') ; } protected function execute(InputInterface $input, OutputInterface $output) { $this->container = $this->getApplication()->getKernel()->getContainer(); $this->em = $this->container->get('doctrine')->getEntityManager(); $this->output = $output; $this->filesystem = new Filesystem(); $this->rootlog = $this->container->get('kernel')->getRootDir()."/../var/logs/"; $alias = $this->container->getParameter('alias'); $this->writelnred(''); $this->writelnred('== Core:PurgeServer'); $this->writelnred('=========================================================================================================='); $now=new \DateTime('now'); $now->sub(new \DateInterval('P3M')); $servers=$this->em->getRepository("CadolesCoreBundle:Server")->findAll(); foreach($servers as $server) { if($server->getUpdatedate()<$now) { $this->writeln($server->getId()." = ".$server->getUpdatedate()->format("Ymd")); $this->em->remove($server); $this->em->flush(); } } return 1; } 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"); } }