Completed
Push — master ( 1a9043...984777 )
by Valentyn
08:31
created

WatchedMovieService   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 9
dl 0
loc 138
ccs 28
cts 56
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

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