ProgressProcessor::getUserLessons()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 11
rs 10
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Serializer\Processor;
6
7
use App\Entity\User;
8
use App\Model\CourseInterface;
9
use App\Model\LessonInterface;
10
use App\Model\ModuleInterface;
11
use App\Model\UserLessonInterface;
12
use App\Repository\LessonRepositoryInterface;
13
use App\Repository\UserLessonRepositoryInterface;
14
use Symfony\Component\Security\Core\Security;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
final class ProgressProcessor
18
{
19
    private $userLessonRepository;
20
    private $lessonRepository;
21
    private $security;
22
23
    public function __construct(
24
        UserLessonRepositoryInterface $userLessonRepository,
25
        LessonRepositoryInterface $lessonRepository,
26
        Security $security
27
    ) {
28
        $this->userLessonRepository = $userLessonRepository;
29
        $this->lessonRepository = $lessonRepository;
30
        $this->security = $security;
31
    }
32
33
    public function process(object $object, array $data): array
34
    {
35
        /** @var User $user */
36
        $user = $this->security->getUser();
37
        if (null === $user || !$user->getId()) {
38
            return $data;
39
        }
40
41
        $userLessons = $this->getUserLessons($user, $object);
42
        $lessons = $this->getLessons($object);
43
44
        $completedTime = 0;
45
        /** @var UserLessonInterface $userLesson */
46
        foreach ($userLessons as $userLesson) {
47
            $completedTime += $userLesson->getLesson()->getDurationInMinutes();
48
        }
49
50
        $totalDuration = 0;
51
        /** @var LessonInterface $lesson */
52
        foreach ($lessons as $lesson) {
53
            $totalDuration += $lesson->getDurationInMinutes();
54
        }
55
56
        $data['progress'] = [
57
            'completed_lessons' => count($userLessons),
58
            'completed_percentage' => (count($userLessons) > 0 && count($lessons) > 0) ? (int) round((count($userLessons) / count($lessons)) * 100) : 0,
59
            'completed_time' => $completedTime,
60
            'total_duration' => $totalDuration,
61
        ];
62
63
        return $data;
64
    }
65
66
    private function getUserLessons(UserInterface $user, object $object): array
67
    {
68
        if ($object instanceof ModuleInterface) {
69
            return $this->userLessonRepository->getCompletedByUserInModule($user, $object);
70
        }
71
72
        if ($object instanceof CourseInterface) {
73
            return $this->userLessonRepository->getCompletedByUserInCourse($user, $object);
74
        }
75
76
        return [];
77
    }
78
79
    private function getLessons(object $object): array
80
    {
81
        if ($object instanceof ModuleInterface) {
82
            return $this->lessonRepository->getByModule($object);
83
        }
84
85
        if ($object instanceof CourseInterface) {
86
            return $this->lessonRepository->getByCourse($object);
87
        }
88
89
        return [];
90
    }
91
}
92