LicenceCmfExporter.php 4.1 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\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
  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. protected function buildModel(ExportRequest $exportRequest): LicenceCmfCollection
  31. {
  32. $organization = $this->accessRepository->find($exportRequest->getRequesterId())?->getOrganization();
  33. if ($organization === null) {
  34. throw new \RuntimeException('Unable to determine the organization of the curent user; abort.');
  35. }
  36. $licenceCmf = new LicenceCmf();
  37. $licenceCmf->setId($organization->getId());
  38. $licenceCmf->setYear($exportRequest->getYear());
  39. $licenceCmf->setIsOrganizationLicence( $exportRequest instanceof LicenceCmfOrganizationER);
  40. $licenceCmf->setOrganizationName($organization->getName());
  41. $licenceCmf->setOrganizationIdentifier($organization->getIdentifier());
  42. $parentFederation = $organization->getNetworkOrganizations()->get(0)?->getParent();
  43. if ($parentFederation !== null) {
  44. $licenceCmf->setFederationName($parentFederation->getName());
  45. }
  46. $licenceCmf->setColor(
  47. $this->getLicenceColor($exportRequest->getYear())
  48. );
  49. $logo = $organization->getLogo();
  50. if ($logo) {
  51. $licenceCmf->setLogoUri(
  52. $this->storage->getDownloadIri($logo)
  53. );
  54. }
  55. $presidents = $this->accessRepository->findByOrganizationAndMission($organization, FunctionEnum::PRESIDENT()->getValue());
  56. if (count($presidents) > 0) {
  57. $president = $presidents[0]->getPerson();
  58. $licenceCmf->setPersonId($president->getId());
  59. $licenceCmf->setPersonGender($president->getGender() ?? '');
  60. $licenceCmf->setPersonFirstName($president->getGivenName());
  61. $licenceCmf->setPersonLastName($president->getName());
  62. }
  63. $cmf = $this->organizationRepository->find(self::CMF_ID);
  64. /** @noinspection NullPointerExceptionInspection */
  65. $qrCode = $cmf->getParameters()?->getQrCode();
  66. if ($qrCode) {
  67. $licenceCmf->setQrCodeUri(
  68. $this->storage->getDownloadIri($qrCode)
  69. );
  70. }
  71. $model = new LicenceCmfCollection();
  72. $model->setLicences([$licenceCmf]);
  73. return $model;
  74. }
  75. /**
  76. * @param ExportRequest $exportRequest
  77. * @return string
  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. * @return FileTypeEnum
  87. */
  88. protected function getFileType(): FileTypeEnum {
  89. return FileTypeEnum::LICENCE_CMF();
  90. }
  91. /**
  92. * Retourne la couleur de licence pour l'année donnée
  93. *
  94. * @param int $year
  95. * @return string
  96. */
  97. protected function getLicenceColor(int $year): string {
  98. if (!($year > self::LICENCE_CMF_COLOR_START_YEAR)) {
  99. return self::LICENCE_CMF_COLOR[0];
  100. }
  101. return self::LICENCE_CMF_COLOR[($year - self::LICENCE_CMF_COLOR_START_YEAR) % count(self::LICENCE_CMF_COLOR)];
  102. }
  103. }