Completed
Push — master ( b4d909...e3c084 )
by Valentyn
03:37
created

Movie   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 391
Duplicated Lines 0 %

Coupling/Cohesion

Components 8
Dependencies 7

Test Coverage

Coverage 70.4%

Importance

Changes 0
Metric Value
wmc 35
lcom 8
cbo 7
dl 0
loc 391
ccs 69
cts 98
cp 0.704
rs 9.6
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
A getId() 0 4 1
A addGenre() 0 6 1
A removeAllGenres() 0 6 1
A getGenres() 0 4 1
A addActor() 0 7 1
A addSimilarMovie() 0 7 1
A removeAllSimilarMovies() 0 6 1
A getSimilarMovies() 0 4 1
A getRecommendations() 0 4 1
A addRecommendation() 0 7 1
A removeAllRecommendations() 0 6 1
A updateTmdb() 0 4 1
A setImdbId() 0 6 1
A setRuntime() 0 6 1
A setBudget() 0 6 1
A setReleaseDate() 0 6 1
A getOriginalTitle() 0 4 1
A changeOriginalTitle() 0 6 1
A getOriginalPosterUrl() 0 4 1
A setOriginalPosterUrl() 0 4 1
A getTmdb() 0 4 1
A getImdbId() 0 4 1
A getRuntime() 0 4 1
A getBudget() 0 4 1
A getReleaseDate() 0 4 1
A getUserWatchedMovie() 0 4 1
A getGuestWatchedMovie() 0 4 1
A isWatched() 0 6 3
A getUserRecommendedMovie() 0 4 1
A setOriginalTitle() 0 4 1
A __toString() 0 4 1
A getActors() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Entity;
6
7
use App\Actors\Entity\Actor;
8
use App\Genres\Entity\Genre;
9
use App\Guests\Entity\GuestWatchedMovie;
10
use App\Movies\DTO\MovieDTO;
11
use App\Translation\TranslatableInterface;
12
use App\Translation\TranslatableTrait;
13
use App\Users\Entity\User;
14
use App\Users\Entity\UserWatchedMovie;
15
use Doctrine\Common\Collections\ArrayCollection;
16
use Doctrine\ORM\Mapping as ORM;
17
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
18
use Symfony\Component\Serializer\Annotation\Groups;
19
use Symfony\Component\Validator\Constraints as Assert;
20
21
//todo production_countries, production_companies
22
23
/**
24
 * @ORM\Entity(repositoryClass="App\Movies\Repository\MovieRepository")
25
 * @ORM\Table(name="movies")
26
 *
27
 * @method MovieTranslations getTranslation(string $locale, bool $useFallbackLocale = true)
28
 * @UniqueEntity("tmdb.id")
29
 */
