Completed
Push — master ( 57fac9...43d856 )
by Valentyn
02:37
created

WatchedMovieService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

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 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
crap 1
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());
0 ignored issues
show
Security Bug introduced by
It seems like $movie can also be of type false; however, App\Users\Entity\UserWatchedMovie::__construct() does only seem to accept object<App\Movies\Entity\Movie>, did you maybe forget to handle an error condition?
Loading history...
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');
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...
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());
0 ignored issues
show
Security Bug introduced by
It seems like $movie can also be of type false; however, App\Guests\Entity\GuestWatchedMovie::__construct() does only seem to accept object<App\Movies\Entity\Movie>, did you maybe forget to handle an error condition?
Loading history...
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');
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...
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
}