Passed
Push — master ( 2da3b2...e3a339 )
by Angel Fernando Quiroz
06:59
created

SessionNormalizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 39
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supportsNormalization() 0 7 2
A normalize() 0 9 1
A getSupportedTypes() 0 3 1
A getSessionAccessVisiblity() 0 4 1
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Serializer\Normalizer;
8
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\ServiceHelper\UserHelper;
11
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
12
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
13
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
14
15
class SessionNormalizer implements NormalizerInterface, NormalizerAwareInterface
16
{
17
    use NormalizerAwareTrait;
18
19
    public function __construct(
20
        private readonly UserHelper $userHelper,
21
    ) {}
22
23
    private const ALREADY_CALLED = 'SESSION_NORMALIZER_ALREADY_CALLED';
24
25
    public function normalize($object, ?string $format = null, array $context = []): array
26
    {
27
        $context[self::ALREADY_CALLED] = true;
28
29
        $data = $this->normalizer->normalize($object, $format, $context);
30
31
        $data['accessVisibility'] = $this->getSessionAccessVisiblity($object);
32
33
        return $data;
34
    }
35
36
    public function supportsNormalization($data, ?string $format = null, array $context = []): bool
37
    {
38
        if (isset($context[self::ALREADY_CALLED])) {
39
            return false;
40
        }
41
42
        return $data instanceof Session;
43
    }
44
45
    public function getSupportedTypes(?string $format): array
46
    {
47
        return [Session::class => false];
48
    }
49
50
    private function getSessionAccessVisiblity(Session $session): int
51
    {
52
        return $session->checkAccessVisibilityByUser(
53
            $this->userHelper->getCurrent()
54
        );
55
    }
56
}
57