LicenceCmfExporter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Export;
  4. use App\ApiResources\Export\ExportRequest;
  5. use App\ApiResources\Export\LicenceCmf\LicenceCmfOrganizationER;
  6. use App\Service\Export\Encoder\EncoderHandler;
  7. use App\Service\Export\Model\LicenceCmf;
  8. use App\Enum\Access\FunctionEnum;
  9. use App\Repository\Access\AccessRepository;
  10. use App\Repository\Organization\OrganizationRepository;
  11. use App\Service\Storage\TemporaryFileStorage;
  12. use App\Service\Utils\Path;
  13. use Twig\Environment;
  14. /**
  15. * Exporte la licence CMF de la structure ou du ou des access, au format demandé
  16. */
  17. class LicenceCmfExporter implements ExporterInterface
  18. {
  19. const CMF_ID = 12097;
  20. /**
  21. * La couleur de la carte de licence change chaque année, de manière cyclique
  22. */
  23. const LICENCE_CMF_COLOR_START_YEAR = "2020";
  24. const LICENCE_CMF_COLOR = [0 => '931572', 1 => 'C2981A', 2 => '003882', 3 => '27AAE1', 4 => '2BB673'];
  25. public function __construct(
  26. private OrganizationRepository $organizationRepository,
  27. private AccessRepository $accessRepository,
  28. private Environment $twig,
  29. private EncoderHandler $encoderHandler
  30. )
  31. {}
  32. public function support($exportRequest): bool
  33. {
  34. return $exportRequest instanceof LicenceCmfOrganizationER;
  35. }
  36. /**
  37. *
  38. */
  39. public function export($exportRequest)
  40. {
  41. $organization = $exportRequest->getRequester()->getOrganization();
  42. $currentYear = (int)date('Y');
  43. $model = new LicenceCmf();
  44. $model->setId($organization->getId());
  45. $model->setYear($currentYear);
  46. $model->setOrganizationName($organization->getName());
  47. $model->setOrganizationIdentifier($organization->getIdentifier());
  48. $parentFederation = $organization->getNetworkOrganizations()[0]->getParent();
  49. $model->setFederationName($parentFederation->getName());
  50. $model->setColor(
  51. $this->getLicenceColor($currentYear)
  52. );
  53. $logoId = $organization->getLogo()?->getId();
  54. if ($logoId) {
  55. $model->setLogoUri(
  56. rtrim($_SERVER['INTERNAL_FILES_DOWNLOAD_URI'], '/') . '/' . $logoId
  57. );
  58. }
  59. $presidents = $this->accessRepository->findByOrganizationAndMission($organization, FunctionEnum::PRESIDENT()->getValue());
  60. if (count($presidents) > 0) {
  61. $president = $presidents[0]->getPerson();
  62. $model->setPersonId($president->getId());
  63. $model->setPersonGender($president->getGender());
  64. $model->setPersonFirstName($president->getGivenName());
  65. $model->setPersonLastName($president->getName());
  66. }
  67. $cmf = $this->organizationRepository->find(self::CMF_ID);
  68. $qrCodeId = $cmf->getParameters()?->getQrCode()?->getId();
  69. if ($qrCodeId) {
  70. $model->setQrCodeUri(
  71. rtrim($_SERVER['INTERNAL_FILES_DOWNLOAD_URI'], '/') . '/' . $qrCodeId
  72. );
  73. }
  74. $html = $this->twig->render(
  75. '@templates/export/licence_cmf.html.twig',
  76. [
  77. 'models' => [$model],
  78. 'isOrganizationLicence' => true
  79. ]
  80. );
  81. $filename = 'licence_cmf_' . $currentYear . '.pdf';
  82. $tempDir = TemporaryFileStorage::makeStorageDirFor($organization->getId());
  83. $tempPath = Path::join($tempDir, $filename);
  84. if (file_exists($tempPath)) {
  85. unlink($tempPath);
  86. }
  87. $encoder = $this->encoderHandler->getEncoderFor($exportRequest);
  88. $encoder->encodeToFile($html, $tempPath);
  89. }
  90. /**
  91. * Retourne la couleur de licence pour l'année donnée
  92. * @param int $year
  93. * @return string
  94. */
  95. protected function getLicenceColor(int $year): string {
  96. if (!self::LICENCE_CMF_COLOR_START_YEAR > $year) {
  97. return self::LICENCE_CMF_COLOR[0];
  98. }
  99. return self::LICENCE_CMF_COLOR[($year - self::LICENCE_CMF_COLOR_START_YEAR) % count(self::LICENCE_CMF_COLOR)];
  100. }
  101. }