Completed
Push — master ( 0b4911...4851fa )
by Valentyn
04:19 queued 11s
created

TmdbSyncService   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 81.82%

Importance

Changes 0
Metric Value
dl 0
loc 46
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4
ccs 18
cts 22
cp 0.8182
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isSupport() 0 4 3
A syncMovies() 0 29 5
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\MessagePriority;
12
use Enqueue\Client\ProducerInterface;
13
14
class TmdbSyncService
15
{
16
    private $repository;
17
    private $producer;
18
19 11
    public function __construct(MovieRepository $repository, ProducerInterface $producer)
20
    {
21 11
        $this->repository = $repository;
22 11
        $this->producer = $producer;
23 11
    }
24
25 1
    public function syncMovies(array $movies, bool $loadSimilar = false): void
26
    {
27 1
        if (!\count($movies)) {
28
            return;
29
        }
30
31 1
        if ($this->isSupport(reset($movies)) === false) {
32
            throw new \InvalidArgumentException('Unsupported array of movies provided');
33
        }
34
35
        $alreadySavedMovies = $this->repository->findAllByTmdbIds(array_map(function (array $tmdbMovie) {
36 1
            return $tmdbMovie['id'];
37 1
        }, $movies));
38
        $alreadySavedMoviesIds = array_flip(array_map(function (Movie $movie) {
39
            return $movie->getId();
40 1
        }, $alreadySavedMovies));
41
42 1
        foreach ($movies as $movie) {
43 1
            if (isset($alreadySavedMoviesIds[$movie['id']])) {
44
                continue;
45
            }
46
47 1
            $message = new Message(json_encode($movie));
48
            // todo redis doesnt support priority $message->setPriority(MessagePriority::HIGH);
49 1
            $message->setProperty(MovieSyncProcessor::PARAM_LOAD_SIMILAR_MOVIES, $loadSimilar);
50
51 1
            $this->producer->sendEvent(MovieSyncProcessor::ADD_MOVIES_TMDB, $message);
52
        }
53 1
    }
54
55 1
    private function isSupport($movie)
56
    {
57 1
        return \is_array($movie) && isset($movie['id']) && isset($movie['original_title']);
58
    }
59
}
60