Completed
Push — translation-class ( c4acc3 )
by Kamil
18:30
created

Product::createTranslation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addTranslation, getTranslation, getTranslations, hasTranslation, removeTranslation, setCurrentLocale, setFallbackLocale
Loading history...
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|BaseTaxonInterface[]
36
     */
37
    protected $taxons;
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->taxons = 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 getTaxons($rootTaxonCode = null)
123
    {
124
        if (null !== $rootTaxonCode) {
125
            return $this->taxons->filter(function (BaseTaxonInterface $taxon) use ($rootTaxonCode) {
126
                return $rootTaxonCode === strtolower($taxon->getRoot()->getCode());
127
            });
128
        }
129
130
        return $this->taxons;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function addTaxon(BaseTaxonInterface $taxon)
137
    {
138
        if (!$this->hasTaxon($taxon)) {
139
            $this->taxons->add($taxon);
140
        }
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function removeTaxon(BaseTaxonInterface $taxon)
147
    {
148
        if ($this->hasTaxon($taxon)) {
149
            $this->taxons->removeElement($taxon);
150
        }
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function hasTaxon(BaseTaxonInterface $taxon)
157
    {
158
        return $this->taxons->contains($taxon);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function getShippingCategory()
165
    {
166
        return $this->shippingCategory;
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function setShippingCategory(ShippingCategoryInterface $category = null)
173
    {
174
        $this->shippingCategory = $category;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180
    public function getChannels()
181
    {
182
        return $this->channels;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function addChannel(BaseChannelInterface $channel)
189
    {
190
        if (!$this->hasChannel($channel)) {
191
            $this->channels->add($channel);
192
        }
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function removeChannel(BaseChannelInterface $channel)
199
    {
200
        if ($this->hasChannel($channel)) {
201
            $this->channels->removeElement($channel);
202
        }
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function hasChannel(BaseChannelInterface $channel)
209
    {
210
        return $this->channels->contains($channel);
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public static function getVariantSelectionMethodLabels()
217
    {
218
        return [
219
            self::VARIANT_SELECTION_CHOICE => 'Variant choice',
220
            self::VARIANT_SELECTION_MATCH => 'Options matching',
221
        ];
222
    }
223
224
    /**
225
     * {@inheritdoc}
226
     */
227
    public function getShortDescription()
228
    {
229
        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...
230
    }
231
232
    /**
233
     * {@inheritdoc}
234
     */
235
    public function setShortDescription($shortDescription)
236
    {
237
        $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...
238
    }
239
240
    /**
241
     * {@inheritdoc}
242
     */
243
    public function getMainTaxon()
244
    {
245
        return $this->mainTaxon;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251
    public function setMainTaxon(TaxonInterface $mainTaxon = null)
252
    {
253
        $this->mainTaxon = $mainTaxon;
254
    }
255
256
    /**
257
     * {@inheritdoc}
258
     */
259
    public function getReviews()
260
    {
261
        return $this->reviews;
262
    }
263
264
    /**
265
     * {@inheritdoc}
266
     */
267
    public function addReview(ReviewInterface $review)
268
    {
269
        $this->reviews->add($review);
270
    }
271
272
    /**
273
     * {@inheritdoc}
274
     */
275
    public function removeReview(ReviewInterface $review)
276
    {
277
        $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...
278
    }
279
280
    /**
281
     * {@inheritdoc}
282
     */
283
    public function getAverageRating()
284
    {
285
        return $this->averageRating;
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function setAverageRating($averageRating)
292
    {
293
        $this->averageRating = $averageRating;
294
    }
295
296
    /**
297
     * {@inheritdoc}
298
     */
299
    public function getPrice()
300
    {
301
        if ($this->variants->isEmpty()) {
302
            return null;
303
        }
304
305
        return $this->variants->first()->getPrice();
306
    }
307
308
    /**
309
     * {@inheritdoc}
310
     */
311
    public function getImages()
312
    {
313
        return $this->images;
314
    }
315
316
    /**
317
     * {@inheritdoc}
318
     */
319
    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...
320
    {
321
        foreach ($this->images as $image) {
322
            if ($code === $image->getCode()) {
323
                return $image;
324
            }
325
        }
326
327
        return null;
328
    }
329
330
    /**
331
     * {@inheritdoc}
332
     */
333
    public function hasImages()
334
    {
335
        return !$this->images->isEmpty();
336
    }
337
338
    /**
339
     * {@inheritdoc}
340
     */
341
    public function hasImage(ImageInterface $image)
342
    {
343
        return $this->images->contains($image);
344
    }
345
346
    /**
347
     * {@inheritdoc}
348
     */
349
    public function addImage(ImageInterface $image)
350
    {
351
        $image->setOwner($this);
352
        $this->images->add($image);
353
    }
354
355
    /**
356
     * {@inheritdoc}
357
     */
358
    public function removeImage(ImageInterface $image)
359
    {
360
        if ($this->hasImage($image)) {
361
            $image->setOwner(null);
362
            $this->images->removeElement($image);
363
        }
364
    }
365
366
    /**
367
     * {@inheritdoc}
368
     */
369
    protected function createTranslation()
370
    {
371
        return new ProductTranslation();
372
    }
373
}
374