1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\EventListener; |
4
|
|
|
|
5
|
|
|
use App\Movies\Repository\MovieRepository; |
6
|
|
|
use App\Users\Entity\User; |
7
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
8
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
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 AddRecommendationProcessor implements Processor, TopicSubscriberInterface |
15
|
|
|
{ |
16
|
|
|
public const ADD_RECOMMENDATION = 'AddRecommendation'; |
17
|
|
|
|
18
|
|
|
private $em; |
19
|
|
|
private $movieRepository; |
20
|
|
|
|
21
|
4 |
|
public function __construct(EntityManagerInterface $em, MovieRepository $movieRepository) |
22
|
|
|
{ |
23
|
4 |
|
$this->em = $em; |
24
|
4 |
|
$this->movieRepository = $movieRepository; |
25
|
4 |
|
} |
26
|
|
|
|
27
|
4 |
|
public function process(QMessage $message, Context $session) |
28
|
|
|
{ |
29
|
4 |
|
$movie = $message->getBody(); |
30
|
4 |
|
$movie = json_decode($movie, true); |
31
|
|
|
|
32
|
4 |
|
$originalMovie = $this->movieRepository->findOneByIdOrTmdbId($movie['movie_id']); |
33
|
|
|
|
34
|
4 |
|
if ($originalMovie === null) { |
35
|
1 |
|
return self::REJECT; |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
$recommendedMovie = $this->movieRepository->findOneByIdOrTmdbId(null, $movie['tmdb_id']); |
39
|
|
|
|
40
|
3 |
|
if ($recommendedMovie === null) { |
41
|
1 |
|
return self::REJECT; |
42
|
|
|
} |
43
|
|
|
|
44
|
2 |
|
$user = $this->em->getReference(User::class, $movie['user_id']); |
45
|
|
|
|
46
|
2 |
|
$originalMovie->addRecommendation($user, $recommendedMovie); |
|
|
|
|
47
|
2 |
|
$this->em->persist($originalMovie); |
48
|
|
|
|
49
|
|
|
try { |
50
|
2 |
|
$this->em->flush(); |
51
|
1 |
|
} catch (UniqueConstraintViolationException $uniqueConstraintViolationException) { |
52
|
|
|
// do nothing, it's ok |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
$this->em->clear(); |
56
|
|
|
|
57
|
2 |
|
return self::ACK; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function getSubscribedTopics() |
61
|
|
|
{ |
62
|
|
|
return [self::ADD_RECOMMENDATION]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: