src/V4/Service/Tab/TabFormHandler.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\V4\Service\Tab;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  6. use App\ApiFormFactory\ApiFormFactory;
  7. use App\Model\Form\FormSchema;
  8. use App\Security\SecurityConfig;
  9. use App\Service\Form\FormUtils;
  10. use App\V4\DataPersister\Tab\TabDataPersister;
  11. use App\V4\DataProvider\Tab\TabCollectionDataProvider;
  12. use App\V4\Form\Type\Tab\TabType;
  13. use App\V4\Model\Tab\Tab;
  14. use Doctrine\Common\Annotations\AnnotationException;
  15. use Psr\Cache\InvalidArgumentException;
  16. use Symfony\Component\Form\FormError;
  17. use Symfony\Component\HttpFoundation\JsonResponse;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  20. use Symfony\Component\String\Slugger\AsciiSlugger;
  21. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  22. class TabFormHandler
  23. {
  24.     use FormUtils;
  25.     /**
  26.      * @var ApiFormFactory
  27.      */
  28.     private $formFactory;
  29.     /**
  30.      * @var ItemDataProviderInterface
  31.      */
  32.     private $itemDataProvider;
  33.     /**
  34.      * @var CollectionDataProviderInterface
  35.      */
  36.     private $collectionDataProvider;
  37.     /**
  38.      * @var TabDataPersister
  39.      */
  40.     private $tabDataPersister;
  41.     /**
  42.      * @param ApiFormFactory                  $formFactory
  43.      * @param ItemDataProviderInterface       $itemDataProvider
  44.      * @param CollectionDataProviderInterface $collectionDataProvider
  45.      * @param TabDataPersister                $tabDataPersister
  46.      */
  47.     public function __construct(
  48.         ApiFormFactory $formFactory,
  49.         ItemDataProviderInterface $itemDataProvider,
  50.         CollectionDataProviderInterface $collectionDataProvider,
  51.         TabDataPersister $tabDataPersister
  52.     ) {
  53.         $this->formFactory $formFactory;
  54.         $this->itemDataProvider $itemDataProvider;
  55.         $this->collectionDataProvider $collectionDataProvider;
  56.         $this->tabDataPersister $tabDataPersister;
  57.     }
  58.     /**
  59.      * @param string|null $tabId
  60.      * @param string|null $customerId
  61.      *
  62.      * @return FormSchema|JsonResponse
  63.      *
  64.      * @throws ResourceClassNotSupportedException
  65.      */
  66.     public function getTabForm(string $tabId nullstring $customerId null)
  67.     {
  68.         $response = new FormSchema();
  69.         $tab $tabId
  70.             $this->itemDataProvider->getItem(Tab::class, $tabIdnull, [TabCollectionDataProvider::IS_FROM_ADMIN_CONTEXT => true])
  71.             : (new Tab())->setCustomerId($customerId)
  72.         ;
  73.         $infos $this->formFactory->createAsJson(TabType::class, $tab);
  74.         $response->setSchema($infos);
  75.         return $response;
  76.     }
  77.     /**
  78.      * @param array       $data
  79.      * @param string|null $id
  80.      *
  81.      * @return array
  82.      *
  83.      * @throws AnnotationException
  84.      * @throws ExceptionInterface
  85.      * @throws InvalidArgumentException
  86.      * @throws ResourceClassNotSupportedException
  87.      * @throws TransportExceptionInterface
  88.      */
  89.     public function handleForm(array $data, ?string $id null): array
  90.     {
  91.         $tab null !== $id
  92.             $this->itemDataProvider->getItem(Tab::class, $idnull, [TabCollectionDataProvider::IS_FROM_ADMIN_CONTEXT => true])
  93.             : (new Tab())->setCustomerId($data['customerId'] ?? null)
  94.         ;
  95.         $form $this->formFactory->create(TabType::class, $tab);
  96.         $form->submit($data);
  97.         if ($form->isValid()) {
  98.             /** @var Tab[] $tabsSaved */
  99.             $tabsSaved $this->collectionDataProvider->getCollection(Tab::class, null, [TabCollectionDataProvider::IS_FROM_ADMIN_CONTEXT => true]);
  100.             /** @var Tab $tab */
  101.             $tab $form->getData();
  102.             $slugger = new AsciiSlugger();
  103.             $tab->setSlug($slugger->slug($tab->getSlug()));
  104.             $info explode('|'$tab->getSavedUserSearchInfo());
  105.             $tab->setSearchType($info[0]);
  106.             $tab->setSearchContent($info[1]);
  107.             foreach ($tabsSaved as $tabSaved) {
  108.                 if ($tabSaved->getId() !== $tab->getId() && $tab->getSlug() === $tabSaved->getSlug()) {
  109.                     $form->get('slug')->addError(new FormError('field_must_be_unique'));
  110.                     return ['code' => Response::HTTP_INTERNAL_SERVER_ERROR'data' => $this->convertFormErrorsToArray($form)];
  111.                 }
  112.             }
  113.             if ($tab->isRoleNeeded() && null === $tab->getRole()) {
  114.                 $tab->setRole(sprintf(SecurityConfig::TAB$tab->getCustomerId(), $tab->getSlug()));
  115.             } elseif (!$tab->isRoleNeeded() && null !== $tab->getRole()) {
  116.                 $tab->setRole(null);
  117.             }
  118.             $this->tabDataPersister->persist($tab);
  119.             return ['code' => Response::HTTP_OK'data' => $tab];
  120.         }
  121.         return ['code' => Response::HTTP_INTERNAL_SERVER_ERROR'data' => $this->convertFormErrorsToArray($form)];
  122.     }
  123.     /**
  124.      * @param $data
  125.      *
  126.      * @return void
  127.      */
  128.     public function remove($data)
  129.     {
  130.         $this->tabDataPersister->remove($data);
  131.     }
  132. }