Completed
Push — master ( 9fb967...b4d909 )
by Valentyn
03:32
created

TmdbNormalizerService::addGenres()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
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 11
    public function __construct(MovieManageService $movieManageService, GenreRepository $genreRepository)
23
    {
24 11
        $this->movieManageService = $movieManageService;
25 11
        $this->genreRepository = $genreRepository;
26 11
    }
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 2
            $actorObject->setGender($actor['gender'] ?? $actorObject::GENDER_MALE);
62 2
            $photoUrl = isset($actor['profile_path']) ? self::IMAGE_HOST.$actor['profile_path'] : '';
63 2
            $actorObject->setPhoto($photoUrl);
64
65 2
            $actorTranslationObject = new ActorTranslations($actorObject, $locale, $actorObject->getOriginalName());
66 2
            $actorTranslationObject->setBiography($actor['biography'] ?? '');
67 2
            $actorObject->addTranslation($actorTranslationObject);
68
69 2
            yield $actorObject;
70
        }
71
    }
72
73
    /**
74
     * @param array $movie
75
     *
76
     * @throws \Exception
77
     *
78
     * @return MovieDTO
79
     */
80 2
    private function createMovieDTO(array $movie): MovieDTO
81
    {
82 2
        return new MovieDTO(
83 2
            $movie['original_title'],
84 2
            isset($movie['poster_path']) ? self::IMAGE_HOST.$movie['poster_path'] : '',
85 2
            $movie['imdb_id'] ?? null,
86 2
            isset($movie['budget']) ? (int) $movie['budget'] : null,
87 2
            isset($movie['runtime']) ? (int) $movie['runtime'] : null,
88 2
            isset($movie['release_date']) ? $movie['release_date'] : null
89
        );
90
    }
91
92 2
    private function createMovieTmdbDTO(array $movie): MovieTMDB
93
    {
94 2
        return new MovieTMDB(
95 2
            (int) $movie['id'],
96 2
            isset($movie['vote_average']) ? (float) $movie['vote_average'] : null,
97 2
            isset($movie['vote_count']) ? (int) $movie['vote_count'] : null
98
        );
99
    }
100
101 2
    private function createMovieTranslationDTO(string $locale, array $movie): MovieTranslationDTO
102
    {
103 2
        return new MovieTranslationDTO($locale, $movie['title'], $movie['overview'], null);
104
    }
105
106 2
    private function addGenres(Movie $movie, array $tmdbGenresIds): Movie
107
    {
108 2
        $genres = $this->genreRepository->findByTmdbIds($tmdbGenresIds);
109 2
        foreach ($genres as $genre) {
110 2
            $movie->addGenre($genre);
111
        }
112
113 2
        return $movie;
114
    }
115
116 2
    private function getGenresIds(array $movie): array
117
    {
118 2
        if (isset($movie['genres'])) {
119
            $movie['genre_ids'] = array_map(function ($genre) {
120 1
                return $genre['id'];
121 1
            }, $movie['genres']);
122
        }
123
124 2
        return $movie['genre_ids'] ?? [];
125
    }
126
127 2
    private function getMovieLocale(array $movie, string $defaultLocale)
128
    {
129 2
        return isset($movie['locale']) ? mb_substr($movie['locale'], 0, 2) : $defaultLocale; // "en-US" to "en"
130
    }
131
}
132