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