vendor/uvdesk/core-framework/EventListener/Console/Console.php line 39

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\EventListener\Console;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Mailbox;
  5. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  6. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Bundle\FrameworkBundle\Command as SymfonyFrameworkCommand;
  9. class Console
  10. {
  11.     private $container;
  12.     private $entityManager;
  13.     public function __construct(ContainerInterface $containerEntityManagerInterface $entityManager)
  14.     {
  15.         $this->container $container;
  16.         $this->entityManager $entityManager;
  17.     }
  18.     public function onConsoleCommand(ConsoleCommandEvent $event)
  19.     {
  20.         $output $event->getOutput();
  21.         $command $event->getCommand();
  22.         switch (true) {
  23.             case $command instanceof SymfonyFrameworkCommand\CacheClearCommand:
  24.                 $this->preCacheClearEvent($event);
  25.                 break;
  26.             default:
  27.                 break;
  28.         }
  29.         return;
  30.     }
  31.     public function onConsoleTerminate(ConsoleTerminateEvent $event)
  32.     {
  33.         return;
  34.     }
  35.     private function isDatabaseConfigurationValid()
  36.     {
  37.         $databaseConnection $this->entityManager->getConnection();
  38.         if (false === $databaseConnection->isConnected()) {
  39.             try {    
  40.                 $databaseConnection->connect();
  41.             } catch (\Exception $e) {
  42.                 return false;
  43.             }
  44.         }
  45.         return true;
  46.     }
  47.     private function preCacheClearEvent(ConsoleCommandEvent $event)
  48.     {
  49.         // Proceed only if database url is correctly configured.
  50.         if (false === $this->isDatabaseConfigurationValid()) {
  51.             return;
  52.         }
  53.         $output $event->getOutput();
  54.         // $mailboxes = $this->container->getParameter('uvdesk.mailboxes');
  55.         // $mailboxRepository = $this->entityManager->getRepository('UVDeskCoreFrameworkBundle:Mailbox');
  56.         // // Check for any duplicate mailboxes for an email
  57.         // foreach (array_count_values(array_column($mailboxes, 'email')) as $email => $occurrences) {
  58.         //     if ($occurrences > 1) {
  59.         //         $output->writeln([
  60.         //             "\n <fg=red;>[MIS-CONFIG]</> <comment>Multiple mailboxes have been configured for email </comment><info>$email</info><comment>.</comment>",
  61.         //             "\n Please verify your configuration settings under <info>uvdesk.mailboxes</info>.\n",
  62.         //         ]);
  63.         //         $event->disableCommand();
  64.         //         return;
  65.         //     }
  66.         // }
  67.         
  68.         // // Validate mailbox configs of localized entities
  69.         // foreach ($mailboxRepository->findByIsLocalized(true) as $localizedMailbox) {
  70.         //     $config = null;
  71.         //     foreach ($mailboxes as $mailboxConfig) {
  72.         //         if ($localizedMailbox->getEmail() == $mailboxConfig['email']) {
  73.         //             $config = $mailboxConfig;
  74.         //             break;
  75.         //         }
  76.         //     }
  77.         //     if (empty($config)) {
  78.         //         $mailboxName = ucwords($localizedMailbox->getName());
  79.         //         $mailboxEmail = $localizedMailbox->getEmail();
  80.         //         $output->writeln([
  81.         //             "\n <fg=red;>[MIS-CONFIG]</> <info>$mailboxName</info><comment> has been setup as a localized mailbox but no configurations were found for email </comment><info>$mailboxEmail</info><comment>.</comment>",
  82.         //             "\n Please verify your configuration settings under <info>uvdesk.mailboxes</info>.\n",
  83.         //         ]);
  84.         //         $event->disableCommand();
  85.         //         return;
  86.         //     }
  87.         // }
  88.         // // Syncronize mailbox configs with database
  89.         // foreach ($mailboxes as $mailboxName => $mailboxConfig) {
  90.         //     $mailbox = $mailboxRepository->findOneByEmail($mailboxConfig['email']) ?: new Mailbox();
  91.         //     $mailbox->setName($mailboxName);
  92.         //     $mailbox->setEmail($mailboxConfig['email']);
  93.         //     $mailbox->setIsEnabled($mailboxConfig['enabled']);
  94.         //     $mailbox->setIsLocalized(true);
  95.         //     $this->entityManager->persist($mailbox);
  96.         // }
  97.         // $this->entityManager->flush();
  98.     }
  99. }