<?php
namespace App\EventSubscriber;
use App\Message\NewInscription;
use Sulu\Bundle\CommunityBundle\Event\AbstractCommunityEvent;
use Sulu\Bundle\CommunityBundle\Event\UserRegisteredEvent;
use Sulu\Bundle\FormBundle\Entity\Dynamic;
use Sulu\Bundle\FormBundle\Event\FormSavePostEvent;
use Sulu\Component\DocumentManager\Subscriber\EventSubscriberInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Twig\Environment;
class InscriptionEventSubscriber implements EventSubscriberInterface
{
/**
* @var MessageBusInterface $bus
*/
private MessageBusInterface $bus;
/**
* __construct
*
* @param MessageBusInterface $dm
* @return void
*/
/**
* @var HttpClientInterface $client
*/
/**
* @var Environment
*/
public function __construct(MessageBusInterface $bus)
{
$this->bus = $bus;
}
public static function getSubscribedEvents(): array
{
return [
FormSavePostEvent::NAME => "noPayboxInscription"
];
}
public function noPayboxInscription(FormSavePostEvent $event)
{
$dynamic = $event->getData();
if (!$dynamic instanceof Dynamic) {
return;
}
$form = $dynamic->getForm()->serializeForLocale($dynamic->getLocale(), $dynamic);
if ($form) {
$ref = isset($dynamic->getData()['text']) ? $dynamic->getData()['text'] : '';
$email = isset($dynamic->getData()['email']) ? $dynamic->getData()['email'] : '';
$lastName = isset($dynamic->getData()['lastName']) ? $dynamic->getData()['lastName'] : '';
$firstName = isset($dynamic->getData()['firstName']) ? $dynamic->getData()['firstName'] : '';
$textarea = isset($dynamic->getData()['textarea']) ? $dynamic->getData()['textarea'] : '';
$structure = isset($dynamic->getData()['text2']) ? $dynamic->getData()['text2'] : '';
$fonction = isset($dynamic->getData()['dropdown']) ? $dynamic->getData()['dropdown'] : '';
$departement = isset($dynamic->getData()['dropdown1']) ? $dynamic->getData()['dropdown1'] : '';
if (empty($ref) || empty($email) || empty($lastName) || empty($firstName) || empty($structure) || empty($fonction) || empty($departement)) {
return;
}
if (array_key_exists('text2', $dynamic->getData())) {
$structure = $dynamic->getData()['text2'];
}
if (array_key_exists('dropdown', $dynamic->getData())) {
$fonction = $dynamic->getData()['dropdown'];
}
if (array_key_exists('dropdown1', $dynamic->getData())) {
$departement = $dynamic->getData()['dropdown1'];
}
if (!$ref || !$email || !$lastName || !$firstName || !$structure || !$fonction || !$departement) {
return;
}
$uuid = explode('_', $ref)[0];
$user_id = intval(explode("_", $ref)[1]);
$array = [
'user_id' => $user_id,
'locale' => $dynamic->getLocale(),
'uuid' => $uuid,
'ref' => $ref,
'email' => $email,
'lastname' => $lastName,
'firstname' => $firstName,
'textarea' => $textarea,
'erreur' => '',
'structure' => $structure,
'fonction' => $fonction,
'departement' => $departement
];
$content = json_encode($array);
$this->bus->dispatch(new NewInscription($content));
sleep(3);
exec('rm -rf /var/www/html/var/cache/common/*');
}
return;
}
}