Test Failed
Push — develop ( 7c3d28...e1debf )
by BENARD
12:55 queued 06:18
created

GetRelatedVideosDebug   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getGenreNames() 0 12 3
A getPlatformNames() 0 12 3
A __construct() 0 3 1
A __invoke() 0 32 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VideoGamesRecords\CoreBundle\Controller\Video;
6
7
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpKernel\Attribute\AsController;
11
use VideoGamesRecords\CoreBundle\Entity\Video;
12
use VideoGamesRecords\CoreBundle\Service\VideoRecommendationService;
13
14
#[AsController]
15
class GetRelatedVideosDebug extends AbstractController
16
{
17
    public function __construct(
18
        private readonly VideoRecommendationService $videoRecommendationService
19
    ) {
20
    }
21
22
    public function __invoke(Video $data, Request $request): JsonResponse
23
    {
24
        $limit = min(20, max(1, (int) $request->query->get('limit', 10)));
25
26
        $scoredRecommendations = $this->videoRecommendationService->getRecommendationsWithScores($data, $limit);
27
28
        $response = [
29
            'video_id' => $data->getId(),
30
            'source_video' => [
31
                'title' => $data->getTitle(),
32
                'game' => $data->getGame()?->getLibGameEn(),
33
                'series' => $data->getGame()?->getSerie()?->getLibSerie(),
34
                'genres' => $this->getGenreNames($data),
35
                'platforms' => $this->getPlatformNames($data),
36
            ],
37
            'scored_recommendations' => array_map(function ($item) {
38
                return [
39
                    'video' => [
40
                        'id' => $item['video']->getId(),
41
                        'title' => $item['video']->getTitle(),
42
                        'game' => $item['video']->getGame()?->getLibGameEn(),
43
                        'series' => $item['video']->getGame()?->getSerie()?->getLibSerie(),
44
                        'view_count' => $item['video']->getViewCount(),
45
                        'like_count' => $item['video']->getLikeCount(),
46
                    ],
47
                    'score' => $item['score'],
48
                    'score_breakdown' => $item['debug']
49
                ];
50
            }, array_slice($scoredRecommendations, 0, $limit))
51
        ];
52
53
        return new JsonResponse($response);
54
    }
55
56
    private function getGenreNames(Video $video): array
57
    {
58
        $genres = $video->getGame()?->getIgdbGame()?->getGenres();
59
        if (!$genres) {
0 ignored issues
show
introduced by
$genres is of type Doctrine\Common\Collections\Collection, thus it always evaluated to true.
Loading history...
60
            return [];
61
        }
62
63
        $names = [];
64
        foreach ($genres as $genre) {
65
            $names[] = $genre->getName();
66
        }
67
        return $names;
68
    }
69
70
    private function getPlatformNames(Video $video): array
71
    {
72
        $platforms = $video->getGame()?->getPlatforms();
73
        if (!$platforms) {
0 ignored issues
show
introduced by
$platforms is of type Doctrine\Common\Collections\Collection, thus it always evaluated to true.
Loading history...
74
            return [];
75
        }
76
77
        $names = [];
78
        foreach ($platforms as $platform) {
79
            $names[] = $platform->getName();
80
        }
81
        return $names;
82
    }
83
}
84