src/EventSubscriber/BrandSubscriber.php line 25

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 BrandSubscriber 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.         $brand $event->getData();
  22.         $method 'POST';
  23.         if ($brand->getId()) {
  24.             $method 'PUT';
  25.         }
  26.         $body = [
  27.             "name" => $brand->getName(),
  28.             "code" => $brand->getCode(),
  29.             "plateDefault" => $brand->getPlateDefault(),
  30.             "platePerColor" => $brand->getPlatePerColor(),
  31.             "productionTime" => $brand->getProductionTime(),
  32.             "setupDefault" => $brand->getSetupDefault(),
  33.             "url" => $brand->getUrl(),
  34.             "freight" => $brand->getFreight()
  35.         ];
  36.         if($method === 'PUT') {
  37.             $body["code"] = $brand->getCode();
  38.         }
  39.         $response $this->client->request(
  40.             $method,
  41.             ApiUtils::getSudek() . '/api/brands'. ($method === 'PUT' '/' $brand->getCode() : ''),
  42.             [
  43.                 'headers' => ['accept' => 'application/ld+json''Content-Type' => 'application/ld+json''Authorization' => sprintf('Bearer %s'ApiUtils::getToken($this->client))],
  44.                 'body' => json_encode($body)
  45.             ]
  46.         );
  47.     }
  48. }