Book::setAuthor()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
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
/*
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 App\Traits\IdColumnTrait;
15
use App\Traits\TimeAwareTrait;
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Doctrine\ORM\Mapping as ORM;
19
use JMS\Serializer\Annotation as JMS;
20
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
21
use Symfony\Component\Validator\Constraints as Assert;
22
23
/**
24
 * @ORM\Entity(repositoryClass="App\Repository\BookRepository")
25
 *
26
 * @UniqueEntity({"isbn"}, message="Book with this ISBN already exists,")
27
 *
28
 * @JMS\ExclusionPolicy("ALL")
29
 */
30
class Book
31
{
32
    use IdColumnTrait;
33
    use TimeAwareTrait;
34
35
    /**
36
     * @var string
37
     *
38
     * @ORM\Column(type="string")
39
     *
40
     * @Assert\NotBlank
41
     *
42
     * @JMS\Expose
43
     */
44
    protected $isbn;
45
46
    /**
47
     * @var string
48
     *
49
     * @ORM\Column(type="string")
50
     *
51
     * @Assert\NotBlank
52
     *
53
     * @JMS\Expose
54
     */
55
    protected $title;
56
57
    /**
58
     * @var string
59
     *
60
     * @ORM\Column(type="text")
61
     *
62
     * @Assert\NotBlank
63
     *
64
     * @JMS\Expose
65
     */
66
    protected $description;
67
68
    /**
69
     * @var string
70
     *
71
     * @ORM\Column(type="text")
72
     *
73
     * @Assert\NotBlank
74
     *
75
     * @JMS\Expose
76
     */
77
    protected $author;
78
79
    /**
80
     * @var \DateTimeInterface
81
     *
82
     * @ORM\Column(type="datetime")
83
     *
84
     * @Assert\NotBlank
85
     *
86
     * @JMS\Expose
87
     */
88
    protected $publicationDate;
89
90
    /**
91
     * @var ArrayCollection|Review[]
92
     *
93
     * @ORM\OneToMany(targetEntity="App\Entity\Review", mappedBy="book", cascade={"all"})
94
     *
95
     * @JMS\Expose
96
     * @JMS\Groups("reviews")
97
     */
98
    protected $reviews;
99
100
    /**
101
     * @var ArrayCollection|User[]
102
     *
103
     * @ORM\ManyToMany(targetEntity="App\Entity\User", mappedBy="books", cascade={"all"})
104
     *
105
     * @JMS\Expose
106
     * @JMS\Groups("readers")
107
     */
108
    protected $readers;
109
110
    /**
111
     * Book constructor.
112
     */
113 2
    public function __construct()
114
    {
115 2
        $this->reviews = new ArrayCollection();
116 2
        $this->readers = new ArrayCollection();
117 2
    }
118
119
    /**
120
     * @return string|null
121
     */
122 3
    public function getIsbn(): ?string
123
    {
124 3
        return $this->isbn;
125
    }
126
127
    /**
128
     * @param string $isbn
129
     *
130
     * @return Book
131
     */
132 3
    public function setIsbn(string $isbn): self
133
    {
134 3
        $this->isbn = $isbn;
135
136 3
        return $this;
137
    }
138
139
    /**
140
     * @return string|null
141
     */
142 3
    public function getTitle(): ?string
143
    {
144 3
        return $this->title;
145
    }
146
147
    /**
148
     * @param string $title
149
     *
150
     * @return Book
151
     */
152 2
    public function setTitle(string $title): self
153
    {
154 2
        $this->title = $title;
155
156 2
        return $this;
157
    }
158
159
    /**
160
     * @return string|null
161
     */
162 3
    public function getDescription(): ?string
163
    {
164 3
        return $this->description;
165
    }
166
167
    /**
168
     * @param string $description
169
     *
170
     * @return Book
171
     */
172 2
    public function setDescription(string $description): self
173
    {
174 2
        $this->description = $description;
175
176 2
        return $this;
177
    }
178
179
    /**
180
     * @return string|null
181
     */
182 3
    public function getAuthor(): ?string
183
    {
184 3
        return $this->author;
185
    }
186
187
    /**
188
     * @param string $author
189
     *
190
     * @return Book
191
     */
192 2
    public function setAuthor(string $author): self
193
    {
194 2
        $this->author = $author;
195
196 2
        return $this;
197
    }
198
199
    /**
200
     * @return \DateTimeInterface|null
201
     */
202 3
    public function getPublicationDate(): ?\DateTimeInterface
203
    {
204 3
        return $this->publicationDate;
205
    }
206
207
    /**
208
     * @param \DateTimeInterface $publicationDate
209
     *
210
     * @return Book
211
     */
212 2
    public function setPublicationDate(?\DateTimeInterface $publicationDate): self
213
    {
214 2
        $this->publicationDate = $publicationDate;
215
216 2
        return $this;
217
    }
218
219
    /**
220
     * @return Collection|Review[]
221
     */
222
    public function getReviews(): Collection
223
    {
224
        return $this->reviews;
225
    }
226
227
    /**
228
     * @param Review $review
229
     *
230
     * @return Book
231
     */
232
    public function addReview(Review $review): self
233
    {
234
        if (!$this->reviews->contains($review)) {
235
            $this->reviews[] = $review;
236
            $review->setBook($this);
237
        }
238
239
        return $this;
240
    }
241
242
    /**
243
     * @param Review $review
244
     *
245
     * @return Book
246
     */
247
    public function removeReview(Review $review): self
248
    {
249
        if ($this->reviews->contains($review)) {
250
            $this->reviews->removeElement($review);
251
            // set the owning side to null (unless already changed)
252
            if ($review->getBook() === $this) {
253
                $review->setBook(null);
254
            }
255
        }
256
257
        return $this;
258
    }
259
260
    /**
261
     * @return Collection
262
     */
263
    public function getReaders(): Collection
264
    {
265
        return $this->readers;
266
    }
267
268
    /**
269
     * @param User $reader
270
     *
271
     * @return Book
272
     */
273
    public function addReader(User $reader): self
274
    {
275
        if (!$this->readers->contains($reader)) {
276
            $this->readers[] = $reader;
277
            $reader->addBook($this);
278
        }
279
280
        return $this;
281
    }
282
283
    /**
284
     * @param User $reader
285
     *
286
     * @return Book
287
     */
288
    public function removeReader(User $reader): self
289
    {
290
        if ($this->readers->contains($reader)) {
291
            $this->readers->removeElement($reader);
292
            $reader->removeBook($this);
293
        }
294
295
        return $this;
296
    }
297
}
298