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