Completed
Push — master ( 3d822c...064d2b )
by Valentyn
32:01
created

Movie::getRuntime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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