Completed
Push — master ( 14d075...b15cae )
by Paweł
12s
created

ModuleProgressProcessor::process()   A

Complexity

Conditions 6
Paths 17

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 17
nop 2
dl 0
loc 30
rs 9.0777
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\LessonInterface;
8
use App\Model\ModuleInterface;
9
use App\Model\UserLessonInterface;
10
use App\Repository\LessonRepositoryInterface;
11
use App\Repository\UserLessonRepositoryInterface;
12
use Symfony\Component\Security\Core\Security;
13
14
final class ModuleProgressProcessor
15
{
16
    private $userLessonRepository;
17
    private $lessonRepository;
18
    private $security;
19
20
    public function __construct(
21
        UserLessonRepositoryInterface $userLessonRepository,
22
        LessonRepositoryInterface $lessonRepository,
23
        Security $security
24
    ) {
25
        $this->userLessonRepository = $userLessonRepository;
26
        $this->lessonRepository = $lessonRepository;
27
        $this->security = $security;
28
    }
29
30
    public function process(ModuleInterface $object, array $data): array
31
    {
32
        $user = $this->security->getUser();
33
        if (null === $user) {
34
            return $data;
35
        }
36
37
        $userLessons = $this->userLessonRepository->getCompletedByUserAndModule($user, $object);
38
        $lessons = $this->lessonRepository->getByModule($object);
39
40
        $completedTime = 0;
41
        /** @var UserLessonInterface $userLesson */
42
        foreach ($userLessons as $userLesson) {
43
            $completedTime += $userLesson->getLesson()->getDurationInMinutes();
44
        }
45
46
        $totalDuration = 0;
47
        /** @var LessonInterface $lesson */
48
        foreach ($lessons as $lesson) {
49
            $totalDuration += $lesson->getDurationInMinutes();
50
        }
51
52
        $data['progress'] = [
53
            'completed_lessons' => count($userLessons),
54
            'completed_percentage' => (count($userLessons) > 0 && count($lessons) > 0) ? floor((count($userLessons) / count($lessons)) * 100) : 0,
55
            'completed_time' => $completedTime,
56
            'total_duration' => $totalDuration,
57
        ];
58
59
        return $data;
60
    }
61
}
62