Completed
Push — master ( 2b96a9...428378 )
by Valentyn
04:33
created

TmdbSyncService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 39
ccs 14
cts 16
cp 0.875
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A syncMovies() 0 17 3
A isSupport() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Service;
6
7
use App\Movies\Entity\Movie;
8
use App\Movies\EventListener\MovieSyncProcessor;
9
use App\Movies\Repository\MovieRepository;
10
use Enqueue\Client\Message;
11
use Enqueue\Client\ProducerInterface;
12
13
class TmdbSyncService
14
{
15
    private $repository;
16
    private $producer;
17
18 9
    public function __construct(MovieRepository $repository, ProducerInterface $producer)
19
    {
20 9
        $this->repository = $repository;
21 9
        $this->producer = $producer;
22 9
    }
23
24
    /**
25
     * @param array|Movie[] $movies
26
     * @param bool          $loadSimilar
27
     * @param array         $similarMoviesTable
28
     */
29 1
    public function syncMovies(array $movies, bool $loadSimilar = true, array $similarMoviesTable = []): void
30
    {
31 1
        if (!count($movies)) {
32
            return;
33
        }
34
35 1
        if ($this->isSupport(reset($movies)) === false) {
36
            throw new \InvalidArgumentException('Unsupported array of movies provided');
37
        }
38
39 1
        $message = new Message(serialize($movies), [
40 1
            'load_similar' => $loadSimilar,
41 1
            'similar_movies_table' => $similarMoviesTable,
42
        ]);
43
44 1
        $this->producer->sendEvent(MovieSyncProcessor::ADD_MOVIES_TMDB, $message);
45 1
    }
46
47 1
    private function isSupport($movie)
48
    {
49 1
        return $movie instanceof Movie;
50
    }
51
}
52