Completed
Push — master ( b15cae...b2cd25 )
by Paweł
19s queued 11s
created

ProgressProcessor::getUserLessons()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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