|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Serializer; |
|
6
|
|
|
|
|
7
|
|
|
use App\Model\CourseInterface; |
|
8
|
|
|
use App\Model\LessonInterface; |
|
9
|
|
|
use App\Repository\UserLessonRepositoryInterface; |
|
10
|
|
|
use function array_key_exists; |
|
11
|
|
|
use function is_array; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
13
|
|
|
use Symfony\Component\Security\Core\Security; |
|
14
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
15
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
16
|
|
|
use Vich\UploaderBundle\Templating\Helper\UploaderHelper; |
|
17
|
|
|
|
|
18
|
|
|
class CoverImageSerializer implements NormalizerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
private $uploaderHelper; |
|
21
|
|
|
private $normalizer; |
|
22
|
|
|
private $requestStack; |
|
23
|
|
|
private $userLessonRepository; |
|
24
|
|
|
private $security; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct( |
|
27
|
|
|
UploaderHelper $uploaderHelper, |
|
28
|
|
|
UserLessonRepositoryInterface $userLessonRepository, |
|
29
|
|
|
ObjectNormalizer $normalizer, |
|
30
|
|
|
Security $security, |
|
31
|
|
|
RequestStack $requestStack |
|
32
|
|
|
) { |
|
33
|
|
|
$this->uploaderHelper = $uploaderHelper; |
|
34
|
|
|
$this->userLessonRepository = $userLessonRepository; |
|
35
|
|
|
$this->normalizer = $normalizer; |
|
36
|
|
|
$this->security = $security; |
|
37
|
|
|
$this->requestStack = $requestStack; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function normalize($object, $format = null, array $context = []) |
|
41
|
|
|
{ |
|
42
|
|
|
$data = $this->normalizer->normalize($object, $format, $context); |
|
43
|
|
|
|
|
44
|
|
|
if (is_array($data) && array_key_exists('cover_image_name', $data)) { |
|
45
|
|
|
$imagePath = $this->uploaderHelper->asset($object, 'coverImageFile'); |
|
46
|
|
|
$currentRequest = $this->requestStack->getCurrentRequest(); |
|
47
|
|
|
if (null !== $imagePath && null !== $currentRequest && false === strpos($imagePath, '://')) { |
|
48
|
|
|
$imagePath = $currentRequest->getSchemeAndHttpHost().$imagePath; |
|
49
|
|
|
} |
|
50
|
|
|
$data['href']['coverImageUrl'] = $imagePath; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($object instanceof LessonInterface) { |
|
54
|
|
|
$userLesson = $this->userLessonRepository->findOneBy( |
|
|
|
|
|
|
55
|
|
|
['user' => $this->security->getUser(), 'lesson' => $object] |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
if (null === $userLesson) { |
|
59
|
|
|
$data['completed'] = null; |
|
60
|
|
|
} else { |
|
61
|
|
|
$data['completed'] = $userLesson->getCompleted(); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $data; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function supportsNormalization($data, $format = null): bool |
|
69
|
|
|
{ |
|
70
|
|
|
return $data instanceof CourseInterface || $data instanceof LessonInterface; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|