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