1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\EventListener; |
4
|
|
|
|
5
|
|
|
use App\Movies\Repository\MovieRepository; |
6
|
|
|
use App\Movies\Utils\Poster; |
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
8
|
|
|
use Enqueue\Client\ProducerInterface; |
9
|
|
|
use Enqueue\Client\TopicSubscriberInterface; |
10
|
|
|
use Interop\Queue\Context; |
11
|
|
|
use Interop\Queue\Message as QMessage; |
12
|
|
|
use Interop\Queue\Processor; |
13
|
|
|
|
14
|
|
|
class MoviePostersProcessor implements Processor, TopicSubscriberInterface |
15
|
|
|
{ |
16
|
|
|
public const LOAD_POSTERS = 'LoadMoviesPosters'; |
17
|
|
|
|
18
|
|
|
private $em; |
19
|
|
|
private $movieRepository; |
20
|
|
|
private $producer; |
21
|
|
|
|
22
|
|
|
public function __construct(EntityManagerInterface $em, MovieRepository $movieRepository, ProducerInterface $producer) |
23
|
|
|
{ |
24
|
|
|
$this->em = $em; |
25
|
|
|
$this->movieRepository = $movieRepository; |
26
|
|
|
$this->producer = $producer; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function process(QMessage $message, Context $session) |
30
|
|
|
{ |
31
|
|
|
$movieId = $message->getBody(); |
32
|
|
|
$movieId = json_decode($movieId, true); |
33
|
|
|
|
34
|
|
|
$movie = $this->movieRepository->find($movieId); |
35
|
|
|
|
36
|
|
|
if ($movie === null) { |
37
|
|
|
return self::REJECT; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$posterUrl = $movie->getOriginalPosterUrl(); |
41
|
|
|
// $posterName = str_replace('https://image.tmdb.org/t/p/original', '', $posterUrl); |
42
|
|
|
if ($posterUrl === 'https://image.tmdb.org/t/p/original') { |
43
|
|
|
return self::REJECT; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$posterPath = Poster::savePoster($movie->getId(), $movie->getOriginalPosterUrl()); |
47
|
|
|
if ($posterPath === null) { |
48
|
|
|
return self::REJECT; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->producer->sendEvent(PosterResizerProcessor::RESIZE_POSTERS, json_encode($movieId)); |
52
|
|
|
$movie->setOriginalPosterUrl(Poster::getUrl($movie->getId())); |
53
|
|
|
|
54
|
|
|
$this->em->flush(); |
55
|
|
|
$this->em->clear(); |
56
|
|
|
|
57
|
|
|
$message = $session = $movieId = $movie = null; |
58
|
|
|
unset($message, $session, $movieId, $movie); |
59
|
|
|
|
60
|
|
|
return self::ACK; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function getSubscribedTopics() |
64
|
|
|
{ |
65
|
|
|
return [self::LOAD_POSTERS]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|