src/Command/GenerateScriptChangeStatusANCE14E15EvalCommand.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Command;
  3. use App\Repository\EvaluationRepository;
  4. use Doctrine\DBAL\Exception;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\Console\Attribute\AsCommand;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Input\InputInterface;
  9. use Symfony\Component\Console\Output\OutputInterface;
  10. use Symfony\Component\Console\Style\SymfonyStyle;
  11. #[AsCommand(
  12.     name'app:generate:script-chnage-status-ance14-e15',
  13.     description'this command will generate script to change status evaluation ance14-e15',
  14. )]
  15. class GenerateScriptChangeStatusANCE14E15EvalCommand extends Command
  16. {
  17.     public function __construct(
  18.         private ?EntityManagerInterface       $em null,
  19.         private readonly EvaluationRepository $evaluationRepository,
  20.     )
  21.     {
  22.         parent::__construct();
  23.     }
  24.     /**
  25.      * @throws Exception
  26.      */
  27.     protected function execute(InputInterface $inputOutputInterface $output): int
  28.     {
  29.         $io = new SymfonyStyle($input$output);
  30.         $io->info('START');
  31.         $ids_sites $this->getSitesPogressionZero();
  32.         $ids_sites array_column($ids_sites'site_id');
  33.         dump'Nb sites: 'count($ids_sites));
  34.         dump'sites: 'implode(','$ids_sites));
  35.         if (count($ids_sites) > 0) {
  36.             $ids_evaluations $this->getEvaluations($ids_sites);
  37.             $ids_evaluations array_column($ids_evaluations'id');
  38.             dump'Nb eval: 'count($ids_evaluations));
  39.             dump'evals: 'implode(','$ids_evaluations));
  40.             $this->updateEvaluations($ids_evaluations);
  41.         }
  42.         $io->success('Done!');
  43.         return Command::SUCCESS;
  44.     }
  45.     /**
  46.      * @throws Exception
  47.      */
  48.     private function getSitesPogressionZero(): array
  49.     {
  50.         $conn $this->em->getConnection();
  51.         $columns array_merge(['e.site_id'], EvaluationRepository::getDefaultStatColumnsReport());
  52.         $sql sprintf("select %s
  53.                                 from evaluation e 
  54.                                    where e.compliance_id in (2373493, 2373492)
  55.                                    group by e.site_id
  56.                                    having progression = 0 "implode(','$columns));
  57.         $stmt $conn->prepare($sql);
  58.         return $stmt->executeQuery()->fetchAllAssociative();
  59.     }
  60.     /**
  61.      * @throws Exception
  62.      */
  63.     private function getEvaluations($ids_sites): array
  64.     {
  65.         $conn $this->em->getConnection();
  66.         $sql sprintf("select id
  67.                     from evaluation 
  68.                     where 
  69.                     compliance_id in ( 2373493, 2373492) and site_id in (%s)"implode(','$ids_sites));
  70.         $stmt $conn->prepare($sql);
  71.         return $stmt->executeQuery()->fetchAllAssociative();
  72.     }
  73.     /**
  74.      * @throws Exception
  75.      */
  76.     private function updateEvaluations($ids_eval): void
  77.     {
  78.         $conn $this->em->getConnection();
  79.         $sql sprintf("update evaluation set status_id = 53, comment = NULL where id in (%s)"implode(','$ids_eval));
  80.         $stmt $conn->prepare($sql);
  81.         $stmt->executeQuery()->fetchAllAssociative();
  82.     }
  83. }