Completed
Push — master ( 59ed7d...9fb967 )
by Valentyn
02:51
created

TmdbNormalizerService::normalizeActorsToObjects()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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