Completed
Push — master ( d15cf4...5163a0 )
by Valentyn
06:05
created

TmdbNormalizerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Service;
5
6
use App\Movies\Entity\Movie;
7
8
class TmdbNormalizerService
9
{
10
    private $movieManageService;
11
    private const IMAGE_HOST = 'https://image.tmdb.org/t/p/original';
12
13
    public function __construct(MovieManageService $movieManageService)
14
    {
15
        $this->movieManageService = $movieManageService;
16
    }
17
18
    /**
19
     * @param array $movies
20
     * @param string $locale
21
     * @return Movie[]
22
     */
23
    public function normalizeMoviesToObjects(array $movies, string $locale = 'en'): array
24
    {
25
        $normalizedMovies = [];
26
        foreach ($movies as $movie) {
27
            $movieArray = [
28
                'originalTitle' => $movie['original_title'],
29
                'originalPosterUrl' => self::IMAGE_HOST . $movie['poster_path'],
30
            ];
31
            if (isset($movie['release_date'])) $movieArray['releaseDate'] = $movie['release_date'];
32
33
            $tmdbArray = [
34
                'id' => $movie['id']
35
            ];
36
            if (isset($movie['vote_average'])) $tmdbArray['voteAverage'] = $movie['vote_average'];
37
            if (isset($movie['vote_count'])) $tmdbArray['voteCount'] = $movie['vote_count'];
38
39
            $translation = [
40
                'locale' => $locale,
41
                'title' => $movie['title'],
42
                'posterUrl' => $movieArray['originalPosterUrl'],
43
                'overview' => $movie['overview']
44
            ];
45
46
            $movieObject = $this->movieManageService->createMovie($movieArray, $tmdbArray, [], [$translation]);
47
            $normalizedMovies[] = $movieObject;
48
        }
49
50
        return $normalizedMovies;
51
    }
52
}