src/App/EventListener/CommandListener.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\Central\Client\Client;
  4. use App\Entity\Central\Command\Command;
  5. use App\Entity\Client\PointOfSale\PointOfSale;
  6. use App\Entity\Client\Store\Store;
  7. use App\Service\AppManager;
  8. use App\Service\Logger;
  9. use Symfony\Component\Console\Command\Command as SymfonyCommand;
  10. use Symfony\Component\Console\ConsoleEvents;
  11. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  12. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  13. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpKernel\KernelInterface;
  16. class CommandListener implements EventSubscriberInterface
  17. {
  18.     private $kernel;
  19.     private $appManager;
  20.     private $logger;
  21.     public function __construct(KernelInterface $kernelAppManager $appManagerLogger $logger)
  22.     {
  23.         $this->appManager $appManager;
  24.         $this->kernel $kernel;
  25.         $this->logger $logger;
  26.     }
  27.     public function onConsoleCommand(ConsoleCommandEvent $event)
  28.     {
  29.         if ($event->getCommand() === null) {
  30.             return;
  31.         }
  32.         $symfonyCommand $event->getCommand();
  33.         if ($this->ignoreCommand($symfonyCommand->getName())) {
  34.             return;
  35.         }
  36.         $argv $_SERVER['argv'];
  37.         unset($argv[0]);
  38.         $command = new Command();
  39.         $command->setCommand('php ' $this->kernel->getProjectDir() . '/bin/console ' implode(" "$argv));
  40.         $command->setOptions($event->getInput()->getOptions());
  41.         $command->setArguments($event->getInput()->getArguments());
  42.         $this->appManager->beginTransaction();
  43.         $this->appManager->persist($command);
  44.         $this->appManager->flush();
  45.         $this->appManager->commit();
  46.         $event->getCommand()->setHelp($command->getId());
  47.     }
  48.     public function onConsoleError(ConsoleErrorEvent $event)
  49.     {
  50.         $this->logger->logException($event->getError());
  51.         if ($event->getCommand() === null) {
  52.             return;
  53.         }
  54.         $this->endCommand($event->getCommand(), $event->getError()->getMessage(), Command::COMMAND_ERROR);
  55.     }
  56.     public function onConsoleTerminate(ConsoleTerminateEvent $event)
  57.     {
  58.         if ($event->getCommand() === null) {
  59.             return;
  60.         }
  61.         $this->endCommand($event->getCommand(), $event->getExitCode(), Command::COMMAND_SUCCESS);
  62.     }
  63.     public function endCommand(SymfonyCommand $symfonyCommandstring $exitCodestring $resultCode)
  64.     {
  65.         if ($this->ignoreCommand($symfonyCommand->getName())) {
  66.             return;
  67.         }
  68.         $commandId $symfonyCommand->getHelp();
  69.         if ($commandId === '') {
  70.             return;
  71.         }
  72.         try {
  73.             $finishAt = new \DateTime();
  74.             $command $this->appManager->getRepository(Command::class)->find($commandId);
  75.             if (!$command instanceof Command) {
  76.                 return;
  77.             }
  78.             if ($command->getResultCode() !== Command::COMMAND_RUNNING && $resultCode === Command::COMMAND_SUCCESS){
  79.                 return;
  80.             }
  81.             $diff = ($realDiff $finishAt->getTimestamp() - $command->getCreatedAt()->getTimestamp()) < $realDiff;
  82.             $command->setTotalExecutionTimeInSeconds($diff);
  83.             $command->setFinishAt($finishAt);
  84.             $command->setResultCode($resultCode);
  85.             $command->setResult($exitCode);
  86.             $this->appManager->persist($command);
  87.             $this->appManager->flush();
  88.             $this->appManager->commit();
  89.         } catch (\Exception $exception) {
  90.         }
  91.     }
  92.     public static function getSubscribedEvents()
  93.     {
  94.         return [
  95.             ConsoleEvents::COMMAND => ['onConsoleCommand'0],
  96.             ConsoleEvents::ERROR => ['onConsoleError', -128],
  97.             ConsoleEvents::TERMINATE => ['onConsoleTerminate', -128],
  98.         ];
  99.     }
  100.     public function ignoreCommand(string $commandName)
  101.     {
  102.         if (in_array($commandName, [
  103.             'swiftmailer:spool:send',
  104.             'doctrine:database:create',
  105.             'doctrine:schema:update',
  106.             'doctrine:schema:drop',
  107.             'doctrine:schemas:update',
  108.             'doctrine:database:drop',
  109.             'to:postgres',
  110.             'cache:clear',
  111.             'assets:install'
  112.         ])) {
  113.             return true;
  114.         }
  115.         return false;
  116.     }
  117. }