CartService   D
last analyzed

Complexity

Total Complexity 80

Size/Duplication

Total Lines 680
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 95.83%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 680
ccs 230
cts 240
cp 0.9583
rs 4.5355
c 2
b 0
f 0
wmc 80
lcom 2
cbo 7

32 Methods

Rating   Name   Duplication   Size   Complexity  
A setCanAddProductType() 0 8 2
A unlock() 0 6 1
A lock() 0 6 1
A getCanAddProductType() 0 4 1
A addProduct() 0 7 1
A __construct() 0 16 2
A loadProductClassFromCartItem() 0 13 3
A save() 0 4 1
A isLocked() 0 4 1
A setPreOrderId() 0 6 1
A getPreOrderId() 0 4 1
A clear() 0 9 1
A getProductQuantity() 0 9 2
A loadProductClassFromCart() 0 15 2
C setProductQuantity() 0 77 13
A canAddProduct() 0 15 2
B canAddProductPayment() 0 38 6
B getCart() 0 41 6
B removeProduct() 0 26 3
A addError() 0 10 2
A upProductQuantity() 0 7 1
B getCartObj() 0 24 4
A downProductQuantity() 0 9 2
A getProductTypes() 0 13 2
A getErrors() 0 4 1
A getMessages() 0 4 1
A setMessage() 0 6 1
A getError() 0 4 1
A setError() 0 7 1
A getProductName() 0 15 3
A isProductDisplay() 0 12 2
C setProductLimit() 0 65 9

How to fix   Complexity   

Complex Class

Complex classes like CartService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use CartService, and based on these observations, apply Extract Interface, too.

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
25
namespace Eccube\Service;
26
27
use Doctrine\ORM\EntityManager;
28
use Eccube\Common\Constant;
29
use Eccube\Entity\CartItem;
30
use Eccube\Entity\Master\Disp;
31
use Eccube\Entity\ProductClass;
32
use Eccube\Exception\CartException;
33
use Symfony\Component\HttpFoundation\Session\Session;
34
35
class CartService
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
36
{
37
    /** @var \Eccube\Application */
38
    public $app;
39
40
    /**
41
     * @var Session
42
     */
43
    private $session;
44
45
    /**
46
     * @var EntityManager
47
     */
48
    private $entityManager;
49
50
    /**
51
     * @var \Eccube\Entity\Cart
52
     */
53
    private $cart;
54
55
    /**
56
     * @var \Eccube\Entity\BaseInfo
57
     */
58
    private $BaseInfo;
59
60
    /**
61
     * @var array
62
     */
63
    private $errors = array();
64
65
    private $ProductType = null;
66
67
    /**
68
     * @var array
69
     */
70
    private $messages = array();
71
72
    /**
73
     * @var array
74
     */
75
    private $error;
76
77 272
    public function __construct(\Eccube\Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
78
    {
79 272
        $this->app = $app;
80 272
        $this->session = $app['session'];
81 272
        $this->entityManager = $app['orm.em'];
82
83 272
        if ($this->session->has('cart')) {
84
            $this->cart = $this->session->get('cart');
85
        } else {
86 272
            $this->cart = new \Eccube\Entity\Cart();
87
        }
88
89 272
        $this->loadProductClassFromCart();
90
91 272
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
92
    }
93
94
    /**
95
     * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。
96
     */
97 272
    protected function loadProductClassFromCart()
98
    {
99
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
100 272
        $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete');
101 272
        $excludes = $softDeleteFilter->getExcludes();
102 272
        $softDeleteFilter->setExcludes(array(
103 272
            'Eccube\Entity\ProductClass',
104
        ));
105
106 272
        foreach ($this->cart->getCartItems() as $CartItem) {
107 272
            $this->loadProductClassFromCartItem($CartItem);
108
        }
109
110 272
        $softDeleteFilter->setExcludes($excludes);
111
    }
112
113
    /**
114
     * CartItem に対応する ProductClass を設定します。
115
     *
116
     * @param CartItem $CartItem
117
     */
118 132
    protected function loadProductClassFromCartItem(CartItem $CartItem)
119
    {
120
        $ProductClass = $this
121 132
            ->entityManager
122 132
            ->getRepository($CartItem->getClassName())
123 132
            ->find($CartItem->getClassId());
124
125 132
        $CartItem->setObject($ProductClass);
126
127 132
        if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) {
128
            $this->setCanAddProductType($ProductClass->getProductType());
129
        }
130
    }
131
132 171
    public function setCanAddProductType(\Eccube\Entity\Master\ProductType $ProductType)
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
introduced by
Missing function doc comment
Loading history...
133
    {
134 171
        if (is_null($this->ProductType)) {
135 171
            $this->ProductType = $ProductType;
136
        }
137
138 171
        return $this;
139
    }
140
141 164
    public function save()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
142
    {
143 164
        return $this->session->set('cart', $this->cart);
144
    }
145
146 14
    public function unlock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
147
    {
148 14
        $this->cart
149 14
            ->setLock(false)
150 14
            ->setPreOrderId(null);
151
    }
152
153 116
    public function lock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
154
    {
155 116
        $this->cart
156 116
            ->setLock(true)
157 116
            ->setPreOrderId(null);
158
    }
159
160
    /**
161
     * @return bool
162
     */
163 106
    public function isLocked()
164
    {
165 106
        return $this->cart->getLock();
166
    }
167
168
    /**
169
     * @param  string $pre_order_id
170
     * @return \Eccube\Service\CartService
171
     */
172 94
    public function setPreOrderId($pre_order_id)
173
    {
174 94
        $this->cart->setPreOrderId($pre_order_id);
175
176 94
        return $this;
177
    }
178
179
    /**
180
     * @return string
181
     */
182 93
    public function getPreOrderId()
183
    {
184 93
        return $this->cart->getPreOrderId();
185
    }
186
187
    /**
188
     * @return \Eccube\Service\CartService
189
     */
190 29
    public function clear()
191
    {
192 29
        $this->cart
193 29
            ->setPreOrderId(null)
194 29
            ->setLock(false)
195 29
            ->clearCartItems();
196
197 29
        return $this;
198
    }
199
200 1
    public function getCanAddProductType()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
201
    {
202 1
        return $this->ProductType;
203
    }
204
205
    /**
206
     *
207
     * @param  string $productClassId
0 ignored issues
show
introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
208
     * @param  integer $quantity
209
     * @return \Eccube\Service\CartService
210
     */
211 127
    public function addProduct($productClassId, $quantity = 1)
212
    {
213 127
        $quantity += $this->getProductQuantity($productClassId);
214 127
        $this->setProductQuantity($productClassId, $quantity);
215
216 126
        return $this;
217
    }
218
219
    /**
220
     * @param  string $productClassId
221
     * @return integer
222
     */
223 136
    public function getProductQuantity($productClassId)
224
    {
225 136
        $CartItem = $this->cart->getCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId);
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
226 136
        if ($CartItem) {
227 51
            return $CartItem->getQuantity();
228
        } else {
229 133
            return 0;
230
        }
231
    }
