<?php
namespace App\Event;
use App\Event\SuluApiEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Sulu\Bundle\FormBundle\Entity\Dynamic;
use Sulu\Bundle\FormBundle\Event\FormSavePostEvent;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class SuluApiEventSubscriber implements EventSubscriberInterface
{
private $id;
private $pwd;
private $client;
public function __construct(HttpClientInterface $client)
{
$this->id = 'btenreiro';
$this->pwd = 'ben';
$this->client = $client;
}
public static function getSubscribedEvents(): array
{
return [
FormSavePostEvent::NAME => "suluApi"
];
}
public function suluApi(FormSavePostEvent $event): void
{
$dynamic = $event->getData();
if (!$dynamic instanceof Dynamic) {
return;
}
$form = $dynamic->getForm()->serializeForLocale($dynamic->getLocale(), $dynamic);
if ($form && strpos($form['title'], "Hub") === 0) {
$apiCall = new SuluApiEvent($this->id, $this->pwd, $this->client);
$apiCall->sendDraft();
}
}
}