Completed
Push — master ( 201948...c76e97 )
by Valentyn
04:58
created

WatchedMovieService::addUserWatchedMovie()   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.6

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 9
cts 15
cp 0.6
rs 8.439
c 0
b 0
f 0
cc 5
eloc 16
nc 10
nop 3
crap 6.6
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([$newWatchedMovie]);
63
            return true;
64
        }
65
66
        try {
67 2
            $this->em->persist($newWatchedMovie);
68 2
            $this->em->flush();
69
        } catch (UniqueConstraintViolationException $exception) {
70
            // 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...
71
            // But I think its nice to return 202 like operation was successful
72
            return true;
73
        }
74
75 2
        return true;
76
    }
77
78
    /**
79
     * @param GuestSession $guestSession
80
     * @param WatchedMovieDTO $watchedMovieDTO
81
     * @param string $locale
82
     * @return bool
83
     * @throws \Exception
84
     */
85 3
    public function addGuestWatchedMovie(GuestSession $guestSession, WatchedMovieDTO $watchedMovieDTO, string $locale): bool
86
    {
87 3
        $movie = $this->repository->findOneByIdOrTmdbId($watchedMovieDTO->getMovieId(), $watchedMovieDTO->getTmdbId());
88
89 3
        if ($movie === null) {
90
            // Lets try to find it in TMDB library
91 1
            $movie = $this->searchService->findByTmdbId($watchedMovieDTO->getTmdbId(), $locale);
92
        }
93
94 3
        if ($movie === null) {
95
            return false;
96
        }
97
98 3
        $newWatchedMovie = new GuestWatchedMovie($guestSession, $movie, $watchedMovieDTO->getVote(), $watchedMovieDTO->getWatchedAt());
99
100 3
        if ($movie->getId() === null) {
101 1
            $this->saveWatchedMovies([$newWatchedMovie]);
102 1
            return true;
103
        }
104
105
        try {
106 2
            $this->em->persist($newWatchedMovie);
107 2
            $this->em->flush();
108
        } catch (UniqueConstraintViolationException $exception) {
109
            // 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...
110
            // But I think its nice to return 202 like operation was successful
111
            return true;
112
        }
113
114 2
        return true;
115
    }
116
117
    /**
118
     * @param GuestSession $guestSession
119
     * @param User $user
120
     * @throws \Exception
121
     */
122
    public function mergeWatchedMovies(GuestSession $guestSession, User $user): void
123
    {
124
        /** @var $guestWatchedMoviesRepository WatchedMovieRepository */
125
        $guestWatchedMoviesRepository = $this->em->getRepository(GuestWatchedMovie::class);
126
        $guestWatchedMovies = $guestWatchedMoviesRepository->findBy([
127
            'guestSession' => $guestSession->getId()
128
        ]);
129
130
        if (!reset($guestWatchedMovies)) {
131
            return;
132
        }
133
134
        $userWatchedMovies = [];
135
        foreach ($guestWatchedMovies as $guestWatchedMovie) {
136
            $movie = $guestWatchedMovie->getMovie();
137
            $vote = $guestWatchedMovie->getVote();
138
            $watchedAt = $guestWatchedMovie->getWatchedAt();
139
            $userWatchedMovies[] = new UserWatchedMovie($user, $movie, $vote, $watchedAt);
140
        }
141
142
        $this->saveWatchedMovies($userWatchedMovies);
143
    }
144
145
    /**
146
     * @param array|UserWatchedMovie[]|GuestWatchedMovie[] $watchedMovies
147
     */
148 1
    private function saveWatchedMovies(array $watchedMovies): void
149
    {
150 1
        $watchedMoviesSerialized = serialize($watchedMovies);
151 1
        $this->producer->sendEvent(WatchedMovieProcessor::ADD_WATCHED_MOVIE_TMDB, $watchedMoviesSerialized);
152
    }
153
}