30
class Movie implements TranslatableInterface
31
{
32
    use TranslatableTrait;
33
34
    /**
35
     * @ORM\Id()
36
     * @ORM\GeneratedValue()
37
     * @ORM\Column(type="integer")
38
     * @Groups({"list", "view"})
39
     */
40
    private $id;
41
42
    /**
43
     * @var MovieTranslations[]|ArrayCollection
44
     * @ORM\OneToMany(targetEntity="App\Movies\Entity\MovieTranslations", mappedBy="movie", cascade={"persist", "remove"})
45
     * @Assert\Valid(traverse=true)
46
     * @Groups({"list", "view"})
47
     */
48
    private $translations;
49
50
    /**
51
     * @var Actor[]|ArrayCollection
52
     * @ORM\OneToMany(targetEntity="App\Movies\Entity\MovieActor", mappedBy="movie", cascade={"persist", "remove"})
53
     * @Assert\Valid(traverse=true)
54
     * @Groups({"view"})
55
     */
56
    private $actors;
57
58
    /**
59
     * @var Genre[]|ArrayCollection
60
     * @ORM\ManyToMany(targetEntity="App\Genres\Entity\Genre")
61
     * @ORM\JoinTable(name="movies_genres",
62
     *      joinColumns={@ORM\JoinColumn(name="movie_id", referencedColumnName="id")},
63
     *      inverseJoinColumns={@ORM\JoinColumn(name="genre_id", referencedColumnName="id")}
64
     *      )
65
     * @ORM\JoinColumn(nullable=false)
66
     * @Assert\Valid(traverse=true)
67
     * @Groups({"list", "view"})
68
     */
69
    private $genres;
70
71
    /**
72
     * @Groups({"list", "view"})
73
     * @ORM\Column(type="string", length=100)
74
     */
75
    private $originalTitle;
76
77
    /**
78
     * @Groups({"list", "view"})
79
     * @ORM\Column(type="string", length=255, nullable=true)
80
     */
81
    private $originalPosterUrl;
82
83
    /**
84
     * @ORM\Embedded(class="App\Movies\Entity\MovieTMDB", columnPrefix="tmdb_")
85
     * @Assert\Valid(traverse=true)
86
     * @Groups({"list", "view"})
87
     */
88
    private $tmdb;
89
90
    /**
91
     * @ORM\Column(type="string", length=20, nullable=true)
92
     * @Groups({"view"})
93
     */
94
    private $imdbId;
95
96
    /**
97
     * @Groups({"view"})
98
     * @ORM\Column(type="integer", nullable=true, options={"default": 0})
99
     */
100
    private $runtime;
101
102
    /**
103
     * @Groups({"view"})
104
     * @ORM\Column(type="integer", nullable=true, options={"default": 0})
105
     */
106
    private $budget;
107
108
    /**
109
     * @Groups({"list", "view"})
110
     * @ORM\Column(type="date", nullable=true)
111
     */
112
    private $releaseDate;
113
114
    /**
115
     * @var GuestWatchedMovie
116
     * @ORM\OneToOne(targetEntity="App\Guests\Entity\GuestWatchedMovie", mappedBy="movie")
117
     * @Groups({"list", "view"})
118
     */
119
    private $guestWatchedMovie;
120
121
    /**
122
     * @var UserWatchedMovie
123
     * @ORM\OneToOne(targetEntity="App\Users\Entity\UserWatchedMovie", mappedBy="movie")
124
     * @Groups({"ROLE_USER"})
125
     */
126
    private $userWatchedMovie;
127
128
    /**
129
     * @Groups({"list", "view"})
130
     */
131
    private $isWatched;
132
133
    /**
134
     * @var MovieRecommendation
135
     * @ORM\OneToOne(targetEntity="App\Movies\Entity\MovieRecommendation", mappedBy="recommendedMovie")
136
     * @Groups({"ROLE_USER"})
137
     */
138
    private $userRecommendedMovie;
139
140
    /**
141
     * @ORM\OneToMany(targetEntity="App\Movies\Entity\SimilarMovie", mappedBy="originalMovie", cascade={"persist", "remove"})
142
     */
143
    private $similarMovies;
144
145
    /**
146
     * @ORM\OneToMany(targetEntity="App\Movies\Entity\MovieRecommendation", mappedBy="originalMovie", cascade={"persist", "remove"})
147
     */
148
    private $recommendations;
149
150 3
    public function __construct(MovieDTO $movieDTO, MovieTMDB $tmdb)
151
    {
152 3
        $this->translations = new ArrayCollection();
153 3
        $this->genres = new ArrayCollection();
154 3
        $this->similarMovies = new ArrayCollection();
155 3
        $this->recommendations = new ArrayCollection();
156 3
        $this->actors = new ArrayCollection();
157
158 3
        $this->originalTitle = $movieDTO->getOriginalTitle();
159 3
        $this->originalPosterUrl = $movieDTO->getOriginalPosterUrl();
160 3
        $this->setImdbId($movieDTO->getImdbId());
161 3
        $this->setBudget($movieDTO->getBudget());
162 3
        $this->setRuntime($movieDTO->getRuntime());
163 3
        $this->setReleaseDate($movieDTO->getReleaseDate());
164 3
        $this->tmdb = $tmdb;
165 3
    }
166
167 15
    public function getId(): ?int
168
    {
169 15
        return $this->id;
170
    }
171
172 3
    public function addGenre(Genre $genre)
173
    {
174 3
        $this->genres->add($genre);
175
176 3
        return $this;
177
    }
178
179
    public function removeAllGenres()
180
    {
181
        $this->genres->clear();
182
183
        return $this;
184
    }
185
186
    /**
187
     * @return Genre[]|array
188
     */
189 12
    public function getGenres()
190
    {
191 12
        return $this->genres->toArray();
192
    }
193
194
    public function addActor(Actor $actor)
195
    {
196
        $movieActor = new MovieActor($this, $actor);
197
        $this->actors->add($movieActor);
198
199
        return $this;
200
    }
201
202
    /**
203
     * @return Actor[]|array
204
     */
205 2
    public function getActors(): array
206
    {
207 2
        $movieActors = $this->actors->toArray();
208
209
        return array_map(function (MovieActor $movieActor) {
210 1
            return $movieActor->getActor();
211 2
        }, $movieActors);
212
    }
213
214
    public function addSimilarMovie(self $similarMovie)
215
    {
216
        $similarMovie = new SimilarMovie($this, $similarMovie);
217
        $this->similarMovies->add($similarMovie);
218
219
        return $this;
220
    }
221
222
    public function removeAllSimilarMovies()
223
    {
224
        $this->similarMovies->clear();
225
226
        return $this;
227
    }
228
229
    /**
230
     * @return Movie[]|array
231
     */
232 7
    public function getSimilarMovies()
233
    {
234 7
        return $this->similarMovies->toArray();
235
    }
236
237
    /**
238
     * @return Movie[]|array
239
     */
240
    public function getRecommendations()
241
    {
242
        return $this->recommendations->toArray();
243
    }
244
245 1
    public function addRecommendation(User $user, self $recommendedMovie)
246
    {
247 1
        $recommendedMovie = new MovieRecommendation($user, $this, $recommendedMovie);
248 1
        $this->recommendations->add($recommendedMovie);
249
250 1
        return $this;
251
    }
252
253
    public function removeAllRecommendations()
254
    {
255
        $this->recommendations->clear();
256
257
        return $this;
258
    }
259
260
    public function updateTmdb(MovieTMDB $tmdb)
261
    {
262
        $this->tmdb = $tmdb;
263
    }
264
265
    /**
266
     * @param string $imdbId
267
     *
268
     * @return Movie
269
     */
270 4
    public function setImdbId(?string $imdbId)
271
    {
272 4
        $this->imdbId = $imdbId;
273
274 4
        return $this;
275
    }
276
277
    /**
278
     * @param int $runtime
279
     *
280
     * @return Movie
281
     */
282 4
    public function setRuntime(int $runtime)
283
    {
284 4
        $this->runtime = $runtime;
285
286 4
        return $this;
287
    }
288
289
    /**
290
     * @param int $budget
291
     *
292
     * @return Movie
293
     */
294 4
    public function setBudget(int $budget)
295
    {
296 4
        $this->budget = $budget;
297
298 4
        return $this;
299
    }
300
301
    /**
302
     * @param \DateTimeInterface $releaseDate
303
     *
304
     * @return Movie
305
     */
306 4
    public function setReleaseDate(\DateTimeInterface $releaseDate)
307
    {
308 4
        $this->releaseDate = $releaseDate;
309
310 4
        return $this;
311
    }
312
313
    /**
314
     * @return string
315
     */
316 12
    public function getOriginalTitle()
317
    {
318 12
        return $this->originalTitle;
319
    }
320
321
    /**
322
     * @param string $title
323
     *
324
     * @return $this
325
     */
326
    public function changeOriginalTitle(string $title)
327
    {
328
        $this->originalTitle = $title;
329
330
        return $this;
331
    }
332
333
    /**
334
     * @return mixed
335
     */
336 12
    public function getOriginalPosterUrl()
337
    {
338 12
        return $this->originalPosterUrl;
339
    }
340
341
    public function setOriginalPosterUrl(string $url)
342
    {
343
        return $this->originalPosterUrl = $url;
344
    }
345
346
    /**
347
     * @return MovieTMDB
348
     */
349 13
    public function getTmdb()
350
    {
351 13
        return $this->tmdb;
352
    }
353
354
    /**
355
     * @return mixed
356
     */
357 2
    public function getImdbId()
358
    {
359 2
        return $this->imdbId;
360
    }
361
362
    /**
363
     * @return mixed
364
     */
365 2
    public function getRuntime()
366
    {
367 2
        return $this->runtime;
368
    }
369
370
    /**
371
     * @return mixed
372
     */
373 2
    public function getBudget()
374
    {
375 2
        return $this->budget;
376
    }
377
378
    /**
379
     * @return mixed
380
     */
381 12
    public function getReleaseDate()
382
    {
383 12
        return $this->releaseDate;
384
    }
385
386 3
    public function getUserWatchedMovie()
387
    {
388 3
        return $this->userWatchedMovie;
389
    }
390
391 12
    public function getGuestWatchedMovie()
392
    {
393 12
        return $this->guestWatchedMovie;
394
    }
395
396 12
    public function isWatched()
397
    {
398 12
        $this->isWatched = ($this->userWatchedMovie || $this->guestWatchedMovie) ? true : false;
399
400 12
        return $this->isWatched;
401
    }
402
403 3
    public function getUserRecommendedMovie()
404
    {
405 3
        return $this->userRecommendedMovie;
406
    }
407
408
    /**
409
     * @param string $originalTitle
410
     */
411 1
    public function setOriginalTitle(string $originalTitle): void
412
    {
413 1
        $this->originalTitle = $originalTitle;
414 1
    }
415
416
    public function __toString()
417
    {
418
        return $this->getId().' | '.$this->getOriginalTitle().' | TMDB: '.$this->getTmdb()->getId();
419
    }
420
}
421