Completed
Push — master ( 66f2ad...196fcb )
by Valentyn
03:19
created

Movie::addSimilarMovie()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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