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