Utils.php 10 KB

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