src/Service/CartService.php line 115

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: Igor Ratsun
  5.  * Date: 27.09.2021
  6.  * Time: 12:55
  7.  */
  8. namespace App\Service;
  9. use App\Config\CartConfig;
  10. use App\Entity\PromoCode;
  11. use App\Entity\Speaker;
  12. use App\Entity\SpeakerConsultation;
  13. use App\Entity\SpeakerCourse;
  14. use App\Repository\SpeakerConsultationRepository;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class CartService
  18. {
  19.     const SESSION_KEY 'cart_bag';
  20.     private SpeakerConsultationRepository $speakerConsultationRepository;
  21.     private EntityManagerInterface $entityManager;
  22.     private RequestStack $requestStack;
  23.     public function __construct(
  24.         SpeakerConsultationRepository $speakerConsultationRepository,
  25.         EntityManagerInterface        $entityManager,
  26.         RequestStack                  $requestStack
  27.     )
  28.     {
  29.         $this->speakerConsultationRepository $speakerConsultationRepository;
  30.         $this->entityManager $entityManager;
  31.         $this->requestStack $requestStack;
  32.     }
  33.     public function freezeConsultation($id$type): bool
  34.     {
  35.         $consultation $this->speakerConsultationRepository->findIfNotFrozen($id);
  36.         if (!is_null($consultation)) {
  37.             $expired = (new \DateTime())->modify('+' CartConfig::FREEZE_MIN ' minutes');
  38.             $this->addConsultation($consultation$type$expired);
  39.             // Update user consultation
  40.             foreach ($this->speakerConsultationRepository->findBy(['id' => $this->getConsultationsIds()]) as $item) {
  41.                 $item->setFreezeDate($expired);
  42.                 $this->entityManager->persist($item);
  43.             }
  44.             $this->entityManager->flush();
  45.             return true;
  46.         }
  47.         return false;
  48.     }
  49.     protected function addConsultation(SpeakerConsultation $consultation$type, \DateTime $expired): CartService
  50.     {
  51.         $consultations $this->getConsultations();
  52.         $consultations[$consultation->getSpeaker()->getId()][$consultation->getId()] = [
  53.             'id'   => $consultation->getId(),
  54.             'type' => $type,
  55.         ];
  56.         $this->setData([
  57.             'consultations' => $consultations,
  58.             'expired'       => $expired,
  59.         ]);
  60.         return $this;
  61.     }
  62.     public function addPromoCode(PromoCode $promoCode): CartService
  63.     {
  64.         $promoCodes $this->getPromoCodes();
  65.         $promoCodes[$promoCode->getSpeaker()->getId()] = $promoCode->getId();
  66.         $this->setData(['promoCodes' => $promoCodes]);
  67.         return $this;
  68.     }
  69.     public function removePromoCodeBySpeakerId($speakerId): CartService
  70.     {
  71.         $promoCodes $this->getPromoCodes();
  72.         if (isset($promoCodes[$speakerId])) {
  73.             unset($promoCodes[$speakerId]);
  74.             $this->setData(['promoCodes' => $promoCodes]);
  75.         }
  76.         return $this;
  77.     }
  78.     public function getConsultations()
  79.     {
  80.         return $this->getData()['consultations'] ?? [];
  81.     }
  82.     public function getExpired()
  83.     {
  84.         return $this->getData()['expired'] ?? null;
  85.     }
  86.     public function getPromoCodes()
  87.     {
  88.         return $this->getData()['promoCodes'] ?? [];
  89.     }
  90.     public function getTimeZone(): string
  91.     {
  92.         return $this->getData()['timezone'] ?? (new \DateTime())->getTimezone()->getName();
  93.     }
  94.     public function clearConsultations(): bool
  95.     {
  96.         foreach ($this->speakerConsultationRepository->findBy(['id' => $this->getConsultationsIds()]) as $item) {
  97.             $item->setFreezeDate(null);
  98.             $this->entityManager->persist($item);
  99.         }
  100.         $this->entityManager->flush();
  101.         $this->setData(['consultations' => [], 'expired' => null]);
  102.         return true;
  103.     }
  104.     public function setTimeZone(string $timezone): CartService
  105.     {
  106.         if (strpos($timezone'/')) {
  107.             $this->setData(['timezone' => $timezone]);
  108.         }
  109.         return $this;
  110.     }
  111.     public function getConsultationsIds(): array
  112.     {
  113.         $ids = [];
  114.         foreach ($this->getConsultations() as $data) {
  115.             foreach (array_keys($data) as $id) {
  116.                 $ids[] = $id;
  117.             }
  118.         }
  119.         return $ids;
  120.     }
  121.     public function removeConsultation(SpeakerConsultation $consultation): CartService
  122.     {
  123.         $consultations $this->getConsultations();
  124.         if (isset($consultations[$consultation->getSpeaker()->getId()][$consultation->getId()])) {
  125.             $consultation->setFreezeDate(null);
  126.             $this->entityManager->persist($consultation);
  127.             $this->entityManager->flush();
  128.             unset($consultations[$consultation->getSpeaker()->getId()][$consultation->getId()]);
  129.             if (empty($consultations[$consultation->getSpeaker()->getId()])) {
  130.                 unset($consultations[$consultation->getSpeaker()->getId()]);
  131.             }
  132.             $this->setData(['consultations' => $consultations]);
  133.         }
  134.         if (empty($consultations)) {
  135.             $this->setData(['expired' => null]);
  136.         }
  137.         return $this;
  138.     }
  139.     public function clear(): CartService
  140.     {
  141.         $this->setData($this->getDefault());
  142.         return $this;
  143.     }
  144.     public function getServices()
  145.     {
  146.         return $this->getData()['services'] ?? [];
  147.     }
  148.     public function addService($codeSpeaker $speaker): CartService
  149.     {
  150.         $services $this->getServices();
  151.         if (empty($services[$speaker->getId()]) || !in_array($code$services[$speaker->getId()])) {
  152.             $services[$speaker->getId()][] = $code;
  153.             $this->setData(['services' => $services]);
  154.         }
  155.         return $this;
  156.     }
  157.     public function removeService($codeSpeaker $speaker): CartService
  158.     {
  159.         $services $this->getServices();
  160.         if (!empty($services[$speaker->getId()]) && in_array($code$services[$speaker->getId()])) {
  161.             unset($services[$speaker->getId()][array_search($code$services[$speaker->getId()])]);
  162.             if (empty($services[$speaker->getId()])) {
  163.                 unset($services[$speaker->getId()]);
  164.             }
  165.             $this->setData(['services' => $services]);
  166.         }
  167.         return $this;
  168.     }
  169.     public function getPremiumPackages()
  170.     {
  171.         return $this->getData()['premium_packages'] ?? [];
  172.     }
  173.     public function addPremiumPackage($codeSpeaker $speaker): CartService
  174.     {
  175.         $package $this->getPremiumPackages();
  176.         if (empty($package[$speaker->getId()]) || !in_array($code$package[$speaker->getId()][$speaker->getId()])) {
  177.             $package[$speaker->getId()][] = $code;
  178.             $this->setData(['premium_packages' => $package]);
  179.         }
  180.         return $this;
  181.     }
  182.     public function removePremiumPackage($codeSpeaker $speaker): CartService
  183.     {
  184.         $package $this->getPremiumPackages();
  185.         if (!empty($package[$speaker->getId()]) && in_array($code$package[$speaker->getId()])) {
  186.             unset($package[$speaker->getId()][array_search($code$package[$speaker->getId()])]);
  187.             if (empty($package[$speaker->getId()])) {
  188.                 unset($package[$speaker->getId()]);
  189.             }
  190.             $this->setData(['premium_packages' => $package]);
  191.         }
  192.         return $this;
  193.     }
  194.     public function getCourses()
  195.     {
  196.         return $this->getData()['courses'] ?? [];
  197.     }
  198.     public function addCourse(SpeakerCourse $course): CartService
  199.     {
  200.         $courses $this->getCourses();
  201.         if (empty($courses[$course->getSpeaker()->getId()]) || !in_array($course->getId(), $courses[$course->getSpeaker()->getId()])) {
  202.             $courses[$course->getSpeaker()->getId()][] = $course->getId();
  203.             $this->setData(['courses' => $courses]);
  204.         }
  205.         return $this;
  206.     }
  207.     public function removeCourse(SpeakerCourse $course): CartService
  208.     {
  209.         $courses $this->getCourses();
  210.         if (!empty($courses[$course->getSpeaker()->getId()]) && in_array($course->getId(), $courses[$course->getSpeaker()->getId()])) {
  211.             unset($courses[$course->getSpeaker()->getId()][array_search($course->getId(), $courses[$course->getSpeaker()->getId()])]);
  212.             if (empty($courses[$course->getSpeaker()->getId()])) {
  213.                 unset($courses[$course->getSpeaker()->getId()]);
  214.             }
  215.             $this->setData(['courses' => $courses]);
  216.         }
  217.         return $this;
  218.     }
  219.     public function getCoursesIds(): array
  220.     {
  221.         $ids = [];
  222.         foreach ($this->getCourses() as $data) {
  223.             foreach ($data as $id) {
  224.                 $ids[] = $id;
  225.             }
  226.         }
  227.         return $ids;
  228.     }
  229.     public function getItemsCount()
  230.     {
  231.         $count count($this->getConsultationsIds());
  232.         foreach ($this->getCourses() as $items) {
  233.             $count += count($items);
  234.         }
  235.         return $count;
  236.     }
  237.     protected function getData()
  238.     {
  239.         return $this->requestStack->getSession()->get(self::SESSION_KEY$this->getDefault());
  240.     }
  241.     protected function setData(array $data): CartService
  242.     {
  243.         $this->requestStack->getSession()->set(self::SESSION_KEYarray_merge($this->getData(), $data));
  244.         return $this;
  245.     }
  246.     protected function getDefault(): array
  247.     {
  248.         return [
  249.             'consultations'    => [],
  250.             'expired'          => null,
  251.             'promoCodes'       => [],
  252.             'services'         => [],
  253.             'premium_packages' => [],
  254.             'courses'          => [],
  255.         ];
  256.     }
  257. }