LicenceCmfExporter.php 4.1 KB

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