Passed
Push — master ( 03c0bd...d9a7d5 )
by
unknown
05:08
created

CartItem::getTotalPromoPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Thelia\Model;
4
5
use Propel\Runtime\Connection\ConnectionInterface;
6
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7
use Thelia\Core\Event\Cart\CartEvent;
8
use Thelia\Core\Event\Cart\CartItemEvent;
9
use Thelia\Core\Event\TheliaEvents;
10
use Thelia\Model\Base\CartItem as BaseCartItem;
0 ignored issues
show
Bug introduced by
The type Thelia\Model\Base\CartItem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Thelia\TaxEngine\Calculator;
12
13
class CartItem extends BaseCartItem
14
{
15
    /** @var EventDispatcherInterface */
16
    protected $dispatcher;
17
18
    /**
19
     * @param EventDispatcherInterface $dispatcher
20
     */
21
    public function setDisptacher(EventDispatcherInterface $dispatcher)
22
    {
23
        $this->dispatcher = $dispatcher;
24
    }
25
26
    /**
27
     * @param ConnectionInterface|null $con
28
     * @return bool
29
     */
30
    public function preInsert(ConnectionInterface $con = null)
31
    {
32
        parent::preInsert($con);
33
34
        if ($this->dispatcher) {
35
            $cartItemEvent = new CartItemEvent($this);
36
            $this->dispatcher->dispatch(TheliaEvents::CART_ITEM_CREATE_BEFORE, $cartItemEvent);
37
        }
38
        return true;
39
    }
40
41
    /**
42
     * @param ConnectionInterface|null $con
43
     * @return bool
44
     */
45
    public function preUpdate(ConnectionInterface $con = null)
46
    {
47
        parent::preUpdate($con);
48
49
        if ($this->dispatcher) {
50
            $cartItemEvent = new CartItemEvent($this);
51
            $this->dispatcher->dispatch(TheliaEvents::CART_ITEM_UPDATE_BEFORE, $cartItemEvent);
52
        }
53
        return true;
54
    }
55
56
    /**
57
     * @param ConnectionInterface|null $con
58
     * @throws \Propel\Runtime\Exception\PropelException
59
     */
60
    public function postInsert(ConnectionInterface $con = null)
61
    {
62
        parent::postInsert($con);
63
64
        if ($this->dispatcher) {
65
            $cartEvent = new CartEvent($this->getCart());
66
67
            $this->dispatcher->dispatch(TheliaEvents::AFTER_CARTADDITEM, $cartEvent);
68
        }
69
    }
70
71
    /**
72
     * @param ConnectionInterface|null $con
73
     * @throws \Propel\Runtime\Exception\PropelException
74
     */
75
    public function postUpdate(ConnectionInterface $con = null)
76
    {
77
        parent::postUpdate($con);
78
79
        if ($this->dispatcher) {
80
            $cartEvent = new CartEvent($this->getCart());
81
82
            $this->dispatcher->dispatch(TheliaEvents::AFTER_CARTUPDATEITEM, $cartEvent);
83
        }
84
    }
85
86
    /**
87
     * @param $value
88
     * @return $this
89
     * @throws \Propel\Runtime\Exception\PropelException
90
     */
91
    public function updateQuantity($value)
92
    {
93
        $currentQuantity = $this->getQuantity();
94
95
        if ($value <= 0) {
96
            $value = $currentQuantity;
97
        }
98
99
        if (ConfigQuery::checkAvailableStock()) {
100
            $productSaleElements = $this->getProductSaleElements();
101
            $product = $productSaleElements->getProduct();
102
103
            if ($product->getVirtual() === 0) {
104
                if ($productSaleElements->getQuantity() < $value) {
105
                    $value = $currentQuantity;
106
                }
107
            }
108
        }
109
110
        $this->setQuantity($value);
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param $value
117
     * @return $this
118
     * @throws \Propel\Runtime\Exception\PropelException
119
     */
120
    public function addQuantity($value)
121
    {
122
        $currentQuantity = $this->getQuantity();
123
        $newQuantity = $currentQuantity + $value;
124
125
        if (ConfigQuery::checkAvailableStock()) {
126
            $productSaleElements = $this->getProductSaleElements();
127
            $product = $productSaleElements->getProduct();
128
129
            if ($product->getVirtual() === 0) {
130
                if ($productSaleElements->getQuantity() < $newQuantity) {
131
                    $newQuantity = $currentQuantity;
132
                }
133
            }
134
        }
135
136
        $this->setQuantity($newQuantity);
137
138
        return $this;
139
    }
140
141
    /**
142
     * @return float
143
     */
144
    public function getRealPrice()
145
    {
146
        return (float) ((int) $this->getPromo() === 1 ? $this->getPromoPrice() : $this->getPrice());
147
    }
148
149
    /**
150
     * @param ConnectionInterface|null $con
151
     * @param null $locale
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $locale is correct as it would always require null to be passed?
Loading history...
152
     * @return Product
153
     * @throws \Propel\Runtime\Exception\PropelException
154
     */
155
    public function getProduct(ConnectionInterface $con = null, $locale = null)
156
    {
157
        $product = parent::getProduct($con);
158
159
        $translation = $product->getTranslation($locale);
160
161
        if ($translation->isNew()) {
162
            if (ConfigQuery::getDefaultLangWhenNoTranslationAvailable() == Lang::REPLACE_BY_DEFAULT_LANGUAGE) {
163
                $locale = Lang::getDefaultLanguage()->getLocale();
164
            }
165
        }
166
167
        $product->setLocale($locale);
168
169
        return $product;
170
    }
171
172
    /**
173
     * @param Country $country
174
     * @param State|null $state
175
     * @return float
176
     * @throws \Propel\Runtime\Exception\PropelException
177
     */
178
    public function getRealTaxedPrice(Country $country, State $state = null)
179
    {
180
        return (int) $this->getPromo() === 1 ? $this->getTaxedPromoPrice($country, $state) : $this->getTaxedPrice($country, $state);
181
    }
182
183
    /**
184
     * @param Country $country
185
     * @param State|null $state
186
     * @return float
187
     * @throws \Propel\Runtime\Exception\PropelException
188
     */
189
    public function getTaxedPrice(Country $country, State $state = null)
190
    {
191
        $taxCalculator = new Calculator();
192
193
        return $taxCalculator->load($this->getProduct(), $country, $state)->getTaxedPrice($this->getPrice());
194
    }
195
196
    /**
197
     * @param Country $country
198
     * @param State|null $state
199
     * @return float
200
     * @throws \Propel\Runtime\Exception\PropelException
201
     */
202
    public function getTaxedPromoPrice(Country $country, State $state = null)
203
    {
204
        $taxCalculator = new Calculator();
205
206
        return $taxCalculator->load($this->getProduct(), $country, $state)->getTaxedPrice($this->getPromoPrice());
207
    }
208
209
    /**
210
     * @since Version 2.3
211
     * @param Country $country
212
     * @param State|null $state
213
     * @return float
214
     * @throws \Propel\Runtime\Exception\PropelException
215
     */
216
    public function getTotalRealTaxedPrice(Country $country, State $state = null)
217
    {
218
        return (int) $this->getPromo() === 1 ? $this->getTotalTaxedPromoPrice($country, $state) : $this->getTotalTaxedPrice($country, $state);
219
    }
220
221
    /**
222
     * @since Version 2.3
223
     * @param Country $country
224
     * @param State|null $state
225
     * @return float
226
     * @throws \Propel\Runtime\Exception\PropelException
227
     */
228
    public function getTotalTaxedPrice(Country $country, State $state = null)
229
    {
230
        return round($this->getTaxedPrice($country, $state) * $this->getQuantity(), 2);
231
    }
232
233
    /**
234
     * @since Version 2.3
235
     * @throws \Propel\Runtime\Exception\PropelException
236
     */
237
    public function getTotalTaxedPromoPrice(Country $country, State $state = null)
238
    {
239
        return round($this->getTaxedPromoPrice($country, $state) * $this->getQuantity(), 2);
240
    }
241
242
    /**
243
     * @since Version 2.4
244
     * @return float
245
     */
246
    public function getTotalPrice()
247
    {
248
        return round($this->getPrice() * $this->getQuantity(), 2);
249
    }
250
251
    /**
252
     * @since Version 2.4
253
     * @return float
254
     */
255
    public function getTotalPromoPrice()
256
    {
257
        return round($this->getPromoPrice() * $this->getQuantity(), 2);
258
    }
259
260
    /**
261
     * @since Version 2.4
262
     * @return float
263
     */
264
    public function getTotalRealPrice()
265
    {
266
        return round($this->getRealPrice() * $this->getQuantity(), 2);
267
    }
268
}
269