|
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 = new MovieDTO( |
|
35
|
1 |
|
$movie['original_title'], |
|
36
|
1 |
|
self::IMAGE_HOST . $movie['poster_path'], |
|
37
|
1 |
|
null, |
|
38
|
1 |
|
null, |
|
39
|
1 |
|
null, |
|
40
|
1 |
|
$movie['release_date'] ?? null |
|
41
|
|
|
); |
|
42
|
1 |
|
$tmdb = new MovieTMDB((int)$movie['id'], null, null); |
|
43
|
1 |
|
$locale = isset($movie['locale']) ? substr($movie['locale'], 0, 2) : $locale; // "en-US" to "en" |
|
44
|
|
|
|
|
45
|
1 |
|
$movieObject = $this->movieManageService->createMovieByDTO($movieDTO, $tmdb, [], [ |
|
46
|
1 |
|
new MovieTranslationDTO($locale, $movie['title'], $movie['overview'], null) |
|
47
|
|
|
]); |
|
48
|
|
|
|
|
49
|
1 |
|
$genres = $this->genreRepository->findByTmdbIds($movie['genre_ids']); |
|
50
|
1 |
|
foreach ($genres as $genre) { |
|
51
|
1 |
|
$movieObject->addGenre($genre); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
$normalizedMovies[] = $movieObject; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return $normalizedMovies; |
|
58
|
|
|
} |
|
59
|
|
|
} |