Completed
Push — master ( 1a9043...984777 )
by Valentyn
08:31
created

SearchService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 4
dl 0
loc 55
ccs 18
cts 20
cp 0.9
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A findByQuery() 0 13 2
A findByTmdbId() 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\Exception\TmdbMovieNotFoundException;
8
use App\Movies\Repository\MovieRepository;
9
10
class SearchService
11
{
12
    private $repository;
13
    private $tmdb;
14
    private $sync;
15
    private $normalizer;
16
17 7
    public function __construct(MovieRepository $repository, TmdbSearchService $tmdb, TmdbSyncService $sync, TmdbNormalizerService $normalizer)
18
    {
19 7
        $this->repository = $repository;
20 7
        $this->tmdb = $tmdb;
21 7
        $this->sync = $sync;
22 7
        $this->normalizer = $normalizer;
23 7
    }
24
25
    /**
26
     * @param string $query
27
     * @param string $locale
28
     * @return Movie[]
29
     * @throws \Exception
30
     */
31 2
    public function findByQuery(string $query, string $locale): array
32
    {
33 2
        $movies = $this->repository->findByTitle($query);
34 2
        if (reset($movies)) {
35 1
            return $movies;
36
        }
37
38 1
        $movies = $this->tmdb->findMoviesByQuery($query, $locale);
39 1
        $movies = $this->normalizer->normalizeMoviesToObjects($movies['results'], $locale);
40 1
        $this->sync->syncMovies($movies);
41
42 1
        return $movies;
43
    }
44
45
    /**
46
     * @param int $tmdb_id
47
     * @param string $locale
48
     * @return Movie|null
49
     * @throws \Exception
50
     */
51 1
    public function findByTmdbId(int $tmdb_id, string $locale): ?Movie
52
    {
53
        try {
54 1
            $movie = $this->tmdb->findMovieById($tmdb_id, $locale);
55
        } catch (TmdbMovieNotFoundException $exception) {
56
            return null;
57
        }
58
59 1
        $movies = $this->normalizer->normalizeMoviesToObjects([$movie], $locale);
60
        #$this->sync->syncMovies($movies);
0 ignored issues
show
Unused Code Comprehensibility introduced by
78% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
62 1
        return reset($movies);
63
    }
64
}