232
233
    /**
234
     * @param  \Eccube\Entity\ProductClass|integer $ProductClass
235
     * @param  integer $quantity
0 ignored issues
show
introduced by
Expected 29 spaces after parameter type; 1 found
Loading history...
236
     * @return \Eccube\Service\CartService
237
     * @throws CartException
238
     */
239 174
    public function setProductQuantity($ProductClass, $quantity)
240
    {
241 174
        if (!$ProductClass instanceof ProductClass) {
0 ignored issues
show
Bug introduced by
The class Eccube\Entity\ProductClass does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
242 169
            $ProductClass = $this->entityManager
243 169
                ->getRepository('Eccube\Entity\ProductClass')
244 169
                ->find($ProductClass);
245 169
            if (!$ProductClass) {
246 3
                throw new CartException('cart.product.delete');
247
            }
248
        }
249
250 171
        if (!$this->isProductDisplay($ProductClass)) {
251 4
            throw new CartException('cart.product.not.status');
252
        }
253
254 170
        $productName = $this->getProductName($ProductClass);
255
256
        // 商品種別に紐づく配送業者を取得
257 170
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType());
258
259 170
        if (count($deliveries) == 0) {
260
            // 商品種別が存在しなければエラー
261
            $this->removeProduct($ProductClass->getId());
262
            $this->addError('cart.product.not.producttype', $productName);
263
            throw new CartException('cart.product.not.producttype');
264
        }
265
266 170
        $this->setCanAddProductType($ProductClass->getProductType());
267
268 170
        if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) {
269 122
            if (!$this->canAddProduct($ProductClass->getId())) {
270
                // 複数配送対応でなければ商品種別が異なればエラー
271 122
                throw new CartException('cart.product.type.kind');
272
            }
273
        } else {
274
            // 複数配送の場合、同一支払方法がなければエラー
275 48
            if (!$this->canAddProductPayment($ProductClass->getProductType())) {
276 1
                throw new CartException('cart.product.payment.kind');
277
            }
278
        }
279
280 170
        $tmp_subtotal = 0;
281 170
        $tmp_quantity = 0;
