1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Movies\Service; |
6
|
|
|
|
7
|
|
|
use App\Genres\Entity\Genre; |
8
|
|
|
use App\Genres\Repository\GenreRepository; |
9
|
|
|
use App\Movies\DTO\MovieDTO; |
10
|
|
|
use App\Movies\DTO\MovieTranslationDTO; |
11
|
|
|
use App\Movies\Entity\Movie; |
12
|
|
|
use App\Movies\Entity\MovieTMDB; |
13
|
|
|
use App\Movies\Entity\MovieTranslations; |
14
|
|
|
use App\Movies\Request\CreateMovieRequest; |
15
|
|
|
|
16
|
|
|
class MovieManageService |
17
|
|
|
{ |
18
|
|
|
private $genreRepository; |
19
|
|
|
|
20
|
13 |
|
public function __construct(GenreRepository $genreRepository) |
21
|
|
|
{ |
22
|
13 |
|
$this->genreRepository = $genreRepository; |
23
|
13 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param CreateMovieRequest $request |
27
|
|
|
* |
28
|
|
|
* @throws \Exception |
29
|
|
|
* |
30
|
|
|
* @return Movie |
31
|
|
|
*/ |
32
|
1 |
|
public function createMovieByRequest(CreateMovieRequest $request): Movie |
33
|
|
|
{ |
34
|
1 |
|
$movie = $request->get('movie'); |
35
|
1 |
|
$tmdb = $movie['tmdb']; |
36
|
|
|
$movie['genres'] = array_map(function ($genre) { return $genre['id']; }, $movie['genres']); |
37
|
1 |
|
$genres = $this->getGenresByIds($movie['genres']); |
38
|
|
|
|
39
|
|
|
$translations = array_map(function (array $translation) { |
40
|
1 |
|
return new MovieTranslationDTO( |
41
|
1 |
|
$translation['locale'], |
42
|
1 |
|
$translation['title'], |
43
|
1 |
|
$translation['overview'] ?? null, |
44
|
1 |
|
$translation['posterUrl'] ?? null |
45
|
|
|
); |
46
|
1 |
|
}, $movie['translations']); |
47
|
|
|
|
48
|
1 |
|
$movieTMDB = new MovieTMDB( |
49
|
1 |
|
$tmdb['id'], |
50
|
1 |
|
$tmdb['voteAverage'] ?? null, |
51
|
1 |
|
$tmdb['voteCount'] ?? null |
52
|
|
|
); |
53
|
|
|
|
54
|
1 |
|
$movieDTO = new MovieDTO( |
55
|
1 |
|
$movie['originalTitle'], |
56
|
1 |
|
$movie['originalPosterUrl'], |
57
|
1 |
|
$movie['imdbId'] ?? null, |
58
|
1 |
|
$movie['budget'] ?? null, |
59
|
1 |
|
$movie['runtime'] ?? null, |
60
|
1 |
|
$movie['releaseDate'] ?? null |
61
|
|
|
); |
62
|
|
|
|
63
|
1 |
|
return $this->createMovieByDTO($movieDTO, $movieTMDB, $genres, $translations); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param MovieDTO $movieDTO |
68
|
|
|
* @param MovieTMDB $movieTMDB |
69
|
|
|
* @param Genre[] $genres |
70
|
|
|
* @param MovieTranslationDTO[] $translations |
71
|
|
|
* |
72
|
|
|
* @throws \ErrorException |
73
|
|
|
* |
74
|
|
|
* @return Movie |
75
|
|
|
*/ |
76
|
3 |
|
public function createMovieByDTO(MovieDTO $movieDTO, MovieTMDB $movieTMDB, array $genres, array $translations): Movie |
77
|
|
|
{ |
78
|
3 |
|
$movie = new Movie($movieDTO, $movieTMDB); |
79
|
|
|
|
80
|
3 |
|
foreach ($genres as $genre) { |
81
|
1 |
|
$movie->addGenre($genre); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$addTranslation = function (MovieTranslationDTO $translation) use ($movie) { |
85
|
3 |
|
$movie->addTranslation( |
86
|
3 |
|
new MovieTranslations($movie, $translation) |
87
|
|
|
); |
88
|
3 |
|
}; |
89
|
|
|
|
90
|
3 |
|
$movie->updateTranslations($translations, $addTranslation); |
91
|
|
|
|
92
|
3 |
|
return $movie; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param array $ids |
97
|
|
|
* |
98
|
|
|
* @return Genre[] |
99
|
|
|
*/ |
100
|
1 |
|
private function getGenresByIds(array $ids) |
101
|
|
|
{ |
102
|
1 |
|
return $this->genreRepository->findBy(['id' => $ids]); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|