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

SearchService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Movies\Service;
5
6
use App\Movies\Entity\Movie;
7
use App\Movies\Repository\MovieRepository;
8
9
class SearchService
10
{
11
    private $repository;
12
    private $tmdb;
13
    private $sync;
14
    private $normalizer;
15
16 2
    public function __construct(MovieRepository $repository, TmdbSearchService $tmdb, TmdbSyncService $sync, TmdbNormalizerService $normalizer)
17
    {
18 2
        $this->repository = $repository;
19 2
        $this->tmdb = $tmdb;
20 2
        $this->sync = $sync;
21 2
        $this->normalizer = $normalizer;
22 2
    }
23
24
    /**
25
     * @param string $query
26
     * @param string $locale
27
     * @return Movie[]
28
     * @throws \Exception
29
     */
30 2
    public function findByQuery(string $query, string $locale): array
31
    {
32 2
        $movies = $this->repository->findByTitle($query);
33 2
        if (reset($movies)) {
34 1
            return $movies;
35
        }
36
37 1
        $movies = $this->tmdb->findMoviesByQuery($query, $locale);
38 1
        $movies = $this->normalizer->normalizeMoviesToObjects($movies['results'], $locale);
39 1
        $this->sync->syncMovies($movies);
40
41 1
        return $movies;
42
    }
43
}