src/EventSubscriber/TaxonSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Utils\ApiUtils;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\Form\FormEvent;
  6. use Symfony\Component\Form\FormEvents;
  7. use Symfony\Contracts\HttpClient\HttpClientInterface;
  8. final class TaxonSubscriber implements EventSubscriberInterface
  9. {
  10.     public function __construct(private HttpClientInterface $client)
  11.     {
  12.     }
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             FormEvents::POST_SUBMIT=> 'pushBrandToCentral',
  17.         ];
  18.     }
  19.     public function pushBrandToCentral(FormEvent $event): void
  20.     {
  21.         $taxon $event->getData();
  22.         $method 'POST';
  23.         if ($taxon->getId()) {
  24.             $method 'PUT';
  25.         }
  26.         $body = [
  27.             "code" => $taxon->getCode(),
  28.             "enabled" => $taxon->isEnabled(),
  29.             "parent" => $taxon->getParent()->getCode() ? '/api/taxons/' $taxon->getParent()->getCode() : null,
  30.             "position" => $taxon->getPosition() ?? 0,
  31.             "translations" => [
  32.                 "en_US" => [
  33.                     "name" => $taxon->getTranslation('en_US')->getName(),
  34.                     "slug" => $taxon->getTranslation('en_US')->getSlug(),
  35.                     "description" => $taxon->getTranslation('en_US')->getDescription() ?? '',
  36.                     "locale" => "en_US"
  37.                 ],
  38.             ],
  39.         ];
  40.         $response $this->client->request(
  41.             $method,
  42.             ApiUtils::getSudek() . '/api/taxons' . ($method === 'PUT' '/' $taxon->getCode() : ''),
  43.             [
  44.                 'headers' => ['accept' => 'application/ld+json''Content-type' => 'application/ld+json''Authorization' => sprintf('Bearer %s'ApiUtils::getToken($this->client))],
  45.                 'body' => json_encode($body)
  46.             ]
  47.         );
  48.     }
  49. }