1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Model; |
6
|
|
|
|
7
|
|
|
use Cake\Chronos\Date; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
use Doctrine\ORM\Mapping as ORM; |
11
|
|
|
use GraphQL\Doctrine\Annotation as API; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* An item that can be booked by a user |
15
|
|
|
* |
16
|
|
|
* @ORM\Entity(repositoryClass="\Application\Repository\ProductRepository") |
17
|
|
|
*/ |
18
|
|
|
class Product extends AbstractProduct |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var null|int |
22
|
|
|
* |
23
|
|
|
* @ORM\Column(type="smallint", nullable=true, options={"unsigned" = true}) |
24
|
|
|
*/ |
25
|
|
|
private $readingDuration; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Date |
29
|
|
|
* |
30
|
|
|
* @ORM\Column(type="date", nullable=true) |
31
|
|
|
*/ |
32
|
|
|
private $releaseDate; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var null|int |
36
|
|
|
* |
37
|
|
|
* @ORM\Column(type="smallint", nullable=true, unique=true, options={"unsigned" = true}) |
38
|
|
|
*/ |
39
|
|
|
private $reviewNumber; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var null|File |
43
|
|
|
* @ORM\OneToOne(targetEntity="File", orphanRemoval=true) |
44
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
45
|
|
|
*/ |
46
|
|
|
private $file; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var null|Product |
50
|
|
|
* |
51
|
|
|
* @ORM\ManyToOne(targetEntity="Product") |
52
|
|
|
* @ORM\JoinColumns({ |
53
|
|
|
* @ORM\JoinColumn(onDelete="CASCADE") |
54
|
|
|
* }) |
55
|
|
|
*/ |
56
|
|
|
private $review; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var Collection |
60
|
|
|
* |
61
|
|
|
* @ORM\ManyToMany(targetEntity="ProductTag", mappedBy="products") |
62
|
|
|
*/ |
63
|
|
|
private $productTags; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var Collection |
67
|
|
|
* |
68
|
|
|
* @ORM\ManyToMany(targetEntity="Product") |
69
|
|
|
*/ |
70
|
|
|
private $relatedProducts; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var bool |
74
|
|
|
* |
75
|
|
|
* @ORM\Column(type="boolean", options={"default" = 0}) |
76
|
|
|
*/ |
77
|
|
|
private $isHighlighted = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Constructor |
81
|
|
|
*/ |
82
|
20 |
|
public function __construct(string $name = '') |
83
|
|
|
{ |
84
|
20 |
|
parent::__construct($name); |
85
|
20 |
|
$this->productTags = new ArrayCollection(); |
86
|
20 |
|
$this->relatedProducts = new ArrayCollection(); |
87
|
20 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Collection |
91
|
|
|
*/ |
92
|
|
|
public function getProductTags(): Collection |
93
|
|
|
{ |
94
|
|
|
return $this->productTags; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Notify the user that it has a new productTag. |
99
|
|
|
* This should only be called by ProductTag::addUser() |
100
|
|
|
* |
101
|
|
|
* @param ProductTag $productTag |
102
|
|
|
*/ |
103
|
|
|
public function productTagAdded(ProductTag $productTag): void |
104
|
|
|
{ |
105
|
|
|
$this->productTags->add($productTag); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Notify the user that it a productTag was removed. |
110
|
|
|
* This should only be called by ProductTag::removeUser() |
111
|
|
|
* |
112
|
|
|
* @param ProductTag $productTag |
113
|
|
|
*/ |
114
|
|
|
public function productTagRemoved(ProductTag $productTag): void |
115
|
|
|
{ |
116
|
|
|
$this->productTags->removeElement($productTag); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Reading duration in minutes |
121
|
|
|
* |
122
|
|
|
* @return null|int |
123
|
|
|
*/ |
124
|
|
|
public function getReadingDuration(): ?int |
125
|
|
|
{ |
126
|
|
|
return $this->readingDuration; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Reading duration in minutes |
131
|
|
|
* |
132
|
|
|
* @param null|int $readingDuration |
133
|
|
|
*/ |
134
|
|
|
public function setReadingDuration(?int $readingDuration): void |
135
|
|
|
{ |
136
|
|
|
$this->readingDuration = $readingDuration; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return null|Date |
141
|
|
|
*/ |
142
|
|
|
public function getReleaseDate(): ?Date |
143
|
|
|
{ |
144
|
|
|
return $this->releaseDate; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param null|Date $releaseDate |
149
|
|
|
*/ |
150
|
1 |
|
public function setReleaseDate(?Date $releaseDate): void |
151
|
|
|
{ |
152
|
1 |
|
$this->releaseDate = $releaseDate; |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return null|int |
157
|
|
|
*/ |
158
|
1 |
|
public function getReviewNumber(): ?int |
159
|
|
|
{ |
160
|
1 |
|
return $this->reviewNumber; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param null|int $reviewNumber |
165
|
|
|
*/ |
166
|
1 |
|
public function setReviewNumber(?int $reviewNumber): void |
167
|
|
|
{ |
168
|
1 |
|
$this->reviewNumber = $reviewNumber; |
169
|
1 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return null|File |
173
|
|
|
*/ |
174
|
|
|
public function getFile(): ?File |
175
|
|
|
{ |
176
|
|
|
return $this->file; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param null|File $file |
181
|
|
|
*/ |
182
|
1 |
|
public function setFile(?File $file): void |
183
|
|
|
{ |
184
|
|
|
// We must trigger lazy loading, otherwise Doctrine will seriously |
185
|
|
|
// mess up lifecycle callbacks and delete unrelated image on disk |
186
|
1 |
|
if ($this->file) { |
187
|
|
|
$this->file->getFilename(); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
$this->file = $file; |
191
|
1 |
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get related products |
195
|
|
|
* |
196
|
|
|
* @API\Field(type="Product[]") |
197
|
|
|
* |
198
|
|
|
* @return Collection |
199
|
|
|
*/ |
200
|
1 |
|
public function getRelatedProducts(): Collection |
201
|
|
|
{ |
202
|
1 |
|
return $this->relatedProducts; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Add related product |
207
|
|
|
* |
208
|
|
|
* @param Product $product |
209
|
|
|
*/ |
210
|
2 |
|
public function addRelatedProduct(self $product): void |
211
|
|
|
{ |
212
|
2 |
|
if ($product === $this) { |
213
|
1 |
|
throw new \InvalidArgumentException('A product cannot be related to itself'); |
214
|
|
|
} |
215
|
|
|
|
216
|
1 |
|
if (!$this->relatedProducts->contains($product)) { |
217
|
1 |
|
$this->relatedProducts[] = $product; |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
if (!$product->relatedProducts->contains($this)) { |
221
|
1 |
|
$product->relatedProducts[] = $this; |
222
|
|
|
} |
223
|
1 |
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Remove related product |
227
|
|
|
* |
228
|
|
|
* @param Product $product |
229
|
|
|
*/ |
230
|
1 |
|
public function removeRelatedProduct(self $product): void |
231
|
|
|
{ |
232
|
1 |
|
$this->relatedProducts->removeElement($product); |
233
|
1 |
|
$product->relatedProducts->removeElement($this); |
234
|
1 |
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return null|Product |
238
|
|
|
*/ |
239
|
|
|
public function getReview(): ?self |
240
|
|
|
{ |
241
|
|
|
return $this->review; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @param null|Product $review |
246
|
|
|
*/ |
247
|
|
|
public function setReview(?self $review): void |
248
|
|
|
{ |
249
|
|
|
$this->review = $review; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Whether this product has more visibility |
254
|
|
|
* |
255
|
|
|
* @return bool |
256
|
|
|
*/ |
257
|
|
|
public function isHighlighted(): bool |
258
|
|
|
{ |
259
|
|
|
return $this->isHighlighted; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
*Whether this product has more visibility |
264
|
|
|
* |
265
|
|
|
* @param bool $isHighlighted |
266
|
|
|
*/ |
267
|
|
|
public function setIsHighlighted(bool $isHighlighted): void |
268
|
|
|
{ |
269
|
|
|
$this->isHighlighted = $isHighlighted; |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|