Passed
Push — develop ( ef53f3...80bc53 )
by BENARD
08:34
created

ForumContextBuilder::createFromRequest()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 19
c 2
b 0
f 0
dl 0
loc 32
rs 9.0111
cc 6
nc 3
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ForumBundle\Serializer;
6
7
use ApiPlatform\State\SerializerContextBuilderInterface;
8
use ProjetNormandie\ForumBundle\Entity\Category;
9
use ProjetNormandie\ForumBundle\Entity\Forum;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
12
13
final class ForumContextBuilder implements SerializerContextBuilderInterface
14
{
15
    private SerializerContextBuilderInterface $decorated;
16
    private AuthorizationCheckerInterface $authorizationChecker;
17
18
    public function __construct(
19
        SerializerContextBuilderInterface $decorated,
20
        AuthorizationCheckerInterface $authorizationChecker
21
    ) {
22
        $this->decorated = $decorated;
23
        $this->authorizationChecker = $authorizationChecker;
24
    }
25
26
    public function createFromRequest(Request $request, bool $normalization, ?array $extractedAttributes = null): array
27
    {
28
        $context = $this->decorated->createFromRequest($request, $normalization, $extractedAttributes);
29
        $resourceClass = $context['resource_class'] ?? null;
30
31
        // Gestion du endpoint GetHome pour Category
32
        if (
33
            $resourceClass === Category::class
34
            && str_contains($request->getRequestUri(), '/forum_category/get-home')
35
            && isset($context['groups'])
36
            && true === $normalization
37
        ) {
38
            // Groups de base pour tous (guests et users)
39
            $baseGroups = [
40
                'category:read',
41
                'category:forums',
42
                'forum:read',
43
                'forum:last-message',
44
                'message:read',
45
                'message:user',
46
                'user:read:minimal',
47
            ];
48
49
            // Si l'utilisateur est connecté, ajouter les groups spécifiques
50
            if ($this->authorizationChecker->isGranted('ROLE_USER')) {
51
                $baseGroups[] = 'forum:read-status';
52
            }
53
54
            $context['groups'] = $baseGroups;
55
        }
56
57
        return $context;
58
    }
59
}
60