Completed
Push — master ( 7c0075...645752 )
by Kamil
18:52
created

Product::getVariantSelectionMethodLabels()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 4
nop 0
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
namespace Sylius\Component\Core\Model;
13
14
use Doctrine\Common\Collections\ArrayCollection;
15
use Doctrine\Common\Collections\Collection;
16
use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface;
17
use Sylius\Component\Product\Model\Product as BaseProduct;
18
use Sylius\Component\Review\Model\ReviewInterface;
19
use Sylius\Component\Shipping\Model\ShippingCategoryInterface;
20
use Sylius\Component\Taxonomy\Model\TaxonInterface as BaseTaxonInterface;
21
22
/**
23
 * @author Paweł Jędrzejewski <[email protected]>
24
 * @author Gonzalo Vilaseca <[email protected]>
25
 * @author Anna Walasek <[email protected]>
26
 */
27
class Product extends BaseProduct implements ProductInterface, ReviewableProductInterface
28
{
29
    /**
30
     * @var string
31
     */
32
    protected $variantSelectionMethod;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $variantSelectionMethod exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
33
34
    /**
35
     * @var Collection|ProductTaxonInterface[]
36
     */
37
    protected $productTaxons;
38
39
    /**
40
     * @var ShippingCategoryInterface
41
     */
42
    protected $shippingCategory;
43
44
    /**
45
     * @var ChannelInterface[]|Collection
46
     */
47
    protected $channels;
48
49
    /**
50
     * @var BaseTaxonInterface
51
     */
52
    protected $mainTaxon;
53
54
    /**
55
     * @var Collection|ReviewInterface[]
56
     */
57
    protected $reviews;
58
59
    /**
60
     * @var float
61
     */
62
    protected $averageRating = 0;
63
64
    /**
65
     * @var Collection|ImageInterface[]
66
     */
67
    protected $images;
68
69
    public function __construct()
70
    {
71
        parent::__construct();
72
73
        $this->productTaxons = new ArrayCollection();
74
        $this->channels = new ArrayCollection();
75
        $this->reviews = new ArrayCollection();
76
        $this->images = new ArrayCollection();
77
78
        $this->variantSelectionMethod = self::VARIANT_SELECTION_CHOICE;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function getVariantSelectionMethod()
85
    {
86
        return $this->variantSelectionMethod;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function setVariantSelectionMethod($variantSelectionMethod)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $variantSelectionMethod exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
93
    {
94
        if (!in_array($variantSelectionMethod, [self::VARIANT_SELECTION_CHOICE, self::VARIANT_SELECTION_MATCH])) {
95
            throw new \InvalidArgumentException(sprintf('Wrong variant selection method "%s" given.', $variantSelectionMethod));
96
        }
97
98
        $this->variantSelectionMethod = $variantSelectionMethod;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function isVariantSelectionMethodChoice()
105
    {
106
        return self::VARIANT_SELECTION_CHOICE === $this->variantSelectionMethod;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getVariantSelectionMethodLabel()
113
    {
114
        $labels = self::getVariantSelectionMethodLabels();
115
116
        return $labels[$this->variantSelectionMethod];
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getProductTaxons()
123
    {
124
        return $this->productTaxons;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function addProductTaxon(ProductTaxonInterface $productTaxon)
131
    {
132
        if (!$this->hasProductTaxon($productTaxon)) {
133
            $this->productTaxons->add($productTaxon);
134
        }
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function removeProductTaxon(ProductTaxonInterface $productTaxon)
141
    {
142
        if ($this->hasProductTaxon($productTaxon)) {
143
            $this->productTaxons->removeElement($productTaxon);
144
        }
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function hasProductTaxon(ProductTaxonInterface $productTaxon)
151
    {
152
        return $this->productTaxons->contains($productTaxon);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function filterProductTaxonsByTaxon(TaxonInterface $taxon)
159
    {
160
        return $this->productTaxons->filter(function ($productTaxon) use ($taxon) {
161
             return $taxon === $productTaxon->getTaxon();
162
        });
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getShippingCategory()
169
    {
170
        return $this->shippingCategory;
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function setShippingCategory(ShippingCategoryInterface $category = null)
177
    {
178
        $this->shippingCategory = $category;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function getChannels()
185
    {
186
        return $this->channels;
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function addChannel(BaseChannelInterface $channel)
193
    {
194
        if (!$this->hasChannel($channel)) {
195
            $this->channels->add($channel);
196
        }
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function removeChannel(BaseChannelInterface $channel)
203
    {
204
        if ($this->hasChannel($channel)) {
205
            $this->channels->removeElement($channel);
206
        }
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function hasChannel(BaseChannelInterface $channel)
213
    {
214
        return $this->channels->contains($channel);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public static function getVariantSelectionMethodLabels()
221
    {
222
        return [
223
            self::VARIANT_SELECTION_CHOICE => 'Variant choice',
224
            self::VARIANT_SELECTION_MATCH => 'Options matching',
225
        ];
226
    }
227
228
    /**
229
     * {@inheritdoc}
230
     */
231
    public function getShortDescription()
232
    {
233
        return $this->getTranslation()->getShortDescription();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method getShortDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
234
    }
235
236
    /**
237
     * {@inheritdoc}
238
     */
239
    public function setShortDescription($shortDescription)
240
    {
241
        $this->getTranslation()->setShortDescription($shortDescription);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Resourc...el\TranslationInterface as the method setShortDescription() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\ProductTranslation.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
242
    }
243
244
    /**
245
     * {@inheritdoc}
246
     */
247
    public function getMainTaxon()
248
    {
249
        return $this->mainTaxon;
250
    }
251
252
    /**
253
     * {@inheritdoc}
254
     */
255
    public function setMainTaxon(TaxonInterface $mainTaxon = null)
256
    {
257
        $this->mainTaxon = $mainTaxon;
258
    }
259
260
    /**
261
     * {@inheritdoc}
262
     */
263
    public function getReviews()
264
    {
265
        return $this->reviews;
266
    }
267
268
    /**
269
     * {@inheritdoc}
270
     */
271
    public function addReview(ReviewInterface $review)
272
    {
273
        $this->reviews->add($review);
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279
    public function removeReview(ReviewInterface $review)
280
    {
281
        $this->reviews->remove($review);
0 ignored issues
show
Documentation introduced by
$review is of type object<Sylius\Component\...\Model\ReviewInterface>, but the function expects a string|integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
282
    }
283
284
    /**
285
     * {@inheritdoc}
286
     */
287
    public function getAverageRating()
288
    {
289
        return $this->averageRating;
290
    }
291
292
    /**
293
     * {@inheritdoc}
294
     */
295
    public function setAverageRating($averageRating)
296
    {
297
        $this->averageRating = $averageRating;
298
    }
299
300
    /**
301
     * {@inheritdoc}
302
     */
303
    public function getPrice()
304
    {
305
        if ($this->variants->isEmpty()) {
306
            return null;
307
        }
308
309
        return $this->variants->first()->getPrice();
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315
    public function getImages()
316
    {
317
        return $this->images;
318
    }
319
320
    /**
321
     * {@inheritdoc}
322
     */
323
    public function getImageByCode($code)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
324
    {
325
        foreach ($this->images as $image) {
326
            if ($code === $image->getCode()) {
327
                return $image;
328
            }
329
        }
330
331
        return null;
332
    }
333
334
    /**
335
     * {@inheritdoc}
336
     */
337
    public function hasImages()
338
    {
339
        return !$this->images->isEmpty();
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345
    public function hasImage(ImageInterface $image)
346
    {
347
        return $this->images->contains($image);
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function addImage(ImageInterface $image)
354
    {
355
        $image->setOwner($this);
356
        $this->images->add($image);
357
    }
358
359
    /**
360
     * {@inheritdoc}
361
     */
362
    public function removeImage(ImageInterface $image)
363
    {
364
        if ($this->hasImage($image)) {
365
            $image->setOwner(null);
366
            $this->images->removeElement($image);
367
        }
368
    }
369
370
    /**
371
     * {@inheritdoc}
372
     */
373
    protected function createTranslation()
374
    {
375
        return new ProductTranslation();
376
    }
377
}
378