TopicExtension::applyToItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 9
rs 10
c 2
b 0
f 0
cc 1
nc 1
nop 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ProjetNormandie\ForumBundle\Doctrine;
6
7
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
8
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
9
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
10
use ApiPlatform\Metadata\Operation;
11
use ProjetNormandie\ForumBundle\Entity\Topic;
12
use Doctrine\ORM\QueryBuilder;
13
use Doctrine\ORM\Query\Expr\Join;
14
use Symfony\Bundle\SecurityBundle\Security;
15
16
final class TopicExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
17
{
18
    private Security $security;
19
20
    public function __construct(Security $security)
21
    {
22
        $this->security = $security;
23
    }
24
25
    public function applyToCollection(
26
        QueryBuilder $queryBuilder,
27
        QueryNameGeneratorInterface $queryNameGenerator,
28
        string $resourceClass,
29
        ?Operation $operation = null,
30
        array $context = []
31
    ): void {
32
        $this->addWhere($queryBuilder, $resourceClass, $context);
33
    }
34
35
    public function applyToItem(
36
        QueryBuilder $queryBuilder,
37
        QueryNameGeneratorInterface $queryNameGenerator,
38
        string $resourceClass,
39
        array $identifiers,
40
        ?Operation $operation = null,
41
        array $context = []
42
    ): void {
43
        $this->addWhere($queryBuilder, $resourceClass, $context);
44
    }
45
46
    /**
47
     * @param QueryBuilder $queryBuilder
48
     * @param string $resourceClass
49
     * @param array $context
50
     */
51
    private function addWhere(QueryBuilder $queryBuilder, string $resourceClass, array $context): void
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

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

51
    private function addWhere(QueryBuilder $queryBuilder, string $resourceClass, /** @scrutinizer ignore-unused */ array $context): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        if (
54
            Topic::class !== $resourceClass ||
55
            !$this->security->isGranted('ROLE_USER') ||
56
            null === $user = $this->security->getUser()
57
        ) {
58
            return;
59
        }
60
61
        // Jointure LEFT avec TopicUserLastVisit pour récupérer les infos de lecture
62
        $queryBuilder->leftJoin(
63
            'o.userLastVisits',
64
            'tuv',
65
            Join::WITH,
66
            'tuv.user = :current_user'
67
        );
68
        $queryBuilder->addSelect('tuv');
69
        $queryBuilder->setParameter('current_user', $user);
70
    }
71
}
72