Completed
Push — master ( 26caf8...b62262 )
by Valentyn
03:08
created

TmdbSyncService::syncMovies()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.1105

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 13
cp 0.7692
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3.1105
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
            echo "Array of movies is empty! \r\n";
33
            return;
34
        }
35
36 1
        if ($this->isSupport(reset($movies)) === false) {
37
            throw new \InvalidArgumentException('Unsupported array of movies provided');
38
        }
39
40 1
        $message = new Message(serialize($movies), [
41 1
            'load_similar' => $loadSimilar,
42 1
            'similar_movies_table' => $similarMoviesTable,
43
        ]);
44
45 1
        echo "Event sent with similar_movies_table as: \r\n";
46 1
        echo var_export($similarMoviesTable);
47 1
        $this->producer->sendEvent(MovieSyncProcessor::ADD_MOVIES_TMDB, $message);
48 1
    }
49
50 1
    private function isSupport($movie)
51
    {
52 1
        return $movie instanceof Movie;
53
    }
54
}
55