src/Listener/Command/CommandListener.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Listener\Command;
  3. use App\Doctrine\DBAL\TenantConnection;
  4. use App\Entity\Main\Tenant;
  5. use App\Repository\Main\TenantRepository;
  6. use Exception;
  7. use Symfony\Component\Console\Command\Command;
  8. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  9. use Symfony\Component\Console\Input\InputArgument;
  10. use Symfony\Component\Console\Input\InputOption;
  11. /**
  12.  * Class CommandListener
  13.  * @package App\Listener\Command
  14.  */
  15. class CommandListener
  16. {
  17.     private array $allowedCommands;
  18.     private TenantConnection $tenantConnection;
  19.     private TenantRepository $tenantRepository;
  20.     /**
  21.      * CommandListener constructor.
  22.      *
  23.      * @param TenantConnection $tenantConnection
  24.      * @param TenantRepository $tenantRepository
  25.      * @param array $allowedCommands
  26.      */
  27.     public function __construct(
  28.         TenantConnection $tenantConnection,
  29.         TenantRepository $tenantRepository,
  30.         $allowedCommands = []
  31.     ) {
  32.         $this->tenantConnection $tenantConnection;
  33.         $this->tenantRepository $tenantRepository;
  34.         $this->allowedCommands $allowedCommands;
  35.     }
  36.     /**
  37.      * @param ConsoleCommandEvent $event
  38.      * @throws Exception
  39.      */
  40.     public function onConsoleCommand(ConsoleCommandEvent $event)
  41.     {
  42.         $command $event->getCommand();
  43.         $input $event->getInput();
  44.         if (!$this->isProperCommand($command)) {
  45.             return;
  46.         }
  47.         $command->addArgument('tenant'InputArgument::OPTIONAL'Tenant ID');
  48.         if (!$command->getDefinition()->hasOption('em')) {
  49.             $command->getDefinition()->addOption(
  50.                 new InputOption('em'nullInputOption::VALUE_OPTIONAL'The entity manager to use for this command')
  51.             );
  52.         }
  53.         $input->bind($command->getDefinition());
  54.         if (is_null($input->getArgument('tenant')) || strpos($input->getArgument('tenant'), 'tenant=') === false) {
  55.             return;
  56.         }
  57.         $tenantID = (int) str_replace('tenant='''$input->getArgument('tenant'));
  58.         /** @var Tenant $tenant */
  59.         $tenant $this->tenantRepository->find($tenantID);
  60.         if (!$tenant instanceof Tenant) {
  61.             throw new Exception(sprintf('Tenant id° %s does not exists'$tenantID));
  62.         }
  63.         $input->setOption('em''tenant');
  64.         $command->getDefinition()->getOption('em')->setDefault('tenant');
  65.         $this->tenantConnection->changeParams($tenant->getDbName(), $tenant->getDbUser(), $tenant->getDbPassword());
  66.     }
  67.     /**
  68.      * @param Command $command
  69.      * @return bool
  70.      */
  71.     private function isProperCommand(
  72.         Command $command
  73.     ): bool {
  74.         return in_array($command->getName(), $this->allowedCommands);
  75.     }
  76. }