282 170
        foreach ($this->getCart()->getCartItems() as $cartitem) {
283 54
            $pc = $cartitem->getObject();
284 54
            if ($pc->getId() != $ProductClass->getId()) {
285
                // 追加された商品以外のtotal priceをセット
286 170
                $tmp_subtotal += $cartitem->getTotalPrice();
287
            }
288
        }
289 170
        for ($i = 0; $i < $quantity; $i++) {
290 170
            $tmp_subtotal += $ProductClass->getPrice02IncTax();
291 170
            if ($tmp_subtotal > $this->app['config']['max_total_fee']) {
292 1
                $this->setError('cart.over.price_limit');
293 1
                break;
294
            }
295 169
            $tmp_quantity++;
296
        }
297 170
        if ($tmp_quantity == 0) {
298
            // 数量が0の場合、エラー
299 1
            throw new CartException('cart.over.price_limit');
300
        }
301
302
        // 制限数チェック
303 169
        $quantity = $this->setProductLimit($ProductClass, $productName, $tmp_quantity);
304
305 169
        $CartItem = new CartItem();
306
        $CartItem
307 169
            ->setClassName('Eccube\Entity\ProductClass')
308 169
            ->setClassId((string)$ProductClass->getId())
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
309 169
            ->setPrice($ProductClass->getPrice02IncTax())
310 169
            ->setQuantity($quantity);
311
312 169
        $this->cart->setCartItem($CartItem);
313
314 169
        return $this;
315
    }
316
317
    /**
318
     * @param  string $productClassId
319
     * @return boolean
320
     */
321 122
    public function canAddProduct($productClassId)
322
    {
323
        $ProductClass = $this
324 122
            ->entityManager
325 122
            ->getRepository('\Eccube\Entity\ProductClass')
326 122
            ->find($productClassId);
327
328 122
        if (!$ProductClass) {
329
            return false;
330
        }
331
332 122
        $ProductType = $ProductClass->getProductType();
333
334 122
        return $this->ProductType == $ProductType;
335
    }
336
337
    /**
338
     * @param \Eccube\Entity\Master\ProductType $ProductType
339
     * @return bool
340
     */
341 49
    public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType)
342
    {
343
        $deliveries = $this
344 49
            ->entityManager
345 49
            ->getRepository('\Eccube\Entity\Delivery')
346 49
            ->findBy(array('ProductType' => $ProductType));
347
348
        // 支払方法を取得
349 49
        $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries);
350
351 49
        if ($this->getCart()->getTotalPrice() < 1) {
352
            // カートになければ支払方法を全て設定
353 49
            $this->getCart()->setPayments($payments);
354
355 49
            return true;
356
        }
357
358
        // カートに存在している支払方法と追加された商品の支払方法チェック
359 45
        $arr = array();
360 45
        foreach ($payments as $payment) {
361 45
            foreach ($this->getCart()->getPayments() as $p) {
362 45
                if ($payment['id'] == $p['id']) {
363 44
                    $arr[] = $payment;
364 45
                    break;
365
                }
366
            }
367
        }
368
369 45
        if (count($arr) > 0) {
370 44
            $this->getCart()->setPayments($arr);
371
372 44
            return true;
373
        }
374
375
        // 支払条件に一致しない
376 1
        return false;
377
378
    }
379
380
    /**
381
     * カートブロックに表示するカートを取得します。
382
     * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。
383
     *
384
     * @return \Eccube\Entity\Cart
385
     */
386 81
    public function getCartObj()
387
    {
388
389 81
        foreach ($this->cart->getCartItems() as $CartItem) {
390
391
            /** @var \Eccube\Entity\ProductClass $ProductClass */
392 5
            $ProductClass = $CartItem->getObject();
393 5
            if (!$ProductClass) {
394
                $this->loadProductClassFromCartItem($CartItem);
395
396
                $ProductClass = $CartItem->getObject();
397
            }
398
399 5
            if ($ProductClass->getDelFlg()) {
400
                // 商品情報が削除されていたらエラー
401
                $this->setError('cart.product.delete');
402
                // カートから削除
403 81
                $this->removeProduct($ProductClass->getId());
404
            }
405
        }
406
407 81
        return $this->cart;
408
409
    }
410
411
    /**
412
     * カートを取得します。
413
     *
414
     * @return \Eccube\Entity\Cart
415
     */
416 175
    public function getCart()
