src/Utils/Uniqskills/SuggestedCoursesService.php line 83

Open in your IDE?
  1. <?php
  2. namespace App\Utils\Uniqskills;
  3. use App\Entity\Gos\Country;
  4. use App\Entity\Gos\ProductVariant;
  5. use App\Entity\Gos\Uniqskills\Course;
  6. use App\Utils\LandingCourse;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\RequestStack;
  9. class SuggestedCoursesService
  10. {
  11.     private $em;
  12.     private $request;
  13.     private $landingService;
  14.     public function __construct(EntityManagerInterface $em,
  15.                                 RequestStack $requestStack,
  16.                                 LandingCourse $landingService)
  17.     {
  18.         $this->em $em;
  19.         $this->request $requestStack->getCurrentRequest();
  20.         $this->landingService $landingService;
  21.     }
  22.     public function findSuggestedCourses(?Course $course): array
  23.     {
  24.         $suggestedCourses = [];
  25.         if ($course === null || $course->getCourse()->isEmpty())
  26.         {
  27.             return $suggestedCourses;
  28.         }
  29.         foreach ($course->getCourse() as $suggestedCourse)
  30.         {
  31.             if ($course->getName() === $suggestedCourse->getName() || !$course->getIsActive())
  32.             {
  33.                 continue;
  34.             }
  35.             if ($suggestedCourse->getHideAfter() !== null)
  36.             {
  37.                 if ($suggestedCourse->getNextCourse() === null)
  38.                 {
  39.                     continue;
  40.                 }
  41.                 $suggestedCourse $suggestedCourse->getNextCourse();
  42.             }
  43.             $suggestedCourses[] = $this->prepareCourseData($suggestedCourse);
  44.         }
  45.         return $suggestedCourses;
  46.     }
  47.     private function prepareCourseData(Course $course): array
  48.     {
  49.         $landingData $this->landingService->prepareLandingData($course);
  50.         $variant     self::chooseDefaultProductVariant($landingData['productVariants']);
  51.         $courseData = [
  52.             'name' => $course->getName(),
  53.             'slug' => $course->getSlug(),
  54.             'shortbody' => $course->getShortBody(),
  55.             'isContinuous' => $course->getIsContinuous(),
  56.             'startDate' => $course->getStartDate() >= new \DateTime() ? $course->getStartDate() : null,
  57.             'category' => $course->getCategory() ? $course->getCategory()->getCategoryName() : '',
  58.             'background' => 'uploads/' $course->getImageName(),
  59.             'link' => [
  60.                 'routing' => 'fmUniqskillsCourseLanding',
  61.                 'category' => $course->getCategory()->getCategorySlug(),
  62.                 'slug' => $course->getSlug()
  63.             ],
  64.             'defaultProductVariant' => $variant,
  65.             'country' => $variant $variant->getCountry() : null,
  66.             'price' => $this->getCoursePrice($course$variant)
  67.         ];
  68.         if ($courseData['country'] === null)
  69.         {
  70.             $courseData['country'] = $this->em->getRepository(Country::class)->findOneByAlpha2($this->request->getSession()->get('userLocale''pl'));
  71.         }
  72.         return $courseData;
  73.     }
  74.     private function getCoursePrice(Course $course, ?ProductVariant $variant)
  75.     {
  76.         if ($variant === null || $variant->getCountry() === null || $variant->getCountry()->getAlpha2() === 'PL')
  77.         {
  78.             return $course->getDefaultPrice();
  79.         }
  80.         return $variant $variant->getFullPrice('gross') : null;
  81.     }
  82.     private static function chooseDefaultProductVariant(array $productVariants): ?ProductVariant
  83.     {
  84.         $activeVariants array_filter($productVariants, function (ProductVariant $pv) {
  85.             return $pv->isActive();
  86.         });
  87.         if (empty($activeVariants))
  88.         {
  89.             return null;
  90.         }
  91.         $lifeTimeVariants array_filter($activeVariants, function (ProductVariant $pv) {
  92.             return strpos($pv->getWorkingTitle(), 'nieoznaczony') !== false;
  93.         });
  94.         if (!empty($lifeTimeVariants))
  95.         {
  96.             return reset($lifeTimeVariants);
  97.         }
  98.         return reset($activeVariants);
  99.     }
  100. }