1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace App\Movies\Service; |
5
|
|
|
|
6
|
|
|
use App\Guests\Repository\WatchedMovieRepository; |
7
|
|
|
use App\Movies\DTO\WatchedMovieDTO; |
8
|
|
|
use App\Movies\Entity\Movie; |
9
|
|
|
use App\Guests\Entity\GuestSession; |
10
|
|
|
use App\Guests\Entity\GuestWatchedMovie; |
11
|
|
|
use App\Movies\Entity\WatchedMovie; |
12
|
|
|
use App\Users\Entity\UserWatchedMovie; |
13
|
|
|
use App\Movies\EventListener\WatchedMovieProcessor; |
14
|
|
|
use App\Movies\Repository\MovieRepository; |
15
|
|
|
use App\Movies\Service\SearchService; |
16
|
|
|
use App\Users\Entity\User; |
17
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
18
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
19
|
|
|
use Enqueue\Client\ProducerInterface; |
20
|
|
|
|
21
|
|
|
class WatchedMovieService |
22
|
|
|
{ |
23
|
|
|
private $em; |
24
|
|
|
/** |
25
|
|
|
* @var MovieRepository |
26
|
|
|
*/ |
27
|
|
|
private $repository; |
28
|
|
|
|
29
|
|
|
private $searchService; |
30
|
|
|
|
31
|
|
|
private $producer; |
32
|
|
|
|
33
|
5 |
|
public function __construct(EntityManagerInterface $entityManager, SearchService $searchService, ProducerInterface $producer) |
34
|
|
|
{ |
35
|
5 |
|
$this->em = $entityManager; |
36
|
5 |
|
$this->repository = $entityManager->getRepository(Movie::class); |
37
|
5 |
|
$this->searchService = $searchService; |
38
|
5 |
|
$this->producer = $producer; |
39
|
5 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param User $user |
43
|
|
|
* @param WatchedMovieDTO $watchedMovieDTO |
44
|
|
|
* @param string $locale |
45
|
|
|
* @return bool |
46
|
|
|
* @throws \Exception |
47
|
|
|
*/ |
48
|
2 |
|
public function addUserWatchedMovie(User $user, WatchedMovieDTO $watchedMovieDTO, string $locale): bool |
49
|
|
|
{ |
50
|
2 |
|
$movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId()); |
51
|
|
|
|
52
|
2 |
|
if ($movie === null) { |
53
|
|
|
// Lets try to find it in TMDB library |
54
|
|
|
$movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
if ($movie === null) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$newWatchedMovie = new UserWatchedMovie($user, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt()); |
|
|
|
|
62
|
|
|
|
63
|
2 |
|
if ($movie->getId() === null) { |
64
|
|
|
$this->saveWatchedMovies([ |
65
|
|
|
$newWatchedMovie |
66
|
|
|
]); |
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
try { |
71
|
2 |
|
$this->em->persist($newWatchedMovie); |
72
|
2 |
|
$this->em->flush(); |
73
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
74
|
|
|
// You can throw new BadRequestHttpException('This movie already in your library of watched movies'); |
|
|
|
|
75
|
|
|
// But I think its nice to return 202 like operation was successful |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param GuestSession $guestSession |
84
|
|
|
* @param WatchedMovieDTO $watchedMovieDTO |
85
|
|
|
* @param string $locale |
86
|
|
|
* @return bool |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
3 |
|
public function addGuestWatchedMovie(GuestSession $guestSession, WatchedMovieDTO $watchedMovieDTO, string $locale): bool |
90
|
|
|
{ |
91
|
3 |
|
$movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId()); |
92
|
|
|
|
93
|
3 |
|
if ($movie === null) { |
94
|
|
|
// Lets try to find it in TMDB library |
95
|
1 |
|
$movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale); |
96
|
|
|
} |
97
|
|
|
|
98
|
3 |
|
if ($movie === null) { |
99
|
|
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
$newWatchedMovie = new GuestWatchedMovie($guestSession, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt()); |
|
|
|
|
103
|
|
|
|
104
|
3 |
|
if ($movie->getId() === null) { |
105
|
1 |
|
$watchedMovieSerialized = serialize([$newWatchedMovie]); |
106
|
1 |
|
$this->producer->sendEvent(WatchedMovieProcessor::ADD_WATCHED_MOVIE_TMDB, $watchedMovieSerialized); |
107
|
1 |
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
try { |
111
|
2 |
|
$this->em->persist($newWatchedMovie); |
112
|
2 |
|
$this->em->flush(); |
113
|
|
|
} catch (UniqueConstraintViolationException $exception) { |
114
|
|
|
// You can throw new BadRequestHttpException('This movie already in your library of watched movies'); |
|
|
|
|
115
|
|
|
// But I think its nice to return 202 like operation was successful |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param GuestSession $guestSession |
124
|
|
|
* @param User $user |
125
|
|
|
* @throws \Exception |
126
|
|
|
*/ |
127
|
|
|
public function mergeWatchedMovies(GuestSession $guestSession, User $user): void |
128
|
|
|
{ |
129
|
|
|
/** @var $guestWatchedMoviesRepository WatchedMovieRepository */ |
130
|
|
|
$guestWatchedMoviesRepository = $this->em->getRepository(GuestWatchedMovie::class); |
131
|
|
|
$guestWatchedMovies = $guestWatchedMoviesRepository->findBy([ |
132
|
|
|
'guestSession' => $guestSession->getId() |
133
|
|
|
]); |
134
|
|
|
|
135
|
|
|
if (!reset($guestWatchedMovies)) { |
136
|
|
|
return; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$userWatchedMovies = []; |
140
|
|
|
foreach ($guestWatchedMovies as $guestWatchedMovie) { |
141
|
|
|
$movie = $guestWatchedMovie->getMovie(); |
142
|
|
|
$vote = $guestWatchedMovie->getVote(); |
143
|
|
|
$watchedAt = $guestWatchedMovie->getWatchedAt(); |
144
|
|
|
$userWatchedMovies[] = new UserWatchedMovie($user, $movie, $vote, $watchedAt); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->saveWatchedMovies($userWatchedMovies); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array|UserWatchedMovie[]|GuestWatchedMovie[] $watchedMovies |
152
|
|
|
*/ |
153
|
|
|
private function saveWatchedMovies(array $watchedMovies): void |
154
|
|
|
{ |
155
|
|
|
$watchedMoviesSerialized = serialize($watchedMovies); |
156
|
|
|
$this->producer->sendEvent(WatchedMovieProcessor::ADD_WATCHED_MOVIE_TMDB, $watchedMoviesSerialized); |
157
|
|
|
} |
158
|
|
|
} |