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
|
2 |
|
public function __construct(MovieManageService $movieManageService, GenreRepository $genreRepository) |
19
|
|
|
{ |
20
|
2 |
|
$this->movieManageService = $movieManageService; |
21
|
2 |
|
$this->genreRepository = $genreRepository; |
22
|
2 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $movies |
26
|
|
|
* @param string $locale |
27
|
|
|
* @throws \Exception |
28
|
|
|
* @return Movie[] |
29
|
|
|
*/ |
30
|
1 |
|
public function normalizeMoviesToObjects(array $movies, string $locale = 'en'): array |
31
|
|
|
{ |
32
|
1 |
|
$normalizedMovies = []; |
33
|
1 |
|
foreach ($movies as $movie) { |
34
|
1 |
|
$movieDTO = $this->createMovieDTO($movie); |
35
|
1 |
|
$tmdb = new MovieTMDB((int)$movie['id'], null, null); |
36
|
1 |
|
$locale = $this->getMovieLocale($movie, $locale); |
37
|
|
|
|
38
|
1 |
|
$movieObject = $this->movieManageService->createMovieByDTO($movieDTO, $tmdb, [], [ |
39
|
1 |
|
$this->createMovieTranslation($locale, $movie) |
40
|
|
|
]); |
41
|
1 |
|
$movieObject = $this->addGenres($movieObject, $movie['genre_ids']); |
42
|
|
|
|
43
|
1 |
|
$normalizedMovies[] = $movieObject; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
return $normalizedMovies; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $movie |
51
|
|
|
* @return MovieDTO |
52
|
|
|
* @throws \Exception |
53
|
|
|
*/ |
54
|
1 |
|
private function createMovieDTO(array $movie): MovieDTO |
55
|
|
|
{ |
56
|
1 |
|
return new MovieDTO( |
57
|
1 |
|
$movie['original_title'], |
58
|
1 |
|
self::IMAGE_HOST . $movie['poster_path'], |
59
|
1 |
|
null, |
60
|
1 |
|
null, |
61
|
1 |
|
null, |
62
|
1 |
|
$movie['release_date'] ?? null |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
private function createMovieTranslation(string $locale, array $movie) |
67
|
|
|
{ |
68
|
1 |
|
return new MovieTranslationDTO($locale, $movie['title'], $movie['overview'], null); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
private function addGenres(Movie $movie, array $tmdbGenresIds): Movie |
72
|
|
|
{ |
73
|
1 |
|
$genres = $this->genreRepository->findByTmdbIds($tmdbGenresIds); |
74
|
1 |
|
foreach ($genres as $genre) { |
75
|
1 |
|
$movie->addGenre($genre); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $movie; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
private function getMovieLocale(array $movie, string $defaultLocale) |
82
|
|
|
{ |
83
|
1 |
|
return isset($movie['locale']) ? substr($movie['locale'], 0, 2) : $defaultLocale; // "en-US" to "en" |
84
|
|
|
} |
85
|
|
|
} |