Passed
Push — master ( 07d19d...1b5ad8 )
by Angel Fernando Quiroz
08:33
created

LpCollectionStateProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 27
c 1
b 0
f 0
dl 0
loc 58
rs 10
wmc 11
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\State;
8
9
use ApiPlatform\Metadata\Operation;
10
use ApiPlatform\State\ProviderInterface;
11
use Chamilo\CoreBundle\Entity\Course;
12
use Chamilo\CoreBundle\Entity\Session as CoreSession;
13
use Chamilo\CoreBundle\Entity\User;
14
use Chamilo\CourseBundle\Entity\CLp;
15
use Chamilo\CourseBundle\Repository\CLpRepository;
16
use Doctrine\ORM\EntityManagerInterface;
17
use Symfony\Bundle\SecurityBundle\Security;
18
19
/**
20
 * @template-implements ProviderInterface<CLp>
21
 */
22
final readonly class LpCollectionStateProvider implements ProviderInterface
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_READONLY, expecting T_CLASS on line 22 at column 6
Loading history...
23
{
24
    public function __construct(
25
        private EntityManagerInterface $em,
26
        private CLpRepository $lpRepo,
27
        private Security $security
28
    ) {}
29
30
    public function supports(Operation $op, array $uriVariables = [], array $ctx = []): bool
31
    {
32
        return CLp::class === $op->getClass() && 'get_lp_collection_with_progress' === $op->getName();
33
    }
34
35
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
36
    {
37
        $f = $context['filters'] ?? [];
38
        $parentNodeId = (int) ($f['resourceNode.parent'] ?? 0);
39
        if ($parentNodeId <= 0) {
40
            return [];
41
        }
42
43
        $course = $this->em->createQuery(
44
            'SELECT c
45
               FROM '.Course::class.' c
46
               JOIN c.resourceNode rn
47
              WHERE rn.id = :nid'
48
        )
49
            ->setParameter('nid', $parentNodeId)
50
            ->getOneOrNullResult()
51
        ;
52
53
        if (!$course) {
54
            return [];
55
        }
56
57
        $sid = isset($f['sid']) ? (int) $f['sid'] : null;
58
        $title = $f['title'] ?? null;
59
60
        $session = $sid ? $this->em->getReference(CoreSession::class, $sid) : null;
61
62
        $lps = $this->lpRepo->findAllByCourse($course, $session, $title)
63
            ->getQuery()
64
            ->getResult()
65
        ;
66
67
        if (!$lps) {
68
            return [];
69
        }
70
71
        $user = $this->security->getUser();
72
        if ($user instanceof User) {
73
            $progress = $this->lpRepo->lastProgressForUser($lps, $user, $session);
74
            foreach ($lps as $lp) {
75
                $lp->setProgress($progress[(int) $lp->getIid()] ?? 0);
76
            }
77
        }
78
79
        return $lps;
80
    }
81
}
82