417
    {
418 175
        foreach ($this->cart->getCartItems() as $CartItem) {
419
420
            /** @var \Eccube\Entity\ProductClass $ProductClass */
421 132
            $ProductClass = $CartItem->getObject();
422 132
            if (!$ProductClass) {
423 132
                $this->loadProductClassFromCartItem($CartItem);
424
425 132
                $ProductClass = $CartItem->getObject();
426
            }
427
428 132
            if ($ProductClass->getDelFlg() == Constant::DISABLED) {
429
                // 商品情報が有効
430
431 132
                if (!$this->isProductDisplay($ProductClass)) {
432 4
                    $this->setError('cart.product.not.status');
433
                } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
434
435 132
                    $productName = $this->getProductName($ProductClass);
436
437
                    // 制限数チェック
438 132
                    $quantity = $this->setProductLimit($ProductClass, $productName, $CartItem->getQuantity());
439
440 132
                    if ($CartItem->getQuantity() != $quantity) {
441
                        // 個数が異なれば更新
442 10
                        $CartItem->setQuantity($quantity);
443 132
                        $this->cart->setCartItem($CartItem);
444
                    }
445
                }
446
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
447
            } else {
448
                // 商品情報が削除されていたらエラー
449 7
                $this->setError('cart.product.delete');
450
                // カートから削除
451 175
                $this->removeProduct($ProductClass->getId());
452
            }
453
        }
454
455 175
        return $this->cart;
456
    }
457
458
    /**
459
     * @param  string $productClassId
460
     * @return \Eccube\Service\CartService
461
     */
462 26
    public function removeProduct($productClassId)
463
    {
464 26
        $this->cart->removeCartItemByIdentifier('Eccube\Entity\ProductClass', (string)$productClassId);
0 ignored issues
show
Coding Style introduced by
As per coding-style, a cast statement should be followed by a single space.
Loading history...
465
466
        // 支払方法の再設定
467 26
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
468
469
            // 複数配送対応
470 1
            $productTypes = array();
471 1
            foreach ($this->getCart()->getCartItems() as $item) {
472
                /* @var $ProductClass \Eccube\Entity\ProductClass */
473 1
                $ProductClass = $item->getObject();
474 1
                $productTypes[] = $ProductClass->getProductType();
475
            }
476
477
            // 配送業者を取得
478 1
            $deliveries = $this->entityManager->getRepository('Eccube\Entity\Delivery')->getDeliveries($productTypes);
479
480
            // 支払方法を取得
481 1
            $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries);
482
483 1
            $this->getCart()->setPayments($payments);
484
        }
485
486 26
        return $this;
487
    }
488
489
    /**
490
     * @param  string $error
491
     * @param  string $productName
492
     * @return \Eccube\Service\CartService
493
     */
494 28
    public function addError($error = null, $productName = null)
495
    {
496 28
        $this->errors[] = $error;
497 28
        $this->session->getFlashBag()->add('eccube.front.request.error', $error);
498 28
        if (!is_null($productName)) {
499 27
            $this->session->getFlashBag()->add('eccube.front.request.product', $productName);
500
        }
501
502 28
        return $this;
503
    }
504
505
    /**
506
     * @param  string $productClassId
507
     * @return \Eccube\Service\CartService
508
     */
509 11
    public function upProductQuantity($productClassId)
510
    {
511 11
        $quantity = $this->getProductQuantity($productClassId) + 1;
512 11
        $this->setProductQuantity($productClassId, $quantity);
513
514 8
        return $this;
515
    }
516
517
    /**
518
     * @param  string $productClassId
519
     * @return \Eccube\Service\CartService
520
     */
521 12
    public function downProductQuantity($productClassId)
522
    {
523 12
        $quantity = $this->getProductQuantity($productClassId) - 1;
524 12
        if ($quantity > 0) {
525 8
            $this->setProductQuantity($productClassId, $quantity);
526
        }
527
528 10
        return $this;
529
    }
530
531
    /**
532
     * @return array
533
     */
534 95
    public function getProductTypes()
535
    {
536
537 95
        $productTypes = array();
538 95
        foreach ($this->getCart()->getCartItems() as $item) {
539
            /* @var $ProductClass \Eccube\Entity\ProductClass */
540 95
            $ProductClass = $item->getObject();
541 95
            $productTypes[] = $ProductClass->getProductType();
542
        }
543
544 95
        return array_unique($productTypes);
545
546
    }
547
548
    /**
549
     * @return string[]
550
     */
551 3
    public function getErrors()
552
    {
553 3
        return $this->errors;
554
    }
555
556
    /**
557
     * @return string[]
558
     */
559 1
    public function getMessages()
560
    {
561 1
        return $this->messages;
562
    }
563
564
    /**
565
     * @param  string $message
566
     * @return \Eccube\Service\CartService
567
     */
