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

SearchService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 4
dl 0
loc 34
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A findByQuery() 0 13 2
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
    public function __construct(MovieRepository $repository, TmdbSearchService $tmdb, TmdbSyncService $sync, TmdbNormalizerService $normalizer)
17
    {
18
        $this->repository = $repository;
19
        $this->tmdb = $tmdb;
20
        $this->sync = $sync;
21
        $this->normalizer = $normalizer;
22
    }
23
24
    /**
25
     * @param string $query
26
     * @param $locale
27
     * @return Movie[]|array
28
     */
29
    public function findByQuery(string $query, $locale): array
30
    {
31
        $movies = $this->repository->search($query);
32
        if (reset($movies)) {
33
            return $movies;
34
        }
35
36
        $movies = $this->tmdb->findMoviesByQuery($query, $locale);
37
        $movies = $this->normalizer->normalizeMoviesToObjects($movies['results'], $locale);
38
        $this->sync->syncMovies($movies);
39
40
        return $movies;
41
    }
42
}