Completed
Push — symfony3 ( 99c17a...a82edd )
by Kamil
17:55
created

ProductVariant::isInStock()   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
nc 1
cc 1
eloc 2
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 Sylius\Component\Core\Pricing\Calculators;
15
use Sylius\Component\Product\Model\ProductVariant as BaseVariant;
16
use Sylius\Component\Taxation\Model\TaxCategoryInterface;
17
use Webmozart\Assert\Assert;
18
19
/**
20
 * @author Paweł Jędrzejewski <[email protected]>
21
 */
22
class ProductVariant extends BaseVariant implements ProductVariantInterface
23
{
24
    /**
25
     * @var int
26
     */
27
    protected $price;
28
29
    /**
30
     * @var string
31
     */
32
    protected $pricingCalculator = Calculators::STANDARD;
33
34
    /**
35
     * @var array
36
     */
37
    protected $pricingConfiguration = [];
38
39
    /**
40
     * @var int
41
     */
42
    protected $onHold = 0;
43
44
    /**
45
     * @var int
46
     */
47
    protected $onHand = 0;
48
49
    /**
50
     * @var bool
51
     */
52
    protected $tracked = false;
53
54
    /**
55
     * @var float
56
     */
57
    protected $weight;
58
59
    /**
60
     * @var float
61
     */
62
    protected $width;
63
64
    /**
65
     * @var float
66
     */
67
    protected $height;
68
69
    /**
70
     * @var float
71
     */
72
    protected $depth;
73
74
    /**
75
     * @var TaxCategoryInterface
76
     */
77
    protected $taxCategory;
78
79
    /**
80
     * @return string
81
     */
82
    public function __toString()
83
    {
84
        $string = $this->getProduct()->getName();
85
86
        if (!$this->getOptionValues()->isEmpty()) {
87
            $string .= '(';
88
89
            foreach ($this->getOptionValues() as $option) {
90
                $string .= $option->getOption()->getName().': '.$option->getValue().', ';
91
            }
92
93
            $string = substr($string, 0, -2).')';
94
        }
95
96
        return $string;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getPrice()
103
    {
104
        return $this->price;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function setPrice($price)
111
    {
112
        if (!is_int($price)) {
113
            throw new \InvalidArgumentException('Price must be an integer.');
114
        }
115
116
        $this->price = $price;
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function getPricingCalculator()
123
    {
124
        return $this->pricingCalculator;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function setPricingCalculator($calculator)
131
    {
132
        $this->pricingCalculator = $calculator;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function getPricingConfiguration()
139
    {
140
        return $this->pricingConfiguration;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function setPricingConfiguration(array $configuration)
147
    {
148
        $this->pricingConfiguration = $configuration;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function isInStock()
155
    {
156
        return 0 < $this->onHand;
157
    }
158
159
    /**
160
     * {@inheritdoc}
161
     */
162
    public function getOnHold()
163
    {
164
        return $this->onHold;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    public function setOnHold($onHold)
171
    {
172
        $this->onHold = $onHold;
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178
    public function getOnHand()
179
    {
180
        return $this->onHand;
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function setOnHand($onHand)
187
    {
188
        $this->onHand = (0 > $onHand) ? 0 : $onHand;
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function isTracked()
195
    {
196
        return $this->tracked;
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     */
202
    public function setTracked($tracked)
203
    {
204
        Assert::boolean($tracked);
205
206
        $this->tracked = $tracked;
207
    }
208
209
    /**
210
     * {@inheritdoc}
211
     */
212
    public function getInventoryName()
213
    {
214
        return $this->getProduct()->getName();
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getShippingCategory()
221
    {
222
        return $this->getProduct()->getShippingCategory();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Sylius\Component\Product\Model\ProductInterface as the method getShippingCategory() does only exist in the following implementations of said interface: Sylius\Component\Core\Model\Product.

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...
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function getWeight()
229
    {
230
        return $this->weight;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function setWeight($weight)
237
    {
238
        $this->weight = $weight;
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function getWidth()
245
    {
246
        return $this->width;
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     */
252
    public function setWidth($width)
253
    {
254
        $this->width = $width;
255
    }
256
257
    /**
258
     * {@inheritdoc}
259
     */
260
    public function getHeight()
261
    {
262
        return $this->height;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268
    public function setHeight($height)
269
    {
270
        $this->height = $height;
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276
    public function getDepth()
277
    {
278
        return $this->depth;
279
    }
280
281
    /**
282
     * {@inheritdoc}
283
     */
284
    public function setDepth($depth)
285
    {
286
        $this->depth = $depth;
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292
    public function getShippingWeight()
293
    {
294
        return $this->getWeight();
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300
    public function getShippingWidth()
301
    {
302
        return $this->getWidth();
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308
    public function getShippingHeight()
309
    {
310
        return $this->getHeight();
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316
    public function getShippingDepth()
317
    {
318
        return $this->getDepth();
319
    }
320
321
    /**
322
     * {@inheritdoc}
323
     */
324
    public function getShippingVolume()
325
    {
326
        return $this->depth * $this->height * $this->width;
327
    }
328
329
    /**
330
     * {@inheritdoc}
331
     */
332
    public function getTaxCategory()
333
    {
334
        return $this->taxCategory;
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340
    public function setTaxCategory(TaxCategoryInterface $category = null)
341
    {
342
        $this->taxCategory = $category;
343
    }
344
}
345