SiretProvider.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\State\Provider\Utils;
  4. use ApiPlatform\Metadata\GetCollection;
  5. use ApiPlatform\Metadata\Operation;
  6. use ApiPlatform\State\ProviderInterface;
  7. use App\ApiResources\Utils\Siret;
  8. use App\Enum\Utils\HttpCodeEnum;
  9. use App\Service\Utils\Siret as SiretUtils;
  10. use RuntimeException;
  11. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  12. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  15. /**
  16. * Class SiretProvider : custom provider pour assurer l'alimentation des utils Siret
  17. * @package App\DataProvider\Utils
  18. */
  19. final class SiretProvider implements ProviderInterface
  20. {
  21. public function __construct(
  22. private SiretUtils $siretUtils
  23. )
  24. { }
  25. /**
  26. * @param Operation $operation
  27. * @param mixed[] $uriVariables
  28. * @param mixed[] $context
  29. * @return Siret|null
  30. * @throws ClientExceptionInterface
  31. * @throws RedirectionExceptionInterface
  32. * @throws ServerExceptionInterface
  33. * @throws TransportExceptionInterface
  34. */
  35. public function provide(Operation $operation, array $uriVariables = [], array $context = []): ?Siret
  36. {
  37. if($operation instanceof GetCollection) {
  38. throw new RuntimeException('not supported', HttpCodeEnum::METHOD_NOT_ALLOWED()->getValue());
  39. }
  40. $id = $uriVariables['id'];
  41. $siretResponse = new Siret();
  42. $siretResponse->setNumber($id);
  43. $siretResponse->setIsCorrect($this->siretUtils->isSiretCorrect($id));
  44. return $siretResponse;
  45. }
  46. }