src/Twig/GedImageExtension.php line 222

Open in your IDE?
  1. <?php
  2. namespace App\Twig;
  3. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  4. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  5. use App\Model\CustomerFile\CustomerFile;
  6. use App\Service\ApiWebService;
  7. use App\Service\CustomerFile\CustomerFileDownloadManager;
  8. use App\Service\Provider\ApiGedProvider;
  9. use App\V4\Logger\SentryLogger;
  10. use Psr\Log\LogLevel;
  11. use ReflectionException;
  12. use Symfony\Component\HttpClient\Exception\ClientException;
  13. use Symfony\Component\HttpClient\Exception\ServerException;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  18. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  19. use Throwable;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFunction;
  22. class GedImageExtension extends AbstractExtension
  23. {
  24.     private const MIME_TYPES_SUPPORTED = ['image/gif''image/png''image/jpeg''image/bmp''image/webp'];
  25.     /**
  26.      * @var ItemDataProviderInterface
  27.      */
  28.     private $itemDataProvider;
  29.     /**
  30.      * @var CustomerFileDownloadManager
  31.      */
  32.     private $customerFileDownloadManager;
  33.     /**
  34.      * @var ApiGedProvider
  35.      */
  36.     private $apiGedProvider;
  37.     /**
  38.      * @var ApiWebService
  39.      */
  40.     private $apiWebService;
  41.     /**
  42.      * @var SentryLogger
  43.      */
  44.     private $sentryLogger;
  45.     /**
  46.      * @param ItemDataProviderInterface   $itemDataProvider
  47.      * @param CustomerFileDownloadManager $customerFileDownloadManager
  48.      * @param ApiGedProvider              $apiGedProvider
  49.      * @param ApiWebService               $apiWebService
  50.      * @param SentryLogger                $sentryLogger
  51.      */
  52.     public function __construct(
  53.         ItemDataProviderInterface $itemDataProvider,
  54.         CustomerFileDownloadManager $customerFileDownloadManager,
  55.         ApiGedProvider $apiGedProvider,
  56.         ApiWebService $apiWebService,
  57.         SentryLogger $sentryLogger
  58.     ) {
  59.         $this->itemDataProvider $itemDataProvider;
  60.         $this->customerFileDownloadManager $customerFileDownloadManager;
  61.         $this->apiGedProvider $apiGedProvider;
  62.         $this->apiWebService $apiWebService;
  63.         $this->sentryLogger $sentryLogger;
  64.     }
  65.     /**
  66.      * @return TwigFunction[]
  67.      */
  68.     public function getFunctions(): array
  69.     {
  70.         return [
  71.             new TwigFunction('gedImage', [$this'getGedImage']),
  72.             new TwigFunction('gedImageByPath', [$this'getGedImageByPath']),
  73.         ];
  74.     }
  75.     /**
  76.      * @param string $id
  77.      *
  78.      * @return string
  79.      *
  80.      * @throws ResourceClassNotSupportedException
  81.      * @throws TransportExceptionInterface
  82.      * @throws \Exception
  83.      */
  84.     public function getGedImage(string $id): ?string
  85.     {
  86.         /** @var CustomerFile $customerFile */
  87.         $customerFile $this->itemDataProvider->getItem(CustomerFile::class, $id);
  88.         if (!$customerFile instanceof CustomerFile) {
  89.             $this->sentryLogger->captureMessage(
  90.                 SentryLogger::CHANNEL_DATA_RETRIEVER,
  91.                 sprintf('CustomerFile with id %s cannot be found'$customerFile->getId()),
  92.                 [
  93.                     'catchOnClass' => self::class,
  94.                     'imageId' => $customerFile->getId(),
  95.                 ],
  96.                 LogLevel::WARNING
  97.             );
  98.             return null;
  99.         }
  100.         if (!in_array($customerFile->getMimeType(), self::MIME_TYPES_SUPPORTEDtrue)) {
  101.             $this->sentryLogger->captureMessage(
  102.                 SentryLogger::CHANNEL_DATA_RETRIEVER,
  103.                 sprintf('CustomerFile with id %s has the wrong mimeTypes : %s'$customerFile->getId(), $customerFile->getMimeType()),
  104.                 [
  105.                     'catchOnClass' => self::class,
  106.                     'imageId' => $customerFile->getId(),
  107.                 ],
  108.                 LogLevel::WARNING
  109.             );
  110.             return null;
  111.         }
  112.         try {
  113.             return $this->getCustomerFileContentForTwig($customerFile);
  114.         } catch (Throwable $exception) {
  115.             $this->sentryLogger->captureException(
  116.                 SentryLogger::CHANNEL_DATA_RETRIEVER,
  117.                 $exception,
  118.                 [
  119.                     'catchOnClass' => self::class,
  120.                     'imageId' => $customerFile->getId(),
  121.                 ]
  122.             );
  123.         }
  124.         return null;
  125.     }
  126.     /**
  127.      * @param string $path
  128.      *
  129.      * @return string
  130.      *
  131.      * @throws ClientExceptionInterface
  132.      * @throws RedirectionExceptionInterface
  133.      * @throws ServerExceptionInterface
  134.      * @throws TransportExceptionInterface
  135.      * @throws ReflectionException
  136.      * @throws \Exception
  137.      */
  138.     public function getGedImageByPath(string $path): ?string
  139.     {
  140.         try {
  141.             $response $this
  142.                 ->apiWebService
  143.                 ->addFilterCustomer()
  144.                 ->addFilter('path'$path)
  145.                 ->send($this->apiGedProviderRequest::METHOD_GET'/api/customer_resource/get_by_path')
  146.             ;
  147.             $result json_decode($response->getContent(), true);
  148.         } catch (ClientException ServerException $e) {
  149.             $this->sentryLogger->captureException(
  150.                 SentryLogger::CHANNEL_TWIG_EXTENSION,
  151.                 $e,
  152.                 [
  153.                     'catchOnClass' => self::class,
  154.                     'apiCalled' => $this->apiGedProvider->getHost(),
  155.                     'urlCalled' => '/api/customer_resource/get_by_path',
  156.                     'method' => Request::METHOD_GET,
  157.                     'params' => [
  158.                         'path' => $path,
  159.                     ],
  160.                 ]
  161.             );
  162.             return null;
  163.         }
  164.         if (null === $result) {
  165.             $this->sentryLogger->captureMessage(
  166.                 SentryLogger::CHANNEL_TWIG_EXTENSION,
  167.                 'JsonResponse cannot be decode',
  168.                 [
  169.                     'catchOnClass' => self::class,
  170.                     'jsonResponse' => $response->getContent(),
  171.                 ],
  172.                 LogLevel::CRITICAL
  173.             );
  174.             return null;
  175.         }
  176.         $customerFile = (new CustomerFile())->importFromData($result);
  177.         if (!in_array($customerFile->getMimeType(), self::MIME_TYPES_SUPPORTEDtrue)) {
  178.             $this->sentryLogger->captureMessage(
  179.                 SentryLogger::CHANNEL_TWIG_EXTENSION,
  180.                 sprintf('CustomerFile with id %s has the wrong mimeTypes : %s'$customerFile->getId(), $customerFile->getMimeType()),
  181.                 [
  182.                     'catchOnClass' => self::class,
  183.                 ],
  184.                 LogLevel::WARNING
  185.             );
  186.             return null;
  187.         }
  188.         return $this->getCustomerFileContentForTwig($customerFile);
  189.     }
  190.     /**
  191.      * @param CustomerFile $customerFile
  192.      *
  193.      * @return string
  194.      *
  195.      * @throws TransportExceptionInterface
  196.      */
  197.     private function getCustomerFileContentForTwig(CustomerFile $customerFile): string
  198.     {
  199.         $tpmPath $this->customerFileDownloadManager->getFile($customerFile);
  200.         return sprintf('data:%s;base64, %s'$customerFile->getMimeType(), base64_encode(file_get_contents($tpmPath)));
  201.     }
  202. }