1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Movies\EventListener; |
4
|
|
|
|
5
|
|
|
use App\Movies\Entity\Movie; |
6
|
|
|
use App\Movies\Repository\MovieRepository; |
7
|
|
|
use App\Movies\Service\TmdbSearchService; |
8
|
|
|
use App\Users\Entity\User; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use Enqueue\Client\TopicSubscriberInterface; |
12
|
|
|
use Interop\Queue\Context; |
13
|
|
|
use Interop\Queue\Message as QMessage; |
14
|
|
|
use Interop\Queue\Processor; |
15
|
|
|
|
16
|
|
|
class AddSimilarMoviesProcessor implements Processor, TopicSubscriberInterface |
17
|
|
|
{ |
18
|
|
|
const ADD_SIMILAR_MOVIES = 'AddSimilarMovies'; |
19
|
|
|
|
20
|
|
|
/** @var EntityManager */ |
21
|
|
|
private $em; |
22
|
|
|
private $searchService; |
23
|
|
|
private $movieRepository; |
24
|
|
|
|
25
|
|
|
public function __construct(EntityManagerInterface $em, MovieRepository $movieRepository, TmdbSearchService $searchService) |
26
|
|
|
{ |
27
|
|
|
$this->em = $em; |
|
|
|
|
28
|
|
|
$this->movieRepository = $movieRepository; |
29
|
|
|
$this->searchService = $searchService; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function process(QMessage $message, Context $session) |
33
|
|
|
{ |
34
|
|
|
$moviesTable = $message->getBody(); |
35
|
|
|
$moviesTable = json_decode($moviesTable, true); |
36
|
|
|
|
37
|
|
|
if ($this->em->isOpen() === false) { |
38
|
|
|
throw new \ErrorException('em is closed'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$originalMoviesIds = array_keys($moviesTable); |
42
|
|
|
$movies = $this->movieRepository->findAllByIds($originalMoviesIds); |
43
|
|
|
$supportAcc = $this->em->getReference(User::class, 1); |
44
|
|
|
|
45
|
|
|
//todo add tests |
46
|
|
|
foreach ($movies as $movie) { |
47
|
|
|
$similarMovies = $this->movieRepository->findAllIdsByTmdbIds($moviesTable[$movie->getId()]); |
48
|
|
|
foreach ($similarMovies as $similarMovie) { |
49
|
|
|
$similarMovieRef = $this->em->getReference(Movie::class, $similarMovie['id']); |
50
|
|
|
$movie->addSimilarMovie($similarMovieRef); |
51
|
|
|
if (is_numeric($similarMovie['tmdb.voteAverage']) && $similarMovie['tmdb.voteAverage'] >= 7) { |
52
|
|
|
if ($similarMovie['releaseDate'] !== null) { |
53
|
|
|
$releaseDate = new \DateTimeImmutable($similarMovie['releaseDate']); |
54
|
|
|
// some kind of discrimination of old movies |
55
|
|
|
if ($releaseDate->format('Y') <= 1980 && $similarMovie['tmdb.voteAverage'] <= 8) { |
56
|
|
|
continue; |
57
|
|
|
} |
58
|
|
|
if ($releaseDate->format('Y') <= 1990 && $similarMovie['tmdb.voteAverage'] <= 7) { |
59
|
|
|
continue; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$movie->addRecommendation($supportAcc, $similarMovieRef); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->em->persist($movie); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->em->flush(); |
71
|
|
|
$this->em->clear(); |
72
|
|
|
|
73
|
|
|
return self::ACK; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function getSubscribedTopics() |
77
|
|
|
{ |
78
|
|
|
return [self::ADD_SIMILAR_MOVIES]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.