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