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

TmdbNormalizerService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 45
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B normalizeMoviesToObjects() 0 29 5
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
}