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
|
|
|
|