Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

src/Sylius/Component/Core/Model/Product.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Component\Core\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface;
19
use Sylius\Component\Product\Model\Product as BaseProduct;
20
use Sylius\Component\Product\Model\ProductTranslationInterface as BaseProductTranslationInterface;
21
use Sylius\Component\Resource\Model\TranslationInterface;
22
use Sylius\Component\Review\Model\ReviewInterface;
23
use Sylius\Component\Taxonomy\Model\TaxonInterface as BaseTaxonInterface;
24
use Webmozart\Assert\Assert;
25
26
class Product extends BaseProduct implements ProductInterface, ReviewableProductInterface
27
{
28
    /**
29
     * @var string
30
     */
31
    protected $variantSelectionMethod = self::VARIANT_SELECTION_CHOICE;
32
33
    /**
34
     * @var Collection|ProductTaxonInterface[]
35
     */
36
    protected $productTaxons;
37
38
    /**
39
     * @var Collection|ChannelInterface[]
40
     */
41
    protected $channels;
42
43
    /**
44
     * @var BaseTaxonInterface
45
     */
46
    protected $mainTaxon;
47
48
    /**
49
     * @var Collection|ReviewInterface[]
50
     */
51
    protected $reviews;
52
53
    /**
54
     * @var float
55
     */
56
    protected $averageRating = 0;
57
58
    /**
59
     * @var Collection|ImageInterface[]
60
     */
61
    protected $images;
62
63
    public function __construct()
64
    {
65
        parent::__construct();
66
67
        $this->productTaxons = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type object<Doctrine\Common\C...ProductTaxonInterface>> of property $productTaxons.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
        $this->channels = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type object<Doctrine\Common\C...odel\ChannelInterface>> of property $channels.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
        $this->reviews = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type object<Doctrine\Common\C...Model\ReviewInterface>> of property $reviews.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70
        $this->images = new ArrayCollection();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Doctrine\Common\Collections\ArrayCollection() of type object<Doctrine\Common\C...ctions\ArrayCollection> is incompatible with the declared type object<Doctrine\Common\C...\Model\ImageInterface>> of property $images.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getVariantSelectionMethod(): string
77
    {
78
        return $this->variantSelectionMethod;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function setVariantSelectionMethod(?string $variantSelectionMethod): void
85
    {
86
        Assert::oneOf(
87
            $variantSelectionMethod,
88
            [self::VARIANT_SELECTION_CHOICE, self::VARIANT_SELECTION_MATCH],
89
            sprintf('Wrong variant selection method "%s" given.', $variantSelectionMethod)
90
        );
91
92
        $this->variantSelectionMethod = $variantSelectionMethod;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function isVariantSelectionMethodChoice(): bool
99
    {
100
        return self::VARIANT_SELECTION_CHOICE === $this->variantSelectionMethod;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function getVariantSelectionMethodLabel(): string
107
    {
108
        $labels = self::getVariantSelectionMethodLabels();
109
110
        return $labels[$this->variantSelectionMethod];
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getProductTaxons(): Collection
117
    {
118
        return $this->productTaxons;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function addProductTaxon(ProductTaxonInterface $productTaxon): void
125
    {
126
        if (!$this->hasProductTaxon($productTaxon)) {
127
            $this->productTaxons->add($productTaxon);
128
            $productTaxon->setProduct($this);
129
        }
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function removeProductTaxon(ProductTaxonInterface $productTaxon): void
136
    {
137
        if ($this->hasProductTaxon($productTaxon)) {
138
            $this->productTaxons->removeElement($productTaxon);
139
        }
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function hasProductTaxon(ProductTaxonInterface $productTaxon): bool
146
    {
147
        return $this->productTaxons->contains($productTaxon);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function getTaxons(): Collection
154
    {
155
        return $this->productTaxons->map(function (ProductTaxonInterface $productTaxon): TaxonInterface {
156
            return $productTaxon->getTaxon();
157
        });
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function hasTaxon(TaxonInterface $taxon): bool
164
    {
165
        return $this->getTaxons()->contains($taxon);
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function getChannels(): Collection
172
    {
173
        return $this->channels;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function addChannel(BaseChannelInterface $channel): void
180
    {
181
        if (!$this->hasChannel($channel)) {
182
            $this->channels->add($channel);
183
        }
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function removeChannel(BaseChannelInterface $channel): void
190
    {
191
        if ($this->hasChannel($channel)) {
192
            $this->channels->removeElement($channel);
193
        }
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function hasChannel(BaseChannelInterface $channel): bool
200
    {
201
        return $this->channels->contains($channel);
202
    }
203
204
    /**
205
     * {@inheritdoc}
206
     */
207
    public function getShortDescription(): ?string
208
    {
209
        return $this->getTranslation()->getShortDescription();
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function setShortDescription(?string $shortDescription): void
216
    {
217
        $this->getTranslation()->setShortDescription($shortDescription);
218
    }
219
220
    /**
221
     * {@inheritdoc}
222
     */
223
    public function getMainTaxon(): ?TaxonInterface
224
    {
225
        return $this->mainTaxon;
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function setMainTaxon(?TaxonInterface $mainTaxon): void
232
    {
233
        $this->mainTaxon = $mainTaxon;
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function getReviews(): Collection
240
    {
241
        return $this->reviews;
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function getAcceptedReviews(): Collection
248
    {
249
        return $this->reviews->filter(function (ReviewInterface $review): bool {
250
            return ReviewInterface::STATUS_ACCEPTED === $review->getStatus();
251
        });
252
    }
253
254
    /**
255
     * {@inheritdoc}
256
     */
257
    public function addReview(ReviewInterface $review): void
258
    {
259
        $this->reviews->add($review);
260
    }
261
262
    /**
263
     * {@inheritdoc}
264
     */
265
    public function removeReview(ReviewInterface $review): void
266
    {
267
        $this->reviews->removeElement($review);
268
    }
269
270
    /**
271
     * {@inheritdoc}
272
     */
273
    public function getAverageRating(): ?float
274
    {
275
        return $this->averageRating;
276
    }
277
278
    /**
279
     * {@inheritdoc}
280
     */
281
    public function setAverageRating(float $averageRating): void
282
    {
283
        $this->averageRating = $averageRating;
284
    }
285
286
    /**
287
     * {@inheritdoc}
288
     */
289
    public function getImages(): Collection
290
    {
291
        return $this->images;
292
    }
293
294
    /**
295
     * {@inheritdoc}
296
     */
297
    public function getImagesByType(string $type): Collection
298
    {
299
        return $this->images->filter(function (ImageInterface $image) use ($type): bool {
300
            return $type === $image->getType();
301
        });
302
    }
303
304
    /**
305
     * {@inheritdoc}
306
     */
307
    public function hasImages(): bool
308
    {
309
        return !$this->images->isEmpty();
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315
    public function hasImage(ImageInterface $image): bool
316
    {
317
        return $this->images->contains($image);
318
    }
319
320
    /**
321
     * {@inheritdoc}
322
     */
323
    public function addImage(ImageInterface $image): void
324
    {
325
        $image->setOwner($this);
326
        $this->images->add($image);
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332
    public function removeImage(ImageInterface $image): void
333
    {
334
        if ($this->hasImage($image)) {
335
            $image->setOwner(null);
336
            $this->images->removeElement($image);
337
        }
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public static function getVariantSelectionMethodLabels(): array
344
    {
345
        return [
346
            self::VARIANT_SELECTION_CHOICE => 'sylius.ui.variant_choice',
347
            self::VARIANT_SELECTION_MATCH => 'sylius.ui.options_matching',
348
        ];
349
    }
350
351
    /**
352
     * @param string|null $locale
353
     *
354
     * @return ProductTranslationInterface
355
     */
356
    public function getTranslation(?string $locale = null): TranslationInterface
357
    {
358
        return parent::getTranslation($locale);
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     */
364
    protected function createTranslation(): BaseProductTranslationInterface
365
    {
366
        return new ProductTranslation();
367
    }
368
}
369