Completed
Push — master ( 665a2d...66f2ad )
by Valentyn
03:38
created

WatchedMovieService::saveWatchedMovies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\WatchedMovieProcessor;
14
use App\Movies\Repository\MovieRepository;
15
use App\Users\Entity\User;
16
use App\Users\Entity\UserWatchedMovie;
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 7
    public function __construct(EntityManagerInterface $entityManager, SearchService $searchService, ProducerInterface $producer)
34
    {
35 7
        $this->em = $entityManager;
36 7
        $this->repository = $entityManager->getRepository(Movie::class);
37 7
        $this->searchService = $searchService;
38 7
        $this->producer = $producer;
39 7
    }
40
41
    /**
42
     * @param User            $user
43
     * @param WatchedMovieDTO $watchedMovieDTO
44
     * @param string          $locale
45
     *
46
     * @throws \Exception
47
     *
48
     * @return bool
49
     */
50 4
    public function addUserWatchedMovie(User $user, WatchedMovieDTO $watchedMovieDTO, string $locale): bool
51
    {
52 4
        $movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId());
53
54 4
        if ($movie === null) {
55
            // Lets try to find it in TMDB library
56
            $movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale);
57
        }
58
59 4
        if ($movie === null) {
60
            return false;
61
        }
62
63 4
        $newWatchedMovie = new UserWatchedMovie($user, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt());
64
65 4
        if ($movie->getId() === null) {
66
            $this->saveWatchedMovies([$newWatchedMovie]);
67
68
            return true;
69
        }
70
71
        try {
72 4
            $this->em->persist($newWatchedMovie);
73 4
            $this->em->flush();
74
        } catch (UniqueConstraintViolationException $exception) {
75
            // 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...
76
            // But I think its nice to return 202 like operation was successful
77
            return true;
78
        }
79
80 4
        return true;
81
    }
82
83 2
    public function updateUserWatchedMovie(WatchedMovie $watchedMovie, WatchedMovieDTO $watchedMovieDTO)
84
    {
85 2
        $watchedMovie->changeVote($watchedMovieDTO->getVote());
86 2
        $watchedMovie->changeWatchedAt($watchedMovieDTO->getWatchedAt());
87 2
    }
88
89
    /**
90
     * @param GuestSession    $guestSession
91
     * @param WatchedMovieDTO $watchedMovieDTO
92
     * @param string          $locale
93
     *
94
     * @throws \Exception
95
     *
96
     * @return bool
97
     */
98 3
    public function addGuestWatchedMovie(GuestSession $guestSession, WatchedMovieDTO $watchedMovieDTO, string $locale): bool
99
    {
100 3
        $movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId());
101
102 3
        if ($movie === null) {
103
            // Lets try to find it in TMDB library
104 1
            $movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale);
105
        }
106
107 3
        if ($movie === null) {
108
            return false;
109
        }
110
111 3
        $newWatchedMovie = new GuestWatchedMovie($guestSession, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt());
112
113 3
        if ($movie->getId() === null) {
114 1
            $this->saveWatchedMovies([$newWatchedMovie]);
115
116 1
            return true;
117
        }
118
119
        try {
120 2
            $this->em->persist($newWatchedMovie);
121 2
            $this->em->flush();
122
        } catch (UniqueConstraintViolationException $exception) {
123
            // 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...
124
            // But I think its nice to return 202 like operation was successful
125
            return true;
126
        }
127
128 2
        return true;
129
    }
130
131
    /**
132
     * @param GuestSession $guestSession
133
     * @param User         $user
134
     *
135
     * @throws \Exception
136
     */
137
    public function mergeWatchedMovies(GuestSession $guestSession, User $user): void
138
    {
139
        /** @var $guestWatchedMoviesRepository WatchedMovieRepository */
140
        $guestWatchedMoviesRepository = $this->em->getRepository(GuestWatchedMovie::class);
141
        $guestWatchedMovies = $guestWatchedMoviesRepository->findBy([
142
            'guestSession' => $guestSession->getId(),
143
        ]);
144
145
        if (!reset($guestWatchedMovies)) {
146
            return;
147
        }
148
149
        $userWatchedMovies = [];
150
        foreach ($guestWatchedMovies as $guestWatchedMovie) {
151
            $movie = $guestWatchedMovie->getMovie();
152
            $vote = $guestWatchedMovie->getVote();
153
            $watchedAt = $guestWatchedMovie->getWatchedAt();
154
            $userWatchedMovies[] = new UserWatchedMovie($user, $movie, $vote, $watchedAt);
155
        }
156
157
        $this->saveWatchedMovies($userWatchedMovies);
158
    }
159
160
    /**
161
     * @param array|UserWatchedMovie[]|GuestWatchedMovie[] $watchedMovies
162
     */
163 1
    private function saveWatchedMovies(array $watchedMovies): void
164
    {
165 1
        $watchedMoviesSerialized = serialize($watchedMovies);
166 1
        $this->producer->sendEvent(WatchedMovieProcessor::ADD_WATCHED_MOVIE_TMDB, $watchedMoviesSerialized);
167 1
    }
168
}
169