<?php
namespace App\Form\EventListener\Central\Client;
use App\Entity\Central\Client\Client;
use App\Model\ShellExec;
use App\Repository\Client\Store\StoreStockRepository;
use App\Service\AppManager;
use App\Service\ShellExecManager;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class ClientCategoryListener implements EventSubscriberInterface
{
private $clientCategory;
private $appManager;
private $shellExecManager;
public function __construct(AppManager $appManager, ShellExecManager $shellExecManager)
{
$this->appManager = $appManager;
$this->shellExecManager = $shellExecManager;
}
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'onPreSetData',
FormEvents::POST_SUBMIT => 'onPostSubmit',
];
}
public function onPreSetData(FormEvent $formEvent)
{
$client = $formEvent->getData();
if (!$client instanceof Client) {
return;
}
$this->clientCategory = $client->hasCategory();
}
public function onPostSubmit(FormEvent $formEvent)
{
$client = $formEvent->getData();
if (!$client instanceof Client) {
return;
}
if ($this->clientCategory !== $client->hasCategory() && $formEvent->getForm()->getErrors(true)->count() === 0) {
if ($client->hasCategory()){
$shellExec = new ShellExec('client:category', '--client=' . $client->getId() . ' --no-debug');
$this->shellExecManager->runShellExec($shellExec);
} else {
StoreStockRepository::updateStoreStocks($this->appManager->getConnection(), $client->getId());
}
}
}
}