Completed
Push — master ( 685b70...d41a9c )
by Valentyn
03:12
created

WatchedMovieService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Service;
6
7
use App\Guests\Entity\GuestSession;
8
use App\Guests\Entity\GuestWatchedMovie;
9
use App\Guests\Repository\WatchedMovieRepository;
10
use App\Movies\DTO\WatchedMovieDTO;
11
use App\Movies\Entity\Movie;
12
use App\Movies\Entity\WatchedMovie;
13
use App\Movies\EventListener\SimilarMoviesProcessor;
14
use App\Movies\EventListener\WatchedMovieProcessor;
15
use App\Movies\Repository\MovieRepository;
16
use App\Users\Entity\User;
17
use App\Users\Entity\UserWatchedMovie;
18
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
19
use Doctrine\ORM\EntityManagerInterface;
20
use Enqueue\Client\ProducerInterface;
21
22
class WatchedMovieService
23
{
24
    private $em;
25
    /**
26
     * @var MovieRepository
27
     */
28
    private $repository;
29
30
    private $searchService;
31
32
    private $producer;
33
34 7
    public function __construct(EntityManagerInterface $entityManager, SearchService $searchService, ProducerInterface $producer)
35
    {
36 7
        $this->em = $entityManager;
37 7
        $this->repository = $entityManager->getRepository(Movie::class);
38 7
        $this->searchService = $searchService;
39 7
        $this->producer = $producer;
40 7
    }
41
42
    /**
43
     * @param User            $user
44
     * @param WatchedMovieDTO $watchedMovieDTO
45
     * @param string          $locale
46
     *
47
     * @throws \Exception
48
     *
49
     * @return bool
50
     */
51 4
    public function addUserWatchedMovie(User $user, WatchedMovieDTO $watchedMovieDTO, string $locale): bool
52
    {
53 4
        $movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId());
54
55 4
        if ($movie === null) {
56
            // Lets try to find it in TMDB library
57
            $movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale);
58
        }
59
60 4
        if ($movie === null) {
61
            return false;
62
        }
63
64 4
        $newWatchedMovie = new UserWatchedMovie($user, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt());
65
66 4
        if ($movie->getId() === null) {
67
            $this->saveWatchedMovies([$newWatchedMovie]);
68
69
            return true;
70
        }
71
72
        try {
73 4
            $this->em->persist($newWatchedMovie);
74 4
            $this->em->flush();
75 4
            $this->onMovieAdded($newWatchedMovie);
76
        } catch (UniqueConstraintViolationException $exception) {
77
            // You can throw new BadRequestHttpException('This movie already in your library of watched movies');
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
78
            // But I think its nice to return 202 like operation was successful
79
            return true;
80
        }
81
82 4
        return true;
83
    }
84
85 2
    public function updateUserWatchedMovie(WatchedMovie $watchedMovie, WatchedMovieDTO $watchedMovieDTO)
86
    {
87 2
        $watchedMovie->changeVote($watchedMovieDTO->getVote());
88 2
        $watchedMovie->changeWatchedAt($watchedMovieDTO->getWatchedAt());
89 2
    }
90
91
    /**
92
     * @param GuestSession    $guestSession
93
     * @param WatchedMovieDTO $watchedMovieDTO
94
     * @param string          $locale
95
     *
96
     * @throws \Exception
97
     *
98
     * @return bool
99
     */
100 3
    public function addGuestWatchedMovie(GuestSession $guestSession, WatchedMovieDTO $watchedMovieDTO, string $locale): bool
101
    {
102 3
        $movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId());
103
104 3
        if ($movie === null) {
105
            // Lets try to find it in TMDB library
106 1
            $movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale);
107
        }
108
109 3
        if ($movie === null) {
110
            return false;
111
        }
112
113 3
        $newWatchedMovie = new GuestWatchedMovie($guestSession, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt());
114
115 3
        if ($movie->getId() === null) {
116 1
            $this->saveWatchedMovies([$newWatchedMovie]);
117
118 1
            return true;
119
        }
120
121
        try {
122 2
            $this->em->persist($newWatchedMovie);
123 2
            $this->em->flush();
124 2
            $this->onMovieAdded($newWatchedMovie);
125
        } catch (UniqueConstraintViolationException $exception) {
126
            // You can throw new BadRequestHttpException('This movie already in your library of watched movies');
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
            // But I think its nice to return 202 like operation was successful
128
            return true;
129
        }
130
131 2
        return true;
132
    }
133
134
    /**
135
     * @param GuestSession $guestSession
136
     * @param User         $user
137
     *
138
     * @throws \Exception
139
     */
140
    public function mergeWatchedMovies(GuestSession $guestSession, User $user): void
141
    {
142
        /** @var $guestWatchedMoviesRepository WatchedMovieRepository */
143
        $guestWatchedMoviesRepository = $this->em->getRepository(GuestWatchedMovie::class);
144
        $guestWatchedMovies = $guestWatchedMoviesRepository->findBy([
145
            'guestSession' => $guestSession->getId(),
146
        ]);
147
148
        if (!reset($guestWatchedMovies)) {
149
            return;
150
        }
151
152
        $userWatchedMovies = [];
153
        foreach ($guestWatchedMovies as $guestWatchedMovie) {
154
            $movie = $guestWatchedMovie->getMovie();
155
            $vote = $guestWatchedMovie->getVote();
156
            $watchedAt = $guestWatchedMovie->getWatchedAt();
157
            $userWatchedMovies[] = new UserWatchedMovie($user, $movie, $vote, $watchedAt);
158
        }
159
160
        $this->saveWatchedMovies($userWatchedMovies);
161
    }
162
163
    /**
164
     * @param array|UserWatchedMovie[]|GuestWatchedMovie[] $watchedMovies
165
     */
166 1
    private function saveWatchedMovies(array $watchedMovies): void
167
    {
168 1
        $watchedMoviesSerialized = serialize($watchedMovies);
169 1
        $this->producer->sendEvent(WatchedMovieProcessor::ADD_WATCHED_MOVIE_TMDB, $watchedMoviesSerialized);
170 1
    }
171
172 6
    private function onMovieAdded(WatchedMovie $watchedMovie): void
173
    {
174 6
        $movie = $watchedMovie->getMovie();
175 6
        if (count($movie->getSimilarMovies()) === 0) {
176 6
            $this->producer->sendEvent(SimilarMoviesProcessor::LOAD_SIMILAR_MOVIES, json_encode($movie->getId()));
177
        }
178 6
    }
179
}
180