Passed
Push — master ( d4517f...26a928 )
by Yannick
08:22
created

GetStatsAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 41
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 31 6
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
declare(strict_types=1);
5
6
namespace Chamilo\CoreBundle\Controller\Api;
7
8
use Chamilo\CoreBundle\Entity\Course;
9
use Chamilo\CoreBundle\Entity\User;
10
use Chamilo\CoreBundle\Repository\Node\UserRepository;
11
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
12
use Chamilo\CoreBundle\Repository\SessionRepository;
13
use Chamilo\CoreBundle\Helpers\TrackingStatsHelper;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Attribute\AsController;
17
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
18
19
#[AsController]
20
final class GetStatsAction
21
{
22
    public function __construct(
23
        private readonly UserRepository $userRepo,
24
        private readonly CourseRepository $courseRepo,
25
        private readonly SessionRepository $sessionRepo,
26
        private readonly TrackingStatsHelper $statsHelper,
27
    ) {}
28
29
    public function __invoke(int $id, int $courseId, string $metric, Request $request): JsonResponse
30
    {
31
        /* @var User $user */
32
        $user = $this->userRepo->find($id);
33
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type Chamilo\CoreBundle\Entity\User, thus it always evaluated to true.
Loading history...
34
            throw new NotFoundHttpException('User not found.');
35
        }
36
37
        /* @var Course $course */
38
        $course = $this->courseRepo->find($courseId);
39
        if (!$course) {
0 ignored issues
show
introduced by
$course is of type Chamilo\CoreBundle\Entity\Course, thus it always evaluated to true.
Loading history...
40
            throw new NotFoundHttpException('Course not found.');
41
        }
42
43
        $session = null;
44
        $sessionId = $request->query->getInt('sessionId') ?: null;
45
        if ($sessionId) {
46
            $session = $this->sessionRepo->find($sessionId);
47
            if (!$session) {
48
                throw new NotFoundHttpException('Session not found.');
49
            }
50
        }
51
52
        $payload = match ($metric) {
53
            'avg-lp-progress'  => $this->statsHelper->getUserAvgLpProgress($user, $course, $session),
54
            'certificates'     => $this->statsHelper->getUserCertificates($user, $course, $session),
55
            'gradebook-global' => $this->statsHelper->getUserGradebookGlobal($user, $course, $session),
56
            default            => throw new NotFoundHttpException('Metric not supported.'),
57
        };
58
59
        return new JsonResponse($payload, 200);
60
    }
61
}
62