1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Movies\Service; |
5
|
|
|
|
6
|
|
|
use App\Movies\Entity\Movie; |
7
|
|
|
use App\Movies\Event\MovieSyncProcessor; |
8
|
|
|
use App\Movies\Repository\MovieRepository; |
9
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
use Enqueue\Client\ProducerInterface; |
12
|
|
|
|
13
|
|
|
class TmdbSyncService |
14
|
|
|
{ |
15
|
|
|
private $logger; |
16
|
|
|
private $repository; |
17
|
|
|
private $em; |
18
|
|
|
private $producer; |
19
|
|
|
|
20
|
|
|
public function __construct(LoggerInterface $logger, MovieRepository $repository, EntityManagerInterface $entityManager, ProducerInterface $producer) |
21
|
|
|
{ |
22
|
|
|
$this->logger = $logger; |
23
|
|
|
$this->repository = $repository; |
24
|
|
|
$this->em = $entityManager; |
25
|
|
|
$this->producer = $producer; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function syncMovies(array $movies): void |
29
|
|
|
{ |
30
|
|
|
if (false === $this->isSupport(reset($movies))) { |
31
|
|
|
$this->logger->error('Unsupported array of movies provided', [ |
32
|
|
|
'movies' => $movies |
33
|
|
|
]); |
34
|
|
|
throw new \InvalidArgumentException('Unsupported array of movies provided'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// todo movies to update |
38
|
|
|
$moviesToSave = $this->getMoviesToSave($movies); |
39
|
|
|
$this->addMovies($moviesToSave); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
private function addMovies(array $movies): void |
43
|
|
|
{ |
44
|
|
|
$this->logger->debug('START Send event ADD_MOVIES_TMDB'); |
45
|
|
|
$this->producer->sendEvent(MovieSyncProcessor::ADD_MOVIES_TMDB, serialize($movies)); |
46
|
|
|
$this->logger->debug('END Send event ADD_MOVIES_TMDB'); |
47
|
|
|
/* |
48
|
|
|
// todo call method to add movies into the queue and then insert them into the database |
49
|
|
|
foreach ($movies as $movie) { |
50
|
|
|
$this->em->persist($movie); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->em->flush();*/ |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function updateMovies(array $movies) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
// todo call method to update movies in the database (use queue) |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* This method give you array of movies which is not yet loaded to our database |
63
|
|
|
* @param array $movies |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
|
|
private function getMoviesToSave(array $movies): array |
67
|
|
|
{ |
68
|
|
|
$ids = array_map(function (Movie $movie) { return $movie->getTmdb()->getId(); }, $movies); |
69
|
|
|
$alreadySavedIds = $this->getAlreadySavedMoviesIdsByTmdbIds($ids); |
70
|
|
|
|
71
|
|
|
return array_filter($movies, function (Movie $movie) use ($alreadySavedIds) { |
72
|
|
|
return in_array($movie->getTmdb()->getId(), $alreadySavedIds) === false; |
73
|
|
|
}); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function getAlreadySavedMoviesIdsByTmdbIds(array $ids) |
77
|
|
|
{ |
78
|
|
|
return $this->repository->getTmdbIds($ids); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function isSupport($movie) |
82
|
|
|
{ |
83
|
|
|
return $movie instanceof Movie; |
84
|
|
|
} |
85
|
|
|
} |