src/Service/Registration/Session.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\Service\Registration;
  3. use App\Entity\ContactEmail;
  4. use App\Entity\ContactPhone;
  5. use App\Entity\Family;
  6. use App\Entity\Person;
  7. use App\Entity\Profiling;
  8. use App\Entity\Registration;
  9. use App\Entity\Season;
  10. use App\Entity\Section;
  11. use App\Entity\SectionMeta;
  12. use App\Entity\User;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. //TODO: clean ce service
  17. class Session
  18. {
  19.     private static string $sessionKeyPrefix 'registration_';
  20.     private SessionInterface $session;
  21.     private ?string $registrationType 'self';
  22.     private ?Season $season null;
  23.     private ?Family $family null;
  24.     private ?Person $person null;
  25.     private ?int $donation null;
  26.     private ?Registration $registration null;
  27.     private ?User $user null;
  28.     /**
  29.      * @var Person[]
  30.      */
  31.     private array $tutors = [];
  32.     private $community;
  33.     private int $step 1;
  34.     public function __construct(private readonly EntityManagerInterface $emRequestStack $requestStack, private readonly OrderManager $orderManager)
  35.     {
  36.         $this->session $requestStack->getSession();
  37.         $this->init();
  38.     }
  39.     public function init()
  40.     {
  41.         $this->initRegistrationType();
  42.         $this->initSeason();
  43.         $this->initFamily();
  44.         $this->initPerson();
  45.         $this->initDonation();
  46.         $this->initRegistration();
  47.         $this->initUser();
  48.         $this->initCommunity();
  49.         $this->initTutors();
  50.         $this->step $this->getKey('step') ?? 1;
  51.     }
  52.     public function dump($die false): void
  53.     {
  54.         dump(
  55.             $this->getRegistrationType(),
  56.             $this->getStep(),
  57.             $this->getSeason(),
  58.             $this->getFamily(),
  59.             $this->getPerson()
  60.         );
  61.         if ($die) {
  62.             die;
  63.         }
  64.     }
  65.     public function reset($upTo 0)
  66.     {
  67.         $this->removeKey('family');
  68.         $this->removeKey('person');
  69.         $this->removeKey('registration');
  70.         $this->removeKey('donation');
  71.         $this->removeKey('season');
  72.         $this->removeKey('registrationType');
  73.         $this->removeKey('user');
  74.         $this->removeKey('community');
  75.         $this->removeKey('step');
  76.         $this->removeKey('tutors');
  77.     }
  78.     private function getKey(string $key): mixed
  79.     {
  80.         return $this->session->get(self::$sessionKeyPrefix.$key);
  81.     }
  82.     private function setKey(string $key$value null): void
  83.     {
  84.         if (null === $value) {
  85.             $this->removeKey($key);
  86.         } else {
  87.             $this->session->set(self::$sessionKeyPrefix.$key$value);
  88.         }
  89.     }
  90.     public function removeKey(string $key)
  91.     {
  92.         $this->session->remove(self::$sessionKeyPrefix.$key);
  93.     }
  94.     private function initRegistrationType()
  95.     {
  96.         $this->registrationType $this->getKey('registrationType');
  97.     }
  98.     public function getRegistrationType(): ?string
  99.     {
  100.         return $this->registrationType;
  101.     }
  102.     public function setRegistrationType(?string $registrationType): Session
  103.     {
  104.         $this->setKey('registrationType'$registrationType);
  105.         $this->registrationType $registrationType;
  106.         return $this;
  107.     }
  108.     private function initSeason(): void
  109.     {
  110.         $id $this->getKey('season');
  111.         if ($id) {
  112.             $this->season $this->em->find(Season::class, $id);
  113.         }
  114.     }
  115.     public function getSeason(): ?Season
  116.     {
  117.         return $this->season;
  118.     }
  119.     public function setSeason(?Season $season): Session
  120.     {
  121.         $id $season?->getId();
  122.         $this->setKey('season'$id);
  123.         $this->season $season;
  124.         return $this;
  125.     }
  126.     private function initFamily()
  127.     {
  128.         $id $this->getKey('family');
  129.         if ($id) {
  130.             $this->family $this->em->find(Family::class, $id);
  131.         }
  132.         return;
  133.         $this->family $this->getKey('family');
  134. //        if (null !== ($id = $this->family?->getId())) {
  135. //            $this->family = $this->em->find(Family::class, $id);
  136. //        }
  137.     }
  138.     public function getFamily(): ?Family
  139.     {
  140.         return $this->family;
  141.     }
  142.     public function setFamily(?Family $family): Session
  143.     {
  144.         $id $family?->getId();
  145.         $this->setKey('family'$id);
  146.         $this->family $family;
  147.         return $this;
  148.     }
  149.     private function initPerson()
  150.     {
  151.         $id $this->getKey('person');
  152.         if ($id) {
  153.             $this->person $this->em->find(Person::class, $id);
  154.         }
  155.         return;
  156.         $this->person $this->getKey('person');
  157.         if ($this->person) {
  158.             if (($contact $this->person->getFirstEmail()) && $this->person->getFirstEmail()->getId()) {
  159.                 $this->person->setFirstEmail($this->em->find(ContactEmail::class, $contact->getId()));
  160.             }
  161.             if (($contact $this->person->getSecondEmail()) && $this->person->getSecondEmail()->getId()) {
  162.                 $this->person->setSecondEmail($this->em->find(ContactEmail::class, $contact->getId()));
  163.             }
  164.             if (($contact $this->person->getFirstPhone()) && $this->person->getFirstPhone()->getId()) {
  165.                 $this->person->setFirstPhone($this->em->find(ContactPhone::class, $contact->getId()));
  166.             }
  167.             if (($contact $this->person->getSecondPhone()) && $this->person->getSecondPhone()->getId()) {
  168.                 $this->person->setSecondPhone($this->em->find(ContactPhone::class, $contact->getId()));
  169.             }
  170.         }
  171. //        if (null !== ($id = $this->person?->getId())) {
  172. //            $this->person = $this->em->find(Person::class, $id);
  173. //        }
  174.     }
  175.     public function getPerson(): ?Person
  176.     {
  177.         return $this->person;
  178.     }
  179.     public function setPerson(?Person $person): Session
  180.     {
  181.         $id $person?->getId();
  182.         $this->setKey('person'$id);
  183.         $this->person $person;
  184.         return $this;
  185.     }
  186.     private function initDonation()
  187.     {
  188.         $this->donation $this->getKey('donation');
  189.     }
  190.     public function getDonation(): ?int
  191.     {
  192.         return $this->donation;
  193.     }
  194.     public function setDonation(?int $donation): Session
  195.     {
  196.         $this->setKey('donation'$donation);
  197.         $this->donation $donation;
  198.         return $this;
  199.     }
  200.     private function initRegistration()
  201.     {
  202.         $this->registration $this->getKey('registration');
  203.         if ($this->registration) {
  204.             if ($this->registration->getSeason()) {
  205.                 $season $this->em->find(Season::class, $this->registration->getSeason()->getId());
  206.                 $this->registration->setSeason($season);
  207.             }
  208.         }
  209. //        if (null !== ($id = $this->registration?->getId())) {
  210. //            $this->registration = $this->em->find(Registration::class, $id);
  211. //        }
  212.     }
  213.     public function getRegistration(): ?Registration
  214.     {
  215.         return $this->registration;
  216.     }
  217.     public function setRegistration(?Registration $registration): Session
  218.     {
  219.         $this->registration $registration;
  220.         if (null === $registration) {
  221.             $this->removeKey('registration');
  222.         } else {
  223.             $this->setKey('registration'$registration);
  224.         }
  225.         return $this;
  226.     }
  227.     private function initUser()
  228.     {
  229.         $this->user $this->getKey('user');
  230.     }
  231.     public function getUser(): ?User
  232.     {
  233.         return $this->user;
  234.     }
  235.     public function setUser(?User $user): Session
  236.     {
  237.         $this->user $user;
  238.         if (null === $user) {
  239.             $this->removeKey('user');
  240.         } else {
  241.             $this->setKey('user'$user);
  242.         }
  243.         return $this;
  244.     }
  245.     private function initCommunity()
  246.     {
  247.         $this->community $this->getKey('community');
  248.         if (null === $this->community) {
  249.             $this->community = [
  250.                 'isSponsor' => null,
  251.                 'volunteer' => null,
  252.                 'donation'  => null,
  253.             ];
  254.         }
  255.         if (null === $this->community['volunteer'] && $this->person?->getMeta()?->getVolunteer()) {
  256.             $this->community['volunteer'] = $this->person->getMeta()->getVolunteer();
  257.         }
  258.         if (null === $this->community['isSponsor'] && $this->person?->getMeta()?->getIsSponsor()) {
  259.             $this->community['isSponsor'] = $this->person->getMeta()->getIsSponsor();
  260.         }
  261.     }
  262.     public function getCommunity(): array
  263.     {
  264.         return $this->community;
  265.     }
  266.     public function setCommunity(array $community): Session
  267.     {
  268.         $this->community $community;
  269.         $this->setKey('community'$community);
  270.         return $this;
  271.     }
  272.     private function initTutors()
  273.     {
  274.         $tutors $this->getKey('tutors') ?? [];
  275.         foreach ($tutors as $tutor) {
  276.             if ($tutor->getId()) {
  277.                 $tutor $this->em->find(Person::class, $tutor->getId());
  278.             }
  279.             $this->tutors[] = $tutor;
  280.         }
  281.     }
  282.     /**
  283.      * @return Person[]
  284.      */
  285.     public function getTutors(): array
  286.     {
  287.         return $this->tutors;
  288.     }
  289.     /**
  290.      * @param Person[] $tutor
  291.      * @return Session
  292.      */
  293.     public function addTutor(Person $tutor): Session
  294.     {
  295.         $this->tutors[] = $tutor;
  296.         $this->setTutors($this->tutors);
  297.         return $this;
  298.     }
  299.     public function getTutor($key): ?Person
  300.     {
  301.         if (isset($this->tutors[$key])) {
  302.             return $this->tutors[$key];
  303.         }
  304.         return null;
  305.     }
  306.     public function setTutor($keyPerson $tutor): self
  307.     {
  308.         if (isset($this->tutors[$key])) {
  309.             $this->tutors[$key] = $tutor;
  310.             $this->setTutors($this->tutors);
  311.         }
  312.         return $this;
  313.     }
  314.     public function removeTutor($key): self
  315.     {
  316.         if ($this->tutors[$key]) {
  317.             unset($this->tutors[$key]);
  318.             $this->setTutors($this->tutors);
  319.         }
  320.         return $this;
  321.     }
  322.     /**
  323.      * @param Person[] $tutors
  324.      * @return Session
  325.      */
  326.     public function setTutors(array $tutors): Session
  327.     {
  328.         $this->setKey('tutors'$tutors);
  329.         return $this;
  330.     }
  331.     public function save()
  332.     {
  333.         //TODO: supprimer les tuteurs si majeur
  334.         //TODO: les supprimer en AJAX au changement d'âge ?
  335.         $person $this->person;
  336.         $family $this->family;
  337.         $tutors $this->tutors;
  338.         $registration $this->registration;
  339.         $community $this->community;
  340.         $family->addPerson($person);
  341.         $person->addRegistration($registration);
  342.         foreach ($tutors ?? [] as $tutor) {
  343.             if (null === $tutor->getId()) {
  344.                 $family->addPerson($tutor);
  345.                 if (($tutor->getUser() === null)
  346.                     && ($tutor->getFirstEmail()->getOwner() === $tutor)
  347.                     && (null === $this->em->getRepository(User::class)
  348.                             ->findOneBy(['email' => $tutor->getFirstEmail()->getEmail()]))) {
  349.                     $tutorUser = new User();
  350.                     $tutorUser->setPerson($tutor)
  351.                         ->setEmail($tutor->getFirstEmail()->getEmail())
  352.                         ->setPlainPassword(uniqid(''true));
  353.                 }
  354.             }
  355.             $person->addTutor($tutor);
  356.         }
  357.         $person->getMeta()
  358.             ->setVolunteer($community['volunteer'])
  359.             ->setIsSponsor($community['isSponsor']);
  360.         if ($community['donation']) {
  361.             $this->orderManager->updateDonation($registration$community['donation']);
  362.         }
  363.         $season $this->em->find(Season::class, $registration->getSeason()->getId());
  364.         foreach ($person->getProfilingPersons() as $profilingPerson) {
  365.             if ($profilingPerson->getSeason()->getId() === $season->getId()) {
  366.                 $profiling $this->em->find(Profiling::class, $profilingPerson->getProfiling()->getId());
  367.                 $profilingPerson->setSeason($season)
  368.                     ->setPerson($person)
  369.                     ->setProfiling($profiling);
  370.             }
  371.         }
  372.         foreach ($person->getSectionPersons() as $sectionPerson) {
  373.             if ($sectionPerson->getSection()->getId() === $season->getSection()->getId()) {
  374.                 $section $this->em->find(Section::class, $sectionPerson->getSection()->getId());
  375.                 $sectionPerson->setSection($section)
  376.                     ->setPerson($person);;
  377.                 foreach ($sectionPerson->getSectionPersonMetas() as $sectionPersonMeta) {
  378.                     $sectionMeta $this->em->find(SectionMeta::class, $sectionPersonMeta->getSectionMeta()->getId());
  379.                     $sectionPersonMeta->setSectionMeta($sectionMeta);
  380.                 }
  381.             }
  382.         }
  383.     }
  384.     public function getStep(): int
  385.     {
  386.         return $this->step;
  387.     }
  388.     public function setStep(int $step): Session
  389.     {
  390.         $this->step $step;
  391.         $this->setKey('step'$step);
  392.         return $this;
  393.     }
  394. }