568 1
    public function setMessage($message)
569
    {
570 1
        $this->messages[] = $message;
571
572 1
        return $this;
573
    }
574
575
    /**
576
     * @return string
577
     */
578 1
    public function getError()
579
    {
580 1
        return $this->error;
581
    }
582
583
    /**
584
     * @param  string $error
585
     * @return \Eccube\Service\CartService
586
     */
587 12
    public function setError($error = null)
588
    {
589 12
        $this->error = $error;
0 ignored issues
show
Documentation Bug introduced by
It seems like $error of type string or null is incompatible with the declared type array of property $error.

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...
590 12
        $this->session->getFlashBag()->set('eccube.front.request.error', $error);
591
592 12
        return $this;
593
    }
594
595
    /**
596
     * 商品名を取得
597
     *
598
     * @param ProductClass $ProductClass
599
     * @return string
600
     */
601 170
    private function getProductName(ProductClass $ProductClass)
602
    {
603
604 170
        $productName = $ProductClass->getProduct()->getName();
605
606 170
        if ($ProductClass->hasClassCategory1()) {
607 170
            $productName .= " - ".$ProductClass->getClassCategory1()->getName();
608
        }
609
610 170
        if ($ProductClass->hasClassCategory2()) {
611 143
            $productName .= " - ".$ProductClass->getClassCategory2()->getName();
612
        }
613
614 170
        return $productName;
615
    }
616
617
618
    /**
619
     * 非公開商品の場合、カートから削除
620
     *
621
     * @param ProductClass $ProductClass
622
     * @return bool
623
     */
624 171
    private function isProductDisplay(ProductClass $ProductClass)
625
    {
626
627 171
        if ($ProductClass->getProduct()->getStatus()->getId() !== Disp::DISPLAY_SHOW) {
628
            // 非公開の商品はカートから削除
629 8
            $this->removeProduct($ProductClass->getId());
630
631 8
            return false;
632
        }
633
634 170
        return true;
635
    }
636
637
638
    /**
639
     * 在庫数と販売制限数のチェック
640
     * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、
641
     * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する
642
     *
643
     * @param ProductClass $ProductClass
644
     * @param $productName
645
     * @param $quantity
646
     * @return int|string
647
     */
648 169
    private function setProductLimit(ProductClass $ProductClass, $productName, $quantity)
649
    {
650
651
        /**
652
         * 実際の在庫は ProductClass::ProductStock だが、購入時にロックがかかるため、
653
         * ここでは ProductClass::stock で在庫のチェックをする
654
         */
655
656 169
        $tmp_quantity = 0;
657
658
        // 在庫数(在庫無制限の場合、null)
659 169
        $stock = $ProductClass->getStock();
660
        // 在庫無制限(在庫無制限の場合、1)
661 169
        $stockUnlimited = $ProductClass->getStockUnlimited();
662
663
        // 販売制限数(設定されていなければnull)
664 169
        $saleLimit = $ProductClass->getSaleLimit();
665
666 169
        if ($stockUnlimited) {
667
            // 在庫無制限
668
669 101
            if ($saleLimit && $saleLimit < $quantity) {
670
                // 販売制限数を超えていれば販売制限数をセット
671
                $tmp_quantity = $saleLimit;
672 101
                $this->addError('cart.over.sale_limit', $productName);
673
            }
674
        } else {
675
            // 在庫制限あり
676
677 81
            if ($stock < 1) {
678
                // 在庫がなければカートから削除
679 7
                $this->addError('cart.zero.stock', $productName);
680 7
                $this->removeProduct($ProductClass->getId());
681
            } else {
682
                // 在庫数チェックと販売制限数チェックどちらを適用するか設定
683 81
                $message = 'cart.over.stock';
684 81
                if ($saleLimit) {
685 14
                    if ($stock > $saleLimit) {
686
                        // 販売制限数チェック
687 14
                        $limit = $saleLimit;
688 14
                        $message = 'cart.over.sale_limit';
689
                    } else {
690
                        // 在庫数チェック
691 14
                        $limit = $stock;
692
                    }
693
                } else {
694
                    // 在庫数チェック
695 74
                    $limit = $stock;
696
                }
697
698 81
                if ($limit < $quantity) {
699
                    // 在庫数、販売制限数を超えていれば購入可能数までをセット
700 20
                    $tmp_quantity = $limit;
701 20
                    $this->addError($message, $productName);
702
                }
703
            }
704
        }
705
706 169
        if ($tmp_quantity) {
707 20
            $quantity = $tmp_quantity;
708
        }
709
710 169
        return $quantity;
711
712
    }
713
714
}
715