Completed
Push — master ( 1302da...3a2ccf )
by Valentyn
03:07
created

SearchService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 73.32%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 76
ccs 22
cts 30
cp 0.7332
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B findByQuery() 0 32 6
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\Pagination\MovieCollection;
9
use App\Movies\Repository\MovieRepository;
10
use App\Pagination\PaginatedCollection;
11
use App\Pagination\PaginatedCollectionInterface;
12
13
class SearchService
14
{
15
    private $repository;
16
    private $tmdb;
17
    private $sync;
18
    private $normalizer;
19
20 7
    public function __construct(MovieRepository $repository, TmdbSearchService $tmdb, TmdbSyncService $sync, TmdbNormalizerService $normalizer)
21
    {
22 7
        $this->repository = $repository;
23 7
        $this->tmdb = $tmdb;
24 7
        $this->sync = $sync;
25 7
        $this->normalizer = $normalizer;
26 7
    }
27
28
    /**
29
     * @param string $query
30
     * @param string $locale
31
     * @param int $offset
32
     * @param int|null $limit
33
     * @return PaginatedCollectionInterface
34
     * @throws \Exception
35
     */
36 2
    public function findByQuery(string $query, string $locale, int $offset = 0, ?int $limit = null): PaginatedCollectionInterface
37
    {
38 2
        $moviesQuery = $this->repository->findByTitleQuery($query);
39 2
        $movies = new PaginatedCollection($moviesQuery, $offset, $limit);
40 2
        if ($movies->getTotal() > 0) {
41 1
            return $movies;
42
        }
43
44 1
        $movies = $this->tmdb->findMoviesByQuery($query, $locale);
45 1
        $totalResults = (int)$movies['total_results'];
46
47 1
        if ($totalResults === 0) {
48
            return new MovieCollection([], 0, $offset);
49
        }
50
51
        // If we have a lot of movies then save it all
52 1
        if (isset($movies['total_pages']) && $movies['total_pages'] > 1) {
53
            // $i = 2 because $movies currently already has movies from page 1
54
            for ($i = 2; $i <= $movies['total_pages']; $i++) {
55
                $moviesOnPage = $this->tmdb->findMoviesByQuery($query, $locale, [
56
                    'page' => $i,
57
                ]);
58
                $moviesObjectsOnPage = $this->normalizer->normalizeMoviesToObjects($moviesOnPage['results'], $locale);
59
                $this->sync->syncMovies($moviesObjectsOnPage);
60
            }
61
        }
62
63 1
        $movies = $this->normalizer->normalizeMoviesToObjects($movies['results'], $locale);
64 1
        $this->sync->syncMovies($movies);
65
66 1
        return new MovieCollection($movies, $totalResults, $offset);
67
    }
68
69
    /**
70
     * @param int $tmdb_id
71
     * @param string $locale
72
     * @return Movie|null
73
     * @throws \Exception
74
     */
75 1
    public function findByTmdbId(int $tmdb_id, string $locale): ?Movie
76
    {
77
        try {
78 1
            $movie = $this->tmdb->findMovieById($tmdb_id, $locale);
79
        } catch (TmdbMovieNotFoundException $exception) {
80
            return null;
81
        }
82
83 1
        $movies = $this->normalizer->normalizeMoviesToObjects([$movie], $locale);
84
        #$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...
85
86 1
        return reset($movies);
87
    }
88
}