|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Movies\Service; |
|
6
|
|
|
|
|
7
|
|
|
use App\Actors\Entity\Actor; |
|
8
|
|
|
use App\Actors\Entity\ActorTMDB; |
|
9
|
|
|
use App\Actors\Entity\ActorTranslations; |
|
10
|
|
|
use App\Genres\Repository\GenreRepository; |
|
11
|
|
|
use App\Movies\DTO\MovieDTO; |
|
12
|
|
|
use App\Movies\DTO\MovieTranslationDTO; |
|
13
|
|
|
use App\Movies\Entity\Movie; |
|
14
|
|
|
use App\Movies\Entity\MovieTMDB; |
|
15
|
|
|
|
|
16
|
|
|
class TmdbNormalizerService |
|
17
|
|
|
{ |
|
18
|
|
|
private $movieManageService; |
|
19
|
|
|
private $genreRepository; |
|
20
|
|
|
private const IMAGE_HOST = 'https://image.tmdb.org/t/p/original'; |
|
21
|
|
|
|
|
22
|
13 |
|
public function __construct(MovieManageService $movieManageService, GenreRepository $genreRepository) |
|
23
|
|
|
{ |
|
24
|
13 |
|
$this->movieManageService = $movieManageService; |
|
25
|
13 |
|
$this->genreRepository = $genreRepository; |
|
26
|
13 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param array $movies |
|
30
|
|
|
* @param string $locale |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \ErrorException |
|
33
|
|
|
* |
|
34
|
|
|
* @return \Iterator |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function normalizeMoviesToObjects(array $movies, string $locale = 'en'): \Iterator |
|
37
|
|
|
{ |
|
38
|
2 |
|
foreach ($movies as $movie) { |
|
39
|
2 |
|
$movieDTO = $this->createMovieDTO($movie); |
|
40
|
2 |
|
$tmdb = $this->createMovieTmdbDTO($movie); |
|
41
|
2 |
|
$locale = $this->getMovieLocale($movie, $locale); |
|
42
|
|
|
|
|
43
|
2 |
|
$movieObject = $this->movieManageService->createMovieByDTO($movieDTO, $tmdb, [], [ |
|
44
|
2 |
|
$this->createMovieTranslationDTO($locale, $movie), |
|
45
|
|
|
]); |
|
46
|
|
|
|
|
47
|
2 |
|
$genresIds = $this->getGenresIds($movie); |
|
48
|
2 |
|
$movieObject = $this->addGenres($movieObject, $genresIds); |
|
49
|
|
|
|
|
50
|
2 |
|
yield $movieObject; |
|
51
|
|
|
} |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
public function normalizeActorsToObjects(array $actors, string $locale = 'en'): \Iterator |
|
55
|
|
|
{ |
|
56
|
2 |
|
foreach ($actors as $actor) { |
|
57
|
2 |
|
$actorTmdbObject = new ActorTMDB($actor['id']); |
|
58
|
2 |
|
$actorObject = new Actor($actor['name'], $actorTmdbObject); |
|
59
|
2 |
|
$actorObject->setImdbId($actor['imdb_id'] ?? ''); |
|
60
|
2 |
|
$actorObject->setBirthday(new \DateTimeImmutable($actor['birthday'] ?? '')); |
|
61
|
|
|
|
|
62
|
2 |
|
$gender = $actor['gender'] ?? 0; |
|
63
|
2 |
|
if (\in_array($gender, [$actorObject::GENDER_FEMALE, $actorObject::GENDER_MALE], true)) { |
|
64
|
1 |
|
$actorObject->setGender($gender); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$photoUrl = isset($actor['profile_path']) ? self::IMAGE_HOST.$actor['profile_path'] : ''; |
|
68
|
2 |
|
$actorObject->setPhoto($photoUrl); |
|
69
|
|
|
|
|
70
|
2 |
|
$actorTranslationObject = new ActorTranslations($actorObject, $locale, $actorObject->getOriginalName()); |
|
71
|
2 |
|
$actorTranslationObject->setBiography($actor['biography'] ?? ''); |
|
72
|
2 |
|
$actorObject->addTranslation($actorTranslationObject); |
|
73
|
|
|
|
|
74
|
2 |
|
yield $actorObject; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param array $movie |
|
80
|
|
|
* |
|
81
|
|
|
* @throws \Exception |
|
82
|
|
|
* |
|
83
|
|
|
* @return MovieDTO |
|
84
|
|
|
*/ |
|
85
|
2 |
|
private function createMovieDTO(array $movie): MovieDTO |
|
86
|
|
|
{ |
|
87
|
2 |
|
return new MovieDTO( |
|
88
|
2 |
|
$movie['original_title'], |
|
89
|
2 |
|
isset($movie['poster_path']) ? self::IMAGE_HOST.$movie['poster_path'] : '', |
|
90
|
2 |
|
$movie['imdb_id'] ?? null, |
|
91
|
2 |
|
isset($movie['budget']) ? (int) $movie['budget'] : null, |
|
92
|
2 |
|
isset($movie['runtime']) ? (int) $movie['runtime'] : null, |
|
93
|
2 |
|
isset($movie['release_date']) ? $movie['release_date'] : null |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
private function createMovieTmdbDTO(array $movie): MovieTMDB |
|
98
|
|
|
{ |
|
99
|
2 |
|
return new MovieTMDB( |
|
100
|
2 |
|
(int) $movie['id'], |
|
101
|
2 |
|
isset($movie['vote_average']) ? (float) $movie['vote_average'] : null, |
|
102
|
2 |
|
isset($movie['vote_count']) ? (int) $movie['vote_count'] : null |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
private function createMovieTranslationDTO(string $locale, array $movie): MovieTranslationDTO |
|
107
|
|
|
{ |
|
108
|
2 |
|
return new MovieTranslationDTO($locale, $movie['title'], $movie['overview'], null); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
private function addGenres(Movie $movie, array $tmdbGenresIds): Movie |
|
112
|
|
|
{ |
|
113
|
2 |
|
$genres = $this->genreRepository->findByTmdbIds($tmdbGenresIds); |
|
114
|
2 |
|
foreach ($genres as $genre) { |
|
115
|
2 |
|
$movie->addGenre($genre); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
2 |
|
return $movie; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
2 |
|
private function getGenresIds(array $movie): array |
|
122
|
|
|
{ |
|
123
|
2 |
|
if (isset($movie['genres'])) { |
|
124
|
|
|
$movie['genre_ids'] = array_map(function ($genre) { |
|
125
|
1 |
|
return $genre['id']; |
|
126
|
1 |
|
}, $movie['genres']); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
return $movie['genre_ids'] ?? []; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
private function getMovieLocale(array $movie, string $defaultLocale) |
|
133
|
|
|
{ |
|
134
|
2 |
|
return isset($movie['locale']) ? mb_substr($movie['locale'], 0, 2) : $defaultLocale; // "en-US" to "en" |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|