LicenceCmfExporter.php 4.0 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\Enum\Access\FunctionEnum;
  7. use App\Enum\Core\FileTypeEnum;
  8. use App\Repository\Organization\OrganizationRepository;
  9. use App\Service\Export\Model\LicenceCmf;
  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 readonly OrganizationRepository $organizationRepository
  24. ) {
  25. }
  26. public function support(ExportRequest $exportRequest): bool
  27. {
  28. return $exportRequest instanceof LicenceCmfOrganizationER;
  29. }
  30. /**
  31. * @param LicenceCmfOrganizationER $exportRequest
  32. */
  33. public function buildModel(ExportRequest $exportRequest): LicenceCmfCollection
  34. {
  35. $organization = $this->accessRepository->find($exportRequest->getRequesterId())?->getOrganization();
  36. if ($organization === null) {
  37. throw new \RuntimeException('Unable to determine the organization of the curent user; abort.');
  38. }
  39. $licenceCmf = new LicenceCmf();
  40. $licenceCmf->setId($organization->getId());
  41. $licenceCmf->setYear($exportRequest->getYear());
  42. $licenceCmf->setIsOrganizationLicence($exportRequest instanceof LicenceCmfOrganizationER);
  43. $licenceCmf->setOrganizationName($organization->getName());
  44. $licenceCmf->setOrganizationIdentifier($organization->getIdentifier());
  45. $parentFederation = $organization->getNetworkOrganizations()->get(0)?->getParent();
  46. if ($parentFederation !== null) {
  47. $licenceCmf->setFederationName($parentFederation->getName());
  48. }
  49. $licenceCmf->setColor(
  50. $this->getLicenceColor($exportRequest->getYear())
  51. );
  52. $logo = $organization->getLogo();
  53. if ($logo) {
  54. $licenceCmf->setLogo($logo);
  55. }
  56. $presidents = $this->accessRepository->findByOrganizationAndMission($organization, FunctionEnum::PRESIDENT);
  57. if (count($presidents) > 0) {
  58. $president = $presidents[0]->getPerson();
  59. if ($president !== null) {
  60. $licenceCmf->setPersonId($president->getId());
  61. $licenceCmf->setPersonGender($president->getGender() ?? null);
  62. $licenceCmf->setPersonFirstName($president->getGivenName());
  63. $licenceCmf->setPersonLastName($president->getName());
  64. }
  65. }
  66. $cmf = $this->organizationRepository->find(self::CMF_ID);
  67. /** @noinspection NullPointerExceptionInspection */
  68. $qrCode = $cmf->getParameters()->getQrCode();
  69. if ($qrCode) {
  70. $licenceCmf->setQrCode($qrCode);
  71. }
  72. $model = new LicenceCmfCollection();
  73. $model->setLicences([$licenceCmf]);
  74. return $model;
  75. }
  76. /**
  77. * @param LicenceCmfOrganizationER $exportRequest
  78. */
  79. protected function getFileBasename(ExportRequest $exportRequest): string
  80. {
  81. return 'licence_cmf_'.$exportRequest->getYear().'.pdf';
  82. }
  83. /**
  84. * Retourne le type de fichier tel qu'il apparait au niveau du champ File.type.
  85. */
  86. protected function getFileType(): FileTypeEnum
  87. {
  88. return FileTypeEnum::LICENCE_CMF;
  89. }
  90. /**
  91. * Retourne la couleur de licence pour l'année donnée.
  92. */
  93. protected function getLicenceColor(int $year): string
  94. {
  95. if (!($year > self::LICENCE_CMF_COLOR_START_YEAR)) {
  96. return self::LICENCE_CMF_COLOR[0];
  97. }
  98. return self::LICENCE_CMF_COLOR[($year - self::LICENCE_CMF_COLOR_START_YEAR) % count(self::LICENCE_CMF_COLOR)];
  99. }
  100. }