Completed
Push — master ( 97cb5e...1302da )
by Valentyn
02:35
created

SearchService::findByTmdbId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.1481

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 4
cts 6
cp 0.6667
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2.1481
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
40 1
        if (!reset($movies['results'])) {
41
            return [];
42
        }
43
44 1
        $movies = $this->normalizer->normalizeMoviesToObjects($movies['results'], $locale);
45 1
        $this->sync->syncMovies($movies);
46
47 1
        return $movies;
48
    }
49
50
    /**
51
     * @param int $tmdb_id
52
     * @param string $locale
53
     * @return Movie|null
54
     * @throws \Exception
55
     */
56 1
    public function findByTmdbId(int $tmdb_id, string $locale): ?Movie
57
    {
58
        try {
59 1
            $movie = $this->tmdb->findMovieById($tmdb_id, $locale);
60
        } catch (TmdbMovieNotFoundException $exception) {
61
            return null;
62
        }
63
64 1
        $movies = $this->normalizer->normalizeMoviesToObjects([$movie], $locale);
65
        #$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...
66
67 1
        return reset($movies);
68
    }
69
}