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

MovieManageService::createMovie()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 18
cts 18
cp 1
rs 4.6666
c 0
b 0
f 0
cc 8
eloc 18
nc 128
nop 4
crap 8
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Service;
5
6
use App\Genres\Repository\GenreRepository;
7
use App\Movies\Entity\Movie;
8
use App\Movies\Entity\MovieTMDB;
9
use App\Movies\Entity\MovieTranslations;
10
use App\Movies\Request\CreateMovieRequest;
11
12
class MovieManageService
13
{
14
    private $genreRepository;
15
16 2
    public function __construct(GenreRepository $genreRepository)
17
    {
18 2
        $this->genreRepository = $genreRepository;
19 2
    }
20
21 1
    public function createMovieByRequest(CreateMovieRequest $request): Movie
22
    {
23 1
        $movie = $request->get('movie');
24 1
        return $this->createMovie($movie, $movie['tmdb'], $movie['genres'], $movie['translations']);
25
    }
26
27 1
    public function createMovie(array $movieArray, array $tmdbArray, array $genres, array $translations): Movie
28
    {
29 1
        $tmdb = new MovieTMDB($tmdbArray['id']);
30 1
        if (isset($tmdbArray['voteAverage'])) $tmdb->setVoteAverage($tmdbArray['voteAverage']);
31 1
        if (isset($tmdbArray['voteCount'])) $tmdb->setVoteCount($tmdbArray['voteCount']);
32
33 1
        $movie = new Movie($movieArray['originalTitle'], $movieArray['originalPosterUrl'], $tmdb);
34 1
        if (isset($movieArray['imdbId'])) $movie->setImdbId($movieArray['imdbId']);
35 1
        if (isset($movieArray['budget'])) $movie->setBudget($movieArray['budget']);
36 1
        if (isset($movieArray['runtime'])) $movie->setRuntime($movieArray['runtime']);
37 1
        if (isset($movieArray['releaseDate'])) $movie->setReleaseDate(new \DateTimeImmutable($movieArray['releaseDate']));
38
39 1
        $genres = $this->genreRepository->findBy([
40
            'id' => array_map(function ($genre) { return $genre['id']; }, $genres)
41
        ]);
42 1
        foreach ($genres as $genre) {
43 1
            $movie->addGenre($genre);
44
        }
45
46 1
        $addTranslation = function ($translation) use ($movie) {
47 1
            $movie->addTranslation(
48 1
                new MovieTranslations($movie, $translation['locale'], $translation['title'], $translation['posterUrl'], $translation['overview'])
49
            );
50 1
        };
51
52 1
        $movie->updateTranslations($translations, $addTranslation);
53
54 1
        return $movie;
55
    }
56
}