Completed
Push — master ( 0c2413...d80943 )
by one
07:40
created

User::addMovie()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
/*
4
 * (c) Lukasz D. Tulikowski <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace App\Entity;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Doctrine\ORM\Mapping as ORM;
17
use JMS\Serializer\Annotation as JMS;
18
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
19
use Symfony\Component\Security\Core\User\UserInterface;
20
21
/**
22
 * @ORM\HasLifecycleCallbacks
23
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
24
 * @ORM\Table(name="app_user")
25
 *
26
 * @UniqueEntity({"email"}, message="Email already exists.")
27
 *
28
 * @JMS\ExclusionPolicy("ALL")
29
 */
30
class User extends AbstractUser implements UserInterface
31
{
32
    /**
33
     * @var ArrayCollection|Book[]
34
     *
35
     * @ORM\ManyToMany(targetEntity="App\Entity\Book", inversedBy="readers", cascade={"persist"})
36
     *
37
     * @JMS\Groups("books")
38
     */
39
    protected $books;
40
41
    /**
42
     * @var ArrayCollection|Movie[]
43
     *
44
     * @ORM\ManyToMany(targetEntity="App\Entity\Movie", inversedBy="audience", cascade={"persist"})
45
     *
46
     * @JMS\Expose
47
     * @JMS\Groups("movies")
48
     */
49
    protected $movies;
50
51
    /**
52
     * @var ArrayCollection|Review[]
53
     *
54
     * @ORM\OneToMany(targetEntity="App\Entity\Review", mappedBy="author", cascade={"persist"})
55
     *
56
     * @JMS\Groups("reviews")
57
     */
58
    protected $reviews;
59
60
    /**
61
     * User constructor.
62
     */
63 2
    public function __construct()
64
    {
65 2
        $this->books = new ArrayCollection();
66 2
        $this->movies = new ArrayCollection();
67 2
        $this->reviews = new ArrayCollection();
68 2
    }
69
70
    /**
71
     * @return Book[]|Collection
72
     */
73 3
    public function getBooks(): Collection
74
    {
75 3
        return $this->books;
76
    }
77
78
    /**
79
     * @param Book $book
80
     *
81
     * @return User
82
     */
83
    public function addBook(Book $book): self
84
    {
85
        if (!$this->books->contains($book)) {
86
            $this->books[] = $book;
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * @param Book $book
94
     *
95
     * @return User
96
     */
97
    public function removeBook(Book $book): self
98
    {
99
        if ($this->books->contains($book)) {
100
            $this->books->removeElement($book);
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return Collection|Movie[]
108
     */
109 3
    public function getMovies(): Collection
110
    {
111 3
        return $this->movies;
112
    }
113
114
    /**
115
     * @param Movie $movie
116
     *
117
     * @return User
118
     */
119
    public function addMovie(Movie $movie): self
120
    {
121
        if (!$this->movies->contains($movie)) {
122
            $this->movies[] = $movie;
123
        }
124
125
        return $this;
126
    }
127
128
    /**
129
     * @param Movie $movie
130
     *
131
     * @return User
132
     */
133
    public function removeMovie(Movie $movie): self
134
    {
135
        if ($this->movies->contains($movie)) {
136
            $this->movies->removeElement($movie);
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return Collection|Review[]
144
     */
145 3
    public function getReviews(): Collection
146
    {
147 3
        return $this->reviews;
148
    }
149
150
    /**
151
     * @param Review $review
152
     *
153
     * @return User
154
     */
155
    public function addReview(Review $review): self
156
    {
157
        if (!$this->reviews->contains($review)) {
158
            $this->reviews[] = $review;
159
            $review->setAuthor($this);
160
        }
161
162
        return $this;
163
    }
164
165
    /**
166
     * @param Review $review
167
     *
168
     * @return User
169
     */
170
    public function removeReview(Review $review): self
171
    {
172
        if ($this->reviews->contains($review)) {
173
            $this->reviews->removeElement($review);
174
            // set the owning side to null (unless already changed)
175
            if ($review->getAuthor() === $this) {
176
                $review->setAuthor(null);
177
            }
178
        }
179
180
        return $this;
181
    }
182
}
183