Completed
Push — master ( 2b96a9...428378 )
by Valentyn
04:33
created

Movie::getImdbId()   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\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
     * @ORM\OneToMany(targetEntity="App\Movies\Entity\SimilarMovie", mappedBy="originalMovie", cascade={"persist", "remove"})
126
     */
127
    private $similarMovies;
128
129
    /**
130
     * @ORM\OneToMany(targetEntity="App\Movies\Entity\MovieRecommendation", mappedBy="originalMovie", cascade={"persist", "remove"})
131
     */
132
    private $recommendations;
133
134 3
    public function __construct(MovieDTO $movieDTO, MovieTMDB $tmdb)
135
    {
136 3
        $this->translations = new ArrayCollection();
137 3
        $this->genres = new ArrayCollection();
138 3
        $this->similarMovies = new ArrayCollection();
139 3
        $this->recommendations = new ArrayCollection();
140
141 3
        $this->originalTitle = $movieDTO->getOriginalTitle();
142 3
        $this->originalPosterUrl = $movieDTO->getOriginalPosterUrl();
143 3
        $this->setImdbId($movieDTO->getImdbId());
144 3
        $this->setBudget($movieDTO->getBudget());
145 3
        $this->setRuntime($movieDTO->getRuntime());
146 3
        $this->setReleaseDate($movieDTO->getReleaseDate());
147 3
        $this->tmdb = $tmdb;
148 3
    }
149
150 12
    public function getId(): ?int
151
    {
152 12
        return $this->id;
153
    }
154
155 3
    public function addGenre(Genre $genre)
156
    {
157 3
        $this->genres->add($genre);
158
159 3
        return $this;
160
    }
161
162
    public function removeAllGenres()
163
    {
164
        $this->genres->clear();
165
166
        return $this;
167
    }
168
169
    /**
170
     * @return Genre[]|array
171
     */
172 9
    public function getGenres()
173
    {
174 9
        return $this->genres->toArray();
175
    }
176
177
    public function addSimilarMovie(self $similarMovie)
178
    {
179
        $similarMovie = new SimilarMovie($this, $similarMovie);
180
        $this->similarMovies->add($similarMovie);
181
182
        return $this;
183
    }
184
185
    public function removeAllSimilarMovies()
186
    {
187
        $this->similarMovies->clear();
188
189
        return $this;
190
    }
191
192
    /**
193
     * @return Movie[]|array
194
     */
195
    public function getSimilarMovies()
196
    {
197
        return $this->similarMovies->toArray();
198
    }
199
200
    /**
201
     * @return Genre[]|array
202
     */
203
    public function getRecommendations()
204
    {
205
        return $this->recommendations->toArray();
206
    }
207
208 1
    public function addRecommendation(User $user, self $recommendedMovie)
209
    {
210 1
        $recommendedMovie = new MovieRecommendation($user, $this, $recommendedMovie);
211 1
        $this->recommendations->add($recommendedMovie);
212
213 1
        return $this;
214
    }
215
216
    public function removeAllRecommendations()
217
    {
218
        $this->recommendations->clear();
219
220
        return $this;
221
    }
222
223
    public function updateTmdb(MovieTMDB $tmdb)
224
    {
225
        $this->tmdb = $tmdb;
226
    }
227
228
    /**
229
     * @param string $imdbId
230
     *
231
     * @return Movie
232
     */
233 3
    private function setImdbId(?string $imdbId)
234
    {
235 3
        $this->imdbId = $imdbId;
236
237 3
        return $this;
238
    }
239
240
    /**
241
     * @param int $runtime
242
     *
243
     * @return Movie
244
     */
245 3
    private function setRuntime(int $runtime)
246
    {
247 3
        $this->runtime = $runtime;
248
249 3
        return $this;
250
    }
251
252
    /**
253
     * @param int $budget
254
     *
255
     * @return Movie
256
     */
257 3
    private function setBudget(int $budget)
258
    {
259 3
        $this->budget = $budget;
260
261 3
        return $this;
262
    }
263
264
    /**
265
     * @param \DateTimeInterface $releaseDate
266
     *
267
     * @return Movie
268
     */
269 3
    private function setReleaseDate(\DateTimeInterface $releaseDate)
270
    {
271 3
        $this->releaseDate = $releaseDate;
272
273 3
        return $this;
274
    }
275
276
    /**
277
     * @return string
278
     */
279 9
    public function getOriginalTitle()
280
    {
281 9
        return $this->originalTitle;
282
    }
283
284
    /**
285
     * @param string $title
286
     * @return $this
287
     */
288
    public function changeOriginalTitle(string $title)
289
    {
290
        $this->originalTitle = $title;
291
292
        return $this;
293
    }
294
295
    /**
296
     * @return mixed
297
     */
298 9
    public function getOriginalPosterUrl()
299
    {
300 9
        return $this->originalPosterUrl;
301
    }
302
303
    /**
304
     * @return MovieTMDB
305
     */
306 10
    public function getTmdb()
307
    {
308 10
        return $this->tmdb;
309
    }
310
311
    /**
312
     * @return mixed
313
     */
314 1
    public function getImdbId()
315
    {
316 1
        return $this->imdbId;
317
    }
318
319
    /**
320
     * @return mixed
321
     */
322 1
    public function getRuntime()
323
    {
324 1
        return $this->runtime;
325
    }
326
327
    /**
328
     * @return mixed
329
     */
330 1
    public function getBudget()
331
    {
332 1
        return $this->budget;
333
    }
334
335
    /**
336
     * @return mixed
337
     */
338 9
    public function getReleaseDate()
339
    {
340 9
        return $this->releaseDate;
341
    }
342
343 3
    public function getUserWatchedMovie()
344
    {
345 3
        return $this->userWatchedMovie;
346
    }
347
348 9
    public function getGuestWatchedMovie()
349
    {
350 9
        return $this->guestWatchedMovie;
351
    }
352
353 9
    public function isWatched()
354
    {
355 9
        $this->isWatched = ($this->userWatchedMovie || $this->guestWatchedMovie) ? true : false;
356
357 9
        return $this->isWatched;
358
    }
359
360
    public function __toString()
361
    {
362
        return $this->getId().' | '.$this->getOriginalTitle().' | TMDB: '.$this->getTmdb()->getId();
363
    }
364
}
365