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

MovieManageService::getGenresByIds()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
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\Genres\Entity\Genre;
7
use App\Genres\Repository\GenreRepository;
8
use App\Movies\DTO\MovieDTO;
9
use App\Movies\DTO\MovieTranslationDTO;
10
use App\Movies\Entity\Movie;
11
use App\Movies\Entity\MovieTMDB;
12
use App\Movies\Entity\MovieTranslations;
13
use App\Movies\Request\CreateMovieRequest;
14
15
class MovieManageService
16
{
17
    private $genreRepository;
18
19 4
    public function __construct(GenreRepository $genreRepository)
20
    {
21 4
        $this->genreRepository = $genreRepository;
22 4
    }
23
24
    /**
25
     * @param CreateMovieRequest $request
26
     * @return Movie
27
     * @throws \Exception
28
     */
29 1
    public function createMovieByRequest(CreateMovieRequest $request): Movie
30
    {
31 1
        $movie = $request->get('movie');
32 1
        $tmdb = $movie['tmdb'];
33
        $movie['genres'] = array_map(function ($genre) { return $genre['id']; }, $movie['genres']);
34 1
        $genres = $this->getGenresByIds($movie['genres']);
35
36 1
        $translations = array_map(function (array $translation) {
37 1
            return new MovieTranslationDTO(
38 1
                $translation['locale'],
39 1
                $translation['title'],
40 1
                $translation['overview'] ?? null,
41 1
                $translation['posterUrl'] ?? null
42
            );
43 1
        }, $movie['translations']);
44
45 1
        $movieTMDB = new MovieTMDB(
46 1
            $tmdb['id'],
47 1
            $tmdb['voteAverage'] ?? null,
48 1
            $tmdb['voteCount'] ?? null
49
        );
50
51 1
        $movieDTO = new MovieDTO(
52 1
            $movie['originalTitle'],
53 1
            $movie['originalPosterUrl'],
54 1
            $movie['imdbId'] ?? null,
55 1
            $movie['budget'] ?? null,
56 1
            $movie['runtime'] ?? null,
57 1
            $movie['releaseDate'] ?? null
58
        );
59
60 1
        return $this->createMovieByDTO($movieDTO, $movieTMDB, $genres, $translations);
61
    }
62
63
    /**
64
     * @param MovieDTO $movieDTO
65
     * @param MovieTMDB $movieTMDB
66
     * @param Genre[] $genres
67
     * @param MovieTranslationDTO[] $translations
68
     * @return Movie
69
     */
70 2
    public function createMovieByDTO(MovieDTO $movieDTO, MovieTMDB $movieTMDB, array $genres, array $translations): Movie
71
    {
72 2
        $movie = new Movie($movieDTO, $movieTMDB);
73
74 2
        foreach ($genres as $genre) {
75 1
            $movie->addGenre($genre);
76
        }
77
78 2
        $addTranslation = function (MovieTranslationDTO $translation) use ($movie) {
79 2
            $movie->addTranslation(
80 2
                new MovieTranslations($movie, $translation)
81
            );
82 2
        };
83
84 2
        $movie->updateTranslations($translations, $addTranslation);
85
86 2
        return $movie;
87
    }
88
89
    /**
90
     * @param array $ids
91
     * @return Genre[]
92
     */
93 1
    private function getGenresByIds(array $ids)
94
    {
95 1
        return $this->genreRepository->findBy(['id' => $ids]);
96
    }
97
}