Passed
Push — master ( 8f9852...5f77c6 )
by Julito
10:43
created

CCalendarEventExtension::applyToItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 6
dl 0
loc 2
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\DataProvider\Extension;
8
9
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
10
//use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
11
//use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
12
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
13
use Chamilo\CoreBundle\Entity\Message;
14
use Chamilo\CoreBundle\Entity\User;
15
use Chamilo\CourseBundle\Entity\CCalendarEvent;
16
use Doctrine\ORM\Query\Expr\Join;
17
use Doctrine\ORM\QueryBuilder;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
use Symfony\Component\Security\Core\Security;
20
21
final class CCalendarEventExtension implements QueryCollectionExtensionInterface //, QueryItemExtensionInterface
22
{
23
    private Security $security;
24
25
    public function __construct(Security $security)
26
    {
27
        $this->security = $security;
28
    }
29
30
    public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null): void
31
    {
32
        /*if ($this->security->isGranted('ROLE_ADMIN')) {
33
            return;
34
        }*/
35
        /*
36
        if ('collection_query' === $operationName) {
37
            if (null === $user = $this->security->getUser()) {
38
                throw new AccessDeniedException('Access Denied.');
39
            }
40
41
            $rootAlias = $queryBuilder->getRootAliases()[0];
42
            $queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias));
43
            $queryBuilder->setParameter('current_user', $user);
44
        }*/
45
46
        $this->addWhere($queryBuilder, $resourceClass);
47
    }
48
49
    public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void
50
    {
51
        //error_log('applyToItem1');
52
        //$this->addWhere($queryBuilder, $resourceClass);
53
    }
54
55
    private function addWhere(QueryBuilder $qb, string $resourceClass): void
56
    {
57
        if (CCalendarEvent::class !== $resourceClass) {
58
            return;
59
        }
60
61
        /*if ($this->security->isGranted('ROLE_ADMIN')) {
62
            return;
63
        }*/
64
65
        /** @var User $user */
66
        $user = $this->security->getUser();
67
        $alias = $qb->getRootAliases()[0];
68
69
        $qb
70
            ->innerJoin("$alias.resourceNode", 'node')
71
            ->leftJoin('node.resourceLinks', 'links')
72
        ;
73
74
        $qb
75
            ->andWhere(
76
                '
77
                links.user = :user OR node.creator = :user
78
                '
79
            )
80
            ->setParameter('user', $user)
81
        ;
82
83
        //$qb->leftJoin("$alias.receivers", 'r');
84
        //$qb->leftJoin("$alias.receivers", 'r', Join::WITH, "r.receiver = :current OR $alias.sender = :current ");
85
        //$qb->leftJoin("$alias.receivers", 'r');
86
        /*$qb->andWhere(
87
            $qb->expr()->orX(
88
                $qb->andWhere(
89
                    $qb->expr()->eq("$alias.sender", $user->getId()),
90
                    $qb->expr()->eq("$alias.msgType", Message::MESSAGE_TYPE_OUTBOX)
91
                ),
92
                $qb->andWhere(
93
                    $qb->expr()->in("r", $user->getId()),
94
                    $qb->expr()->eq("$alias.msgType", Message::MESSAGE_TYPE_INBOX)
95
                )
96
            ),
97
        );*/
98
    }
99
}
100