ParametersDataPersister.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\DataPersister\Organization;
  4. use App\DataPersister\BaseDataPersister;
  5. use App\Entity\Organization\Parameters;
  6. use App\Message\Command\Parameters\AverageChange;
  7. use App\Message\Command\Typo3\Typo3DeleteCommand;
  8. use App\Message\Command\Typo3\Typo3UndeleteCommand;
  9. use App\Message\Command\Typo3\Typo3UpdateCommand;
  10. use App\Service\OnChange\Organization\OnParametersChange;
  11. use Exception;
  12. use Symfony\Component\Messenger\MessageBusInterface;
  13. /**
  14. * Classe ParametersDataPersister qui est un custom dataPersister gérant la resource Parameters
  15. */
  16. class ParametersDataPersister extends BaseDataPersister
  17. {
  18. public function __construct(
  19. private MessageBusInterface $messageBus,
  20. private OnParametersChange $onParametersChange,
  21. private \App\Service\Network\Utils $networkUtils
  22. )
  23. {}
  24. public function supports($data, array $context = []): bool
  25. {
  26. return $data instanceof Parameters;
  27. }
  28. protected function validate($parameters): void
  29. {
  30. // Une structure CMF n'a pas le droit de désactiver son site typo3
  31. if (
  32. $parameters->getDesactivateOpentalentSiteWeb() === true &&
  33. $this->networkUtils->isCMFAndActiveNow($parameters->getOrganization())
  34. ) {
  35. throw new \RuntimeException('This structure is currently active in the CMF network, the website can not be disabled.');
  36. }
  37. }
  38. /**
  39. * @param Parameters $parameters
  40. * @throws Exception
  41. */
  42. public function prePersist($parameters): void{
  43. if(
  44. $this->previousData() &&
  45. $this->previousData()->getAdvancedEducationNotationType() !== $parameters->getAdvancedEducationNotationType()
  46. ){
  47. $this->onParametersChange->onAdvancedEducationNotationTypeChange($parameters);
  48. }
  49. //La date de début d'activité change
  50. if(
  51. $this->previousData() &&
  52. $this->previousData()->getMusicalDate() != $parameters->getMusicalDate()
  53. ){
  54. $this->onParametersChange->onMusicalDateChange(
  55. $parameters->getOrganization(),
  56. $this->previousData()->getMusicalDate()
  57. );
  58. }
  59. }
  60. /**
  61. * @param Parameters $parameters
  62. */
  63. public function postPersist($parameters): void{
  64. //La note maximale du suivi pédagogique change
  65. if(
  66. $this->previousData() &&
  67. $this->previousData()->getAverage() !== $parameters->getAverage()
  68. ){
  69. $this->messageBus->dispatch(
  70. new AverageChange($parameters->getId())
  71. );
  72. }
  73. // Le customDomain a été modifié, on met à jour le site typo3 (s'il est actif)
  74. if(
  75. $this->previousData() &&
  76. !$parameters->getDesactivateOpentalentSiteWeb() &&
  77. $this->previousData()->getCustomDomain() !== $parameters->getCustomDomain()
  78. ){
  79. $this->messageBus->dispatch(
  80. new Typo3UpdateCommand($parameters->getOrganization()->getId())
  81. );
  82. }
  83. // Le site web opentalent a été désactivé / réactivé
  84. if(
  85. $this->previousData() &&
  86. $this->previousData()->getDesactivateOpentalentSiteWeb() !== $parameters->getDesactivateOpentalentSiteWeb()
  87. ){
  88. if ($parameters->getDesactivateOpentalentSiteWeb()) {
  89. $this->messageBus->dispatch(
  90. new Typo3DeleteCommand($parameters->getOrganization()->getId())
  91. );
  92. } else {
  93. $this->messageBus->dispatch(
  94. new Typo3UndeleteCommand($parameters->getOrganization()->getId())
  95. );
  96. $this->messageBus->dispatch(
  97. new Typo3UpdateCommand($parameters->getOrganization()->getId())
  98. );
  99. }
  100. }
  101. }
  102. public function remove($data, array $context = []): void
  103. {
  104. throw new Exception('not supported', 500);
  105. }
  106. }