Passed
Pull Request — master (#8)
by Paweł
03:21
created

CoverImageSerializer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 53
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A supportsNormalization() 0 3 2
B normalize() 0 26 8
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(
0 ignored issues
show
Bug introduced by
The method findOneBy() does not exist on App\Repository\UserLessonRepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to App\Repository\UserLessonRepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            /** @scrutinizer ignore-call */ 
55
            $userLesson = $this->userLessonRepository->findOneBy(
Loading history...
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