1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Movies\Service; |
5
|
|
|
|
6
|
|
|
use App\Genres\Repository\GenreRepository; |
7
|
|
|
use App\Movies\DTO\MovieDTO; |
8
|
|
|
use App\Movies\DTO\MovieTranslationDTO; |
9
|
|
|
use App\Movies\Entity\Movie; |
10
|
|
|
use App\Movies\Entity\MovieTMDB; |
11
|
|
|
|
12
|
|
|
class TmdbNormalizerService |
13
|
|
|
{ |
14
|
|
|
private $movieManageService; |
15
|
|
|
private $genreRepository; |
16
|
|
|
private const IMAGE_HOST = 'https://image.tmdb.org/t/p/original'; |
17
|
|
|
|
18
|
7 |
|
public function __construct(MovieManageService $movieManageService, GenreRepository $genreRepository) |
19
|
|
|
{ |
20
|
7 |
|
$this->movieManageService = $movieManageService; |
21
|
7 |
|
$this->genreRepository = $genreRepository; |
22
|
7 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $movies |
26
|
|
|
* @param string $locale |
27
|
|
|
* @throws \Exception |
28
|
|
|
* @return Movie[] |
29
|
|
|
*/ |
30
|
2 |
|
public function normalizeMoviesToObjects(array $movies, string $locale = 'en'): array |
31
|
|
|
{ |
32
|
2 |
|
$normalizedMovies = []; |
33
|
2 |
|
foreach ($movies as $movie) { |
34
|
2 |
|
$movieDTO = $this->createMovieDTO($movie); |
35
|
2 |
|
$tmdb = $this->createMovieTmdbDTO($movie); |
36
|
2 |
|
$locale = $this->getMovieLocale($movie, $locale); |
37
|
|
|
|
38
|
2 |
|
$movieObject = $this->movieManageService->createMovieByDTO($movieDTO, $tmdb, [], [ |
39
|
2 |
|
$this->createMovieTranslationDTO($locale, $movie) |
40
|
|
|
]); |
41
|
|
|
|
42
|
2 |
|
$genresIds = $this->getGenresIds($movie); |
43
|
2 |
|
$movieObject = $this->addGenres($movieObject, $genresIds); |
44
|
|
|
|
45
|
2 |
|
$normalizedMovies[] = $movieObject; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
return $normalizedMovies; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $movie |
53
|
|
|
* @return MovieDTO |
54
|
|
|
* @throws \Exception |
55
|
|
|
*/ |
56
|
2 |
|
private function createMovieDTO(array $movie): MovieDTO |
57
|
|
|
{ |
58
|
2 |
|
return new MovieDTO( |
59
|
2 |
|
$movie['original_title'], |
60
|
2 |
|
self::IMAGE_HOST . $movie['poster_path'], |
61
|
2 |
|
$movie['imdb_id'] ?? null, |
62
|
2 |
|
isset($movie['budget']) ? (int)$movie['budget'] : null, |
63
|
2 |
|
isset($movie['runtime']) ? (int)$movie['runtime'] : null, |
64
|
2 |
|
isset($movie['release_date']) ? $movie['release_date'] : null |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
private function createMovieTmdbDTO(array $movie): MovieTMDB |
69
|
|
|
{ |
70
|
2 |
|
return new MovieTMDB( |
71
|
2 |
|
(int)$movie['id'], |
72
|
2 |
|
isset($movie['vote_average']) ? (float)$movie['vote_average'] : null, |
73
|
2 |
|
isset($movie['vote_count']) ? (int)$movie['vote_count'] : null |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
private function createMovieTranslationDTO(string $locale, array $movie): MovieTranslationDTO |
78
|
|
|
{ |
79
|
2 |
|
return new MovieTranslationDTO($locale, $movie['title'], $movie['overview'], null); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
private function addGenres(Movie $movie, array $tmdbGenresIds): Movie |
83
|
|
|
{ |
84
|
2 |
|
$genres = $this->genreRepository->findByTmdbIds($tmdbGenresIds); |
85
|
2 |
|
foreach ($genres as $genre) { |
86
|
2 |
|
$movie->addGenre($genre); |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
return $movie; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
private function getGenresIds(array $movie): array |
93
|
|
|
{ |
94
|
2 |
|
if (isset($movie['genres'])) { |
95
|
1 |
|
$movie['genre_ids'] = array_map(function ($genre) { |
96
|
1 |
|
return $genre['id']; |
97
|
1 |
|
}, $movie['genres']); |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
return $movie['genre_ids'] ?? []; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
private function getMovieLocale(array $movie, string $defaultLocale) |
104
|
|
|
{ |
105
|
2 |
|
return isset($movie['locale']) ? substr($movie['locale'], 0, 2) : $defaultLocale; // "en-US" to "en" |
106
|
|
|
} |
107
|
|
|
} |