Completed
Push — master ( 685b70...d41a9c )
by Valentyn
03:12
created

Movie::setRuntime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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