<?php
namespace App\Utils\Uniqskills;
use App\Entity\Gos\Country;
use App\Entity\Gos\ProductVariant;
use App\Entity\Gos\Uniqskills\Course;
use App\Utils\LandingCourse;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class SuggestedCoursesService
{
private $em;
private $request;
private $landingService;
public function __construct(EntityManagerInterface $em,
RequestStack $requestStack,
LandingCourse $landingService)
{
$this->em = $em;
$this->request = $requestStack->getCurrentRequest();
$this->landingService = $landingService;
}
public function findSuggestedCourses(?Course $course): array
{
$suggestedCourses = [];
if ($course === null || $course->getCourse()->isEmpty())
{
return $suggestedCourses;
}
foreach ($course->getCourse() as $suggestedCourse)
{
if ($course->getName() === $suggestedCourse->getName() || !$course->getIsActive())
{
continue;
}
if ($suggestedCourse->getHideAfter() !== null)
{
if ($suggestedCourse->getNextCourse() === null)
{
continue;
}
$suggestedCourse = $suggestedCourse->getNextCourse();
}
$suggestedCourses[] = $this->prepareCourseData($suggestedCourse);
}
return $suggestedCourses;
}
private function prepareCourseData(Course $course): array
{
$landingData = $this->landingService->prepareLandingData($course);
$variant = self::chooseDefaultProductVariant($landingData['productVariants']);
$courseData = [
'name' => $course->getName(),
'slug' => $course->getSlug(),
'shortbody' => $course->getShortBody(),
'isContinuous' => $course->getIsContinuous(),
'startDate' => $course->getStartDate() >= new \DateTime() ? $course->getStartDate() : null,
'category' => $course->getCategory() ? $course->getCategory()->getCategoryName() : '',
'background' => 'uploads/' . $course->getImageName(),
'link' => [
'routing' => 'fmUniqskillsCourseLanding',
'category' => $course->getCategory()->getCategorySlug(),
'slug' => $course->getSlug()
],
'defaultProductVariant' => $variant,
'country' => $variant ? $variant->getCountry() : null,
'price' => $this->getCoursePrice($course, $variant)
];
if ($courseData['country'] === null)
{
$courseData['country'] = $this->em->getRepository(Country::class)->findOneByAlpha2($this->request->getSession()->get('userLocale', 'pl'));
}
return $courseData;
}
private function getCoursePrice(Course $course, ?ProductVariant $variant)
{
if ($variant === null || $variant->getCountry() === null || $variant->getCountry()->getAlpha2() === 'PL')
{
return $course->getDefaultPrice();
}
return $variant ? $variant->getFullPrice('gross') : null;
}
private static function chooseDefaultProductVariant(array $productVariants): ?ProductVariant
{
$activeVariants = array_filter($productVariants, function (ProductVariant $pv) {
return $pv->isActive();
});
if (empty($activeVariants))
{
return null;
}
$lifeTimeVariants = array_filter($activeVariants, function (ProductVariant $pv) {
return strpos($pv->getWorkingTitle(), 'nieoznaczony') !== false;
});
if (!empty($lifeTimeVariants))
{
return reset($lifeTimeVariants);
}
return reset($activeVariants);
}
}