src/EventSubscriber/SchoolYear/SchoolYearSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\SchoolYear;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Command\SchoolYear\SchoolYearClone;
  5. use App\Entity\SchoolYear\SchoolYear;
  6. use Exception;
  7. use JetBrains\PhpStorm\ArrayShape;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\ViewEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\Process\Process;
  15. class SchoolYearSubscriber implements EventSubscriberInterface
  16. {
  17.     public function __construct(
  18.         private KernelInterface $kernel,
  19.         private LoggerInterface $logger,
  20.     )
  21.     {
  22.     }
  23.     #[ArrayShape([KernelEvents::VIEW => "array"])]
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             KernelEvents::VIEW => ['duplicate'EventPriorities::POST_WRITE],
  28.         ];
  29.     }
  30.     /**
  31.      * @throws Exception
  32.      */
  33.     public function duplicate(ViewEvent $event): void
  34.     {
  35.         $schoolYear $event->getControllerResult();
  36.         $method $event->getRequest()->getMethod();
  37.         if (!$schoolYear instanceof SchoolYear || Request::METHOD_POST !== $method) {
  38.             return;
  39.         }
  40.         $pathToBin $this->kernel->getProjectDir() . '/bin/console';
  41.         foreach (SchoolYearClone::ORDERED_PART_TO_CLONE as $part) {
  42.             $cmd 'php ' $pathToBin ' ' SchoolYearClone::DEFAULT_NAME ' ' $schoolYear->getId() . ' ' $schoolYear->getSchoolYearToDuplicate()->getId() . ' ' $part;
  43.             $process Process::fromShellCommandline($cmd);
  44.             $process->setTimeout(10000);
  45.             $process->start();
  46.             $process->wait(
  47.                 function ($type$buffer) use ($process) {
  48.                     if($type === Process::ERR && !str_contains($buffer'[debug]')){
  49.                         $this->logger->error($process->getCommandLine() . " :\n" $buffer);
  50.                     }
  51.                 }
  52.             );
  53.         }
  54.     }
  55. }