Completed
Push — master ( 5163a0...712d21 )
by Valentyn
06:59
created

TmdbNormalizerService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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