Passed
Push — master ( a8b983...5b3176 )
by Adrien
10:48
created

Product::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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