src/App/Form/EventListener/Central/Client/ClientCategoryListener.php line 35

Open in your IDE?
  1. <?php
  2. namespace App\Form\EventListener\Central\Client;
  3. use App\Entity\Central\Client\Client;
  4. use App\Model\ShellExec;
  5. use App\Repository\Client\Store\StoreStockRepository;
  6. use App\Service\AppManager;
  7. use App\Service\ShellExecManager;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. class ClientCategoryListener implements EventSubscriberInterface
  12. {
  13.     private $clientCategory;
  14.     private $appManager;
  15.     private $shellExecManager;
  16.     public function __construct(AppManager $appManagerShellExecManager $shellExecManager)
  17.     {
  18.         $this->appManager $appManager;
  19.         $this->shellExecManager $shellExecManager;
  20.     }
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             FormEvents::PRE_SET_DATA => 'onPreSetData',
  25.             FormEvents::POST_SUBMIT   => 'onPostSubmit',
  26.         ];
  27.     }
  28.     public function onPreSetData(FormEvent $formEvent)
  29.     {
  30.         $client $formEvent->getData();
  31.         if (!$client instanceof Client) {
  32.             return;
  33.         }
  34.         $this->clientCategory $client->hasCategory();
  35.     }
  36.     public function onPostSubmit(FormEvent $formEvent)
  37.     {
  38.         $client $formEvent->getData();
  39.         if (!$client instanceof Client) {
  40.             return;
  41.         }
  42.         if ($this->clientCategory !== $client->hasCategory() && $formEvent->getForm()->getErrors(true)->count() === 0) {
  43.             if ($client->hasCategory()){
  44.                 $shellExec = new ShellExec('client:category''--client=' $client->getId() . ' --no-debug');
  45.                 $this->shellExecManager->runShellExec($shellExec);
  46.             } else {
  47.                 StoreStockRepository::updateStoreStocks($this->appManager->getConnection(), $client->getId());
  48.             }
  49.         }
  50.     }
  51. }