LicenceCmfExporter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\Enum\Core\FileTypeEnum;
  7. use App\Service\Export\Model\LicenceCmf;
  8. use App\Enum\Access\FunctionEnum;
  9. use App\Repository\Organization\OrganizationRepository;
  10. use App\Service\Export\Model\LicenceCmfCollection;
  11. /**
  12. * Exporte la licence CMF de la structure ou du ou des access, au format demandé
  13. */
  14. class LicenceCmfExporter extends BaseExporter implements ExporterInterface
  15. {
  16. public const CMF_ID = 12097;
  17. /**
  18. * La couleur de la carte de licence change chaque année, de manière cyclique
  19. */
  20. public const LICENCE_CMF_COLOR_START_YEAR = 2020;
  21. public const LICENCE_CMF_COLOR = [0 => '931572', 1 => 'C2981A', 2 => '003882', 3 => '27AAE1', 4 => '2BB673'];
  22. public function __construct(
  23. private OrganizationRepository $organizationRepository
  24. )
  25. {}
  26. public function support($exportRequest): bool
  27. {
  28. return $exportRequest instanceof LicenceCmfOrganizationER;
  29. }
  30. /**
  31. * @param LicenceCmfOrganizationER $exportRequest
  32. * @return LicenceCmfCollection
  33. */
  34. public function buildModel(ExportRequest $exportRequest): LicenceCmfCollection
  35. {
  36. $organization = $this->accessRepository->find($exportRequest->getRequesterId())?->getOrganization();
  37. if ($organization === null) {
  38. throw new \RuntimeException('Unable to determine the organization of the curent user; abort.');
  39. }
  40. $licenceCmf = new LicenceCmf();
  41. $licenceCmf->setId($organization->getId());
  42. $licenceCmf->setYear($exportRequest->getYear());
  43. $licenceCmf->setIsOrganizationLicence( $exportRequest instanceof LicenceCmfOrganizationER);
  44. $licenceCmf->setOrganizationName($organization->getName());
  45. $licenceCmf->setOrganizationIdentifier($organization->getIdentifier());
  46. $parentFederation = $organization->getNetworkOrganizations()->get(0)?->getParent();
  47. if ($parentFederation !== null) {
  48. $licenceCmf->setFederationName($parentFederation->getName());
  49. }
  50. $licenceCmf->setColor(
  51. $this->getLicenceColor($exportRequest->getYear())
  52. );
  53. $logo = $organization->getLogo();
  54. if ($logo) {
  55. $licenceCmf->setLogo($logo);
  56. }
  57. $presidents = $this->accessRepository->findByOrganizationAndMission($organization, FunctionEnum::PRESIDENT);
  58. if (count($presidents) > 0) {
  59. $president = $presidents[0]->getPerson();
  60. if ($president !== null) {
  61. $licenceCmf->setPersonId($president->getId());
  62. $licenceCmf->setPersonGender($president->getGender() ?? '');
  63. $licenceCmf->setPersonFirstName($president->getGivenName());
  64. $licenceCmf->setPersonLastName($president->getName());
  65. }
  66. }
  67. $cmf = $this->organizationRepository->find(self::CMF_ID);
  68. /** @noinspection NullPointerExceptionInspection */
  69. $qrCode = $cmf->getParameters()->getQrCode();
  70. if ($qrCode) {
  71. $licenceCmf->setQrCode($qrCode);
  72. }
  73. $model = new LicenceCmfCollection();
  74. $model->setLicences([$licenceCmf]);
  75. return $model;
  76. }
  77. /**
  78. * @param LicenceCmfOrganizationER $exportRequest
  79. * @return string
  80. */
  81. protected function getFileBasename(ExportRequest $exportRequest): string
  82. {
  83. return 'licence_cmf_' . $exportRequest->getYear() . '.pdf';
  84. }
  85. /**
  86. * Retourne le type de fichier tel qu'il apparait au niveau du champ File.type
  87. *
  88. * @return FileTypeEnum
  89. */
  90. protected function getFileType(): FileTypeEnum
  91. {
  92. return FileTypeEnum::LICENCE_CMF;
  93. }
  94. /**
  95. * Retourne la couleur de licence pour l'année donnée
  96. *
  97. * @param int $year
  98. * @return string
  99. */
  100. protected function getLicenceColor(int $year): string
  101. {
  102. if (!($year > self::LICENCE_CMF_COLOR_START_YEAR)) {
  103. return self::LICENCE_CMF_COLOR[0];
  104. }
  105. return self::LICENCE_CMF_COLOR[($year - self::LICENCE_CMF_COLOR_START_YEAR) % count(self::LICENCE_CMF_COLOR)];
  106. }
  107. }