Utils.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service\Organization;
  4. use App\Entity\Organization\Organization;
  5. use App\Enum\Organization\OrganizationIdsEnum;
  6. use App\Enum\Organization\SettingsProductEnum;
  7. use App\Repository\Network\NetworkOrganizationRepository;
  8. use App\Service\Network\Utils as NetworkUtils;
  9. use App\Service\Utils\DatesUtils;
  10. use App\Service\Utils\UrlBuilder;
  11. /**
  12. * Service rassemblant des fonctions d'aide pour les questions se rapportant à l'organisation.
  13. */
  14. class Utils
  15. {
  16. public const START_DATE_KEY = 'dateStart';
  17. public const END_DATE_KEY = 'dateEnd';
  18. public const OUT_OF_NET_SUBDOMAIN = 'outofnet';
  19. public function __construct(
  20. private NetworkOrganizationRepository $networkOrganizationRepository,
  21. private NetworkUtils $networkUtils
  22. ) {
  23. }
  24. /**
  25. * Teste si l'organisation est considérée comme une structure == n'a pas un produit manager.
  26. *
  27. * @see UtilsTest::testIsStructureTest()
  28. */
  29. public function isStructure(Organization $organization): bool
  30. {
  31. return $organization->getSettings()->getProduct() !== SettingsProductEnum::MANAGER
  32. && $organization->getSettings()->getProduct() !== SettingsProductEnum::MANAGER_PREMIUM;
  33. }
  34. /**
  35. * Teste si l'organisation est considérée comme un manager == a un produit manager standard.
  36. *
  37. * @see UtilsTest::testIsManagerTest()
  38. */
  39. public function isManager(Organization $organization): bool
  40. {
  41. return $organization->getSettings()->getProduct() === SettingsProductEnum::MANAGER;
  42. }
  43. /**
  44. * Teste si l'organisation est considérée comme un school == a un produit school standard ou premium.
  45. *
  46. * @see UtilsTest::testIsSchool()
  47. */
  48. public function isSchool(Organization $organization): bool
  49. {
  50. return $organization->getSettings()->getProduct() === SettingsProductEnum::SCHOOL
  51. || $organization->getSettings()->getProduct() === SettingsProductEnum::SCHOOL_PREMIUM;
  52. }
  53. /**
  54. * Teste si l'organisation est considérée comme un school == a un produit artiste standard ou premium.
  55. *
  56. * @see UtilsTest::testIsArtist()
  57. */
  58. public function isArtist(Organization $organization): bool
  59. {
  60. return $organization->getSettings()->getProduct() === SettingsProductEnum::ARTIST
  61. || $organization->getSettings()->getProduct() === SettingsProductEnum::ARTIST_PREMIUM;
  62. }
  63. /**
  64. * Teste si l'organisation est la structure 2iOpenservice.
  65. *
  66. * @see UtilsTest::testIsOrganizationIs2ios()
  67. */
  68. public function is2iosOrganization(Organization $organization): bool
  69. {
  70. return $this->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::_2IOS);
  71. }
  72. /**
  73. * Test si l'organisation est la structure CMF.
  74. *
  75. * @see UtilsTest::testIsCMF()
  76. */
  77. public function isCMF(Organization $organization): bool
  78. {
  79. return $this->isOrganizationIdEqualTo($organization, OrganizationIdsEnum::CMF);
  80. }
  81. /**
  82. * Test si l'organisation est un dernier parent ET appartient à la CMF.
  83. *
  84. * @see UtilsTest::testIsLastParentAndCMF()
  85. */
  86. public function isLastParentAndCMF(Organization $organization): bool
  87. {
  88. return $this->networkOrganizationRepository->isLastParent($organization) && $this->networkUtils->isCMF($organization);
  89. }
  90. /**
  91. * Test si l'organisation est une structure (non manager) ET appartient à la CMF.
  92. *
  93. * @see UtilsTest::testIsStructureAndCMF()
  94. */
  95. public function isStructureAndCMF(Organization $organization): bool
  96. {
  97. return $this->isStructure($organization) && $this->networkUtils->isCMF($organization);
  98. }
  99. /**
  100. * Test si la structure est un manager ET qu'elle appartient à la CMF.
  101. *
  102. * @see UtilsTest::testIsManagerAndCMF()
  103. */
  104. public function isManagerAndCMF(Organization $organization): bool
  105. {
  106. return $this->isManager($organization) && $this->networkUtils->isCMF($organization);
  107. }
  108. /**
  109. * Test si l'organisation est un manager ET un dernier parent ET appartient à la CMF.
  110. *
  111. * @see UtilsTest::testIsManagerAndLastParentAndCMF()
  112. */
  113. public function isManagerAndLastParentAndCMF(Organization $organization): bool
  114. {
  115. return $this->isManager($organization) && $this->isLastParentAndCMF($organization);
  116. }
  117. /**
  118. * Test si l'organisation est un manager ET n'est pas un dernier parent ET appartient à la CMF.
  119. *
  120. * @see UtilsTest::testIsManagerAndNotLastParentAndCMF()
  121. */
  122. public function isManagerAndNotLastParentAndCMF(Organization $organization): bool
  123. {
  124. return $this->isManager($organization) && !$this->networkOrganizationRepository->isLastParent($organization) && $this->networkUtils->isCMF($organization);
  125. }
  126. /**
  127. * Test si l'id de l'organisation est celui passé en paramètre (doit faire partie des OrganizationIdsEnum).
  128. */
  129. public function isOrganizationIdEqualTo(Organization $organization, OrganizationIdsEnum $organizationIds): bool
  130. {
  131. return $organization->getId() === $organizationIds->value;
  132. }
  133. /**
  134. * Retourne l'année d'activité dans laquelle on se situe aujourd'hui.
  135. *
  136. * @throws \Exception
  137. *
  138. * @see UtilsTest::testGetOrganizationCurrentActivityYear()
  139. */
  140. public function getOrganizationCurrentActivityYear(Organization $organization): int
  141. {
  142. $today = DatesUtils::new('now');
  143. $year = (int) $today->format('Y');
  144. $musicalDate = $organization->getParameters()->getMusicalDate();
  145. if (empty($musicalDate)) {
  146. $musicalDate = new \DateTime($year.'-08-31');
  147. }
  148. $base_date = new \DateTime($musicalDate->format($year.'-m-d'));
  149. $dateStart = new \DateTime($year.'-01-01');
  150. if ($today >= $dateStart && $today < $base_date) {
  151. return $year - 1;
  152. } else {
  153. return $year;
  154. }
  155. }
  156. /**
  157. * Fonction permettant de récupérer les dates de début et de fin d'activité d'une structure selon une année.
  158. *
  159. * @return string[]
  160. *
  161. * @throws \Exception
  162. *
  163. * @see UtilsTest::testGetActivityPeriodsSwitchYear()
  164. */
  165. public function getActivityPeriodsSwitchYear(Organization $organization, int $year): array
  166. {
  167. $musicalDate = $organization->getParameters()->getMusicalDate();
  168. if (empty($musicalDate)) {
  169. $dateStart = new \DateTime($year.'-09-01');
  170. $dateEnd = new \DateTime(($year + 1).'-08-31');
  171. } else {
  172. $dateStart = new \DateTime($year.'-'.$musicalDate->format('m').'-'.$musicalDate->format('d'));
  173. $dateEnd = clone $dateStart;
  174. $dateEnd->add(new \DateInterval('P1Y'))->sub(new \DateInterval('P1D'));
  175. }
  176. return [
  177. self::START_DATE_KEY => $dateStart->format('Y-m-d'),
  178. self::END_DATE_KEY => $dateEnd->format('Y-m-d'),
  179. ];
  180. }
  181. /**
  182. * Permet de retrouver quelle sera la date d'activité correspondant à une date précise de l'année.
  183. *
  184. * @throws \Exception
  185. *
  186. * @see UtilsTest::testgetActivityYearSwitchDate()
  187. */
  188. public function getActivityYearSwitchDate(Organization $organization, \DateTimeInterface $date): int
  189. {
  190. $year = $date->format('Y');
  191. $musicalDate = $organization->getParameters()->getMusicalDate();
  192. if (empty($musicalDate)) {
  193. $startDate = new \DateTime($year.'-08-31');
  194. } else {
  195. $startDate = new \DateTime($musicalDate->format($year.'-m-d'));
  196. }
  197. $pivotDate = new \DateTime($year.'-01-01');
  198. if ($date >= $pivotDate && $date < $startDate) {
  199. return (int) ($year - 1);
  200. } else {
  201. return (int) $year;
  202. }
  203. }
  204. /**
  205. * Return the active subdomain of an organization as a string, or null.
  206. */
  207. public function getOrganizationActiveSubdomain(Organization $organization): mixed
  208. {
  209. foreach ($organization->getSubdomains() as $subdomain) {
  210. if ($subdomain->isActive()) {
  211. return $subdomain->getSubdomain();
  212. }
  213. }
  214. return null;
  215. }
  216. /**
  217. * Get the URL of the current website of the organization.
  218. *
  219. * @see https://ressources.opentalent.fr/display/SPEC/Preferences#Preferences-Siteinternet
  220. */
  221. public function getOrganizationWebsite(Organization $organization): mixed
  222. {
  223. $parameters = $organization->getParameters();
  224. if ($parameters->getDesactivateOpentalentSiteWeb()) {
  225. if ($parameters->getWebsite()) {
  226. return UrlBuilder::prependHttps($parameters->getWebsite());
  227. }
  228. return null;
  229. }
  230. if (!empty($parameters->getCustomDomain())) {
  231. return UrlBuilder::prependHttps($parameters->getCustomDomain());
  232. }
  233. $subdomain = $this->getOrganizationActiveSubdomain($organization);
  234. if (!$subdomain) {
  235. return null;
  236. }
  237. if ($subdomain === self::OUT_OF_NET_SUBDOMAIN) {
  238. return 'https://opentalent.fr';
  239. }
  240. return 'https://'.$subdomain.'.opentalent.fr';
  241. }
  242. /**
  243. * Does the organization own the given module.
  244. */
  245. public function hasModule(Organization $organization, string $module): bool
  246. {
  247. $modules = $organization->getSettings()->getModules();
  248. return !empty($modules) && in_array($module, $modules);
  249. }
  250. }