<?php
namespace App\V4\Service\Tab;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\ApiFormFactory\ApiFormFactory;
use App\Model\Form\FormSchema;
use App\Security\SecurityConfig;
use App\Service\Form\FormUtils;
use App\V4\DataPersister\Tab\TabDataPersister;
use App\V4\DataProvider\Tab\TabCollectionDataProvider;
use App\V4\Form\Type\Tab\TabType;
use App\V4\Model\Tab\Tab;
use Doctrine\Common\Annotations\AnnotationException;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class TabFormHandler
{
use FormUtils;
/**
* @var ApiFormFactory
*/
private $formFactory;
/**
* @var ItemDataProviderInterface
*/
private $itemDataProvider;
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
/**
* @var TabDataPersister
*/
private $tabDataPersister;
/**
* @param ApiFormFactory $formFactory
* @param ItemDataProviderInterface $itemDataProvider
* @param CollectionDataProviderInterface $collectionDataProvider
* @param TabDataPersister $tabDataPersister
*/
public function __construct(
ApiFormFactory $formFactory,
ItemDataProviderInterface $itemDataProvider,
CollectionDataProviderInterface $collectionDataProvider,
TabDataPersister $tabDataPersister
) {
$this->formFactory = $formFactory;
$this->itemDataProvider = $itemDataProvider;
$this->collectionDataProvider = $collectionDataProvider;
$this->tabDataPersister = $tabDataPersister;
}
/**
* @param string|null $tabId
* @param string|null $customerId
*
* @return FormSchema|JsonResponse
*
* @throws ResourceClassNotSupportedException
*/
public function getTabForm(string $tabId = null, string $customerId = null)
{
$response = new FormSchema();
$tab = $tabId
? $this->itemDataProvider->getItem(Tab::class, $tabId, null, [TabCollectionDataProvider::IS_FROM_ADMIN_CONTEXT => true])
: (new Tab())->setCustomerId($customerId)
;
$infos = $this->formFactory->createAsJson(TabType::class, $tab);
$response->setSchema($infos);
return $response;
}
/**
* @param array $data
* @param string|null $id
*
* @return array
*
* @throws AnnotationException
* @throws ExceptionInterface
* @throws InvalidArgumentException
* @throws ResourceClassNotSupportedException
* @throws TransportExceptionInterface
*/
public function handleForm(array $data, ?string $id = null): array
{
$tab = null !== $id
? $this->itemDataProvider->getItem(Tab::class, $id, null, [TabCollectionDataProvider::IS_FROM_ADMIN_CONTEXT => true])
: (new Tab())->setCustomerId($data['customerId'] ?? null)
;
$form = $this->formFactory->create(TabType::class, $tab);
$form->submit($data);
if ($form->isValid()) {
/** @var Tab[] $tabsSaved */
$tabsSaved = $this->collectionDataProvider->getCollection(Tab::class, null, [TabCollectionDataProvider::IS_FROM_ADMIN_CONTEXT => true]);
/** @var Tab $tab */
$tab = $form->getData();
$slugger = new AsciiSlugger();
$tab->setSlug($slugger->slug($tab->getSlug()));
$info = explode('|', $tab->getSavedUserSearchInfo());
$tab->setSearchType($info[0]);
$tab->setSearchContent($info[1]);
foreach ($tabsSaved as $tabSaved) {
if ($tabSaved->getId() !== $tab->getId() && $tab->getSlug() === $tabSaved->getSlug()) {
$form->get('slug')->addError(new FormError('field_must_be_unique'));
return ['code' => Response::HTTP_INTERNAL_SERVER_ERROR, 'data' => $this->convertFormErrorsToArray($form)];
}
}
if ($tab->isRoleNeeded() && null === $tab->getRole()) {
$tab->setRole(sprintf(SecurityConfig::TAB, $tab->getCustomerId(), $tab->getSlug()));
} elseif (!$tab->isRoleNeeded() && null !== $tab->getRole()) {
$tab->setRole(null);
}
$this->tabDataPersister->persist($tab);
return ['code' => Response::HTTP_OK, 'data' => $tab];
}
return ['code' => Response::HTTP_INTERNAL_SERVER_ERROR, 'data' => $this->convertFormErrorsToArray($form)];
}
/**
* @param $data
*
* @return void
*/
public function remove($data)
{
$this->tabDataPersister->remove($data);
}
}