Failed Conditions
Pull Request — experimental/3.1 (#2159)
by Kentaro
39:20
created

CartService::setError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
ccs 0
cts 0
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
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 145
    public function __construct(\Eccube\Application $app)
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
78
    {
79 145
        $this->app = $app;
80 145
        $this->session = $app['session'];
81 145
        $this->entityManager = $app['orm.em'];
82
83 145
        if ($this->session->has('cart')) {
84
            $this->cart = $this->session->get('cart');
85
        } else {
86 145
            $this->cart = new \Eccube\Entity\Cart();
87
        }
88
89 145
        $this->loadProductClassFromCart();
90
91 145
        $this->BaseInfo = $app['eccube.repository.base_info']->get();
92
    }
93
94
    /**
95
     * カートに保存されている商品の ProductClass エンティティを読み込み、カートへ設定します。
96
     */
97
    protected function loadProductClassFromCart()
98
    {
99
        /* @var $softDeleteFilter \Eccube\Doctrine\Filter\SoftDeleteFilter */
100
        // $softDeleteFilter = $this->entityManager->getFilters()->getFilter('soft_delete');
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
        // $excludes = $softDeleteFilter->getExcludes();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
        // $softDeleteFilter->setExcludes(array(
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
        //     'Eccube\Entity\ProductClass',
104
        // ));
105
106
        // foreach ($this->cart->getCartItems() as $CartItem) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
107
        //     $this->loadProductClassFromCartItem($CartItem);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
108
        // }
109
110
        // $softDeleteFilter->setExcludes($excludes);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
111
    }
112
113
    /**
114
     * CartItem に対応する ProductClass を設定します。
115
     *
116
     * @param CartItem $CartItem
117
     */
118 39
    protected function loadProductClassFromCartItem(CartItem $CartItem)
119
    {
120
        $ProductClass = $this
121 39
            ->entityManager
122 39
            ->getRepository($CartItem->getClassName())
123 39
            ->find($CartItem->getClassId());
124
125 39
        $CartItem->setObject($ProductClass);
126
127 39
        if (is_null($this->ProductType) && $ProductClass->getDelFlg() == Constant::DISABLED) {
128
            $this->setCanAddProductType($ProductClass->getProductType());
129
        }
130
    }
131
132 66
    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 66
        if (is_null($this->ProductType)) {
135 66
            $this->ProductType = $ProductType;
136
        }
137
138 66
        return $this;
139
    }
140
141 71
    public function save()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
142
    {
143 71
        return $this->session->set('cart', $this->cart);
144
    }
145
146 3
    public function unlock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
147
    {
148 3
        $this->cart
149 3
            ->setLock(false)
150 3
            ->setPreOrderId(null);
151
    }
152
153 35
    public function lock()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
154
    {
155 35
        $this->cart
156 35
            ->setLock(true)
157 35
            ->setPreOrderId(null);
158
    }
159
160
    /**
161
     * @return bool
162
     */
163 40
    public function isLocked()
164
    {
165 40
        return $this->cart->getLock();
166
    }
167
168
    /**
169
     * @param  string $pre_order_id
170
     * @return \Eccube\Service\CartService
171
     */
172 17
    public function setPreOrderId($pre_order_id)
173
    {
174 17
        $this->cart->setPreOrderId($pre_order_id);
175
176 17
        return $this;
177
    }
178
179
    /**
180
     * @return string
181
     */
182 36
    public function getPreOrderId()
183
    {
184 36
        return $this->cart->getPreOrderId();
185
    }
186
187
    /**
188
     * @return \Eccube\Service\CartService
189
     */
190
    public function clear()
191
    {
192
        $this->cart
193
            ->setPreOrderId(null)
194
            ->setLock(false)
195
            ->clearCartItems();
196
197
        return $this;
198
    }
199
200
    public function getCanAddProductType()
0 ignored issues
show
introduced by
Missing function doc comment
Loading history...
201
    {
202
        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 36
    public function addProduct($productClassId, $quantity = 1)
212
    {
213 36
        $quantity += $this->getProductQuantity($productClassId);
214 36
        $this->setProductQuantity($productClassId, $quantity);
215
216 35
        return $this;
217
    }
218
219
    /**
220
     * @param  string $productClassId
221
     * @return integer
222
     */
223 42
    public function getProductQuantity($productClassId)
224
    {
225 42
        $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 42
        if ($CartItem) {
227 3
            return $CartItem->getQuantity();
228
        } else {
229 42
            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 68
    public function setProductQuantity($ProductClass, $quantity)
240
    {
241 68
        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 68
            $ProductClass = $this->entityManager
243 68
                ->getRepository('Eccube\Entity\ProductClass')
244 68
                ->find($ProductClass);
245 68
            if (!$ProductClass) {
246 2
                throw new CartException('cart.product.delete');
247
            }
248
        }
249 66
250
        if (!$this->isProductDisplay($ProductClass)) {
251
            throw new CartException('cart.product.not.status');
252
        }
253
254 66
        $productName = $this->getProductName($ProductClass);
255 66
256 66
        // 商品種別に紐づく配送業者を取得
257
        $deliveries = $this->app['eccube.repository.delivery']->getDeliveries($ProductClass->getProductType());
258 66
259 66
        if (count($deliveries) == 0) {
260
            // 商品種別が存在しなければエラー
261
            $this->removeProduct($ProductClass->getId());
262
            $this->addError('cart.product.not.producttype', $productName);
263 66
            throw new CartException('cart.product.not.producttype');
264
        }
265 66
266
        $this->setCanAddProductType($ProductClass->getProductType());
267
268
        if ($this->BaseInfo->getOptionMultipleShipping() != Constant::ENABLED) {
269
            if (!$this->canAddProduct($ProductClass->getId())) {
270
                // 複数配送対応でなければ商品種別が異なればエラー
271
                throw new CartException('cart.product.type.kind');
272 66
            }
273
        } else {
274 66
            // 複数配送の場合、同一支払方法がなければエラー
275 63
            if (!$this->canAddProductPayment($ProductClass->getProductType())) {
276
                throw new CartException('cart.product.payment.kind');
277 63
            }
278
        }
279
280
        $tmp_subtotal = 0;
281 3
        $tmp_quantity = 0;
282
        foreach ($this->getCartObj()->getCartItems() as $cartitem) {
283
            $pc = $cartitem->getObject();
284
            if ($pc->getId() != $ProductClass->getId()) {
285
                // 追加された商品以外のtotal priceをセット
286
                $tmp_subtotal += $cartitem->getTotalPrice();
287 66
            }
288 66
        }
289 66
        for ($i = 0; $i < $quantity; $i++) {
290 3
            $tmp_subtotal += $ProductClass->getPrice02IncTax();
291 3
            if ($tmp_subtotal > $this->app['config']['max_total_fee']) {
292
                $this->setError('cart.over.price_limit');
293 66
                break;
294
            }
295
            $tmp_quantity++;
296 66
        }
297 66
        if ($tmp_quantity == 0) {
298 66
            // 数量が0の場合、エラー
299
            throw new CartException('cart.over.price_limit');
300
        }
301
302 66
        // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している)
303
        $quantity = $this->setProductLimit($ProductClass, $productName, $tmp_quantity);
304 66
305
		// 新しい数量でカート内商品を登録する
306 66
        if (0 < $quantity) {
307
            $CartItem = new CartItem();
308
            $CartItem
309
                ->setClassName('Eccube\Entity\ProductClass')
310
                ->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...
311
                ->setPrice($ProductClass->getPrice02IncTax())
312 66
                ->setQuantity($quantity);
313
314
            $this->cart->setCartItem($CartItem);
315
        }
316
317
        return $this;
318
    }
319
320
    /**
321 66
     * @param  string $productClassId
322
     * @return boolean
323
     */
324
    public function canAddProduct($productClassId)
325 66
    {
326
        $ProductClass = $this
327
            ->entityManager
328
            ->getRepository('\Eccube\Entity\ProductClass')
329 66
            ->find($productClassId);
330
331 66
        if (!$ProductClass) {
332 66
            return false;
333 66
        }
334 66
335
        $ProductType = $ProductClass->getProductType();
336 66
337
        return $this->ProductType == $ProductType;
338 66
    }
339
340
    /**
341
     * @param \Eccube\Entity\Master\ProductType $ProductType
342
     * @return bool
343
     */
344
    public function canAddProductPayment(\Eccube\Entity\Master\ProductType $ProductType)
345 63
    {
346
        $deliveries = $this
347
            ->entityManager
348 63
            ->getRepository('\Eccube\Entity\Delivery')
349 63
            ->findBy(array('ProductType' => $ProductType));
350 63
351
        // 支払方法を取得
352 63
        $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries);
353
354
        if ($this->getCart()->getTotalPrice() < 1) {
355
            // カートになければ支払方法を全て設定
356 63
            $this->getCart()->setPayments($payments);
357
358 63
            return true;
359
        }
360
361
        // カートに存在している支払方法と追加された商品の支払方法チェック
362
        $arr = array();
363
        foreach ($payments as $payment) {
364
            foreach ($this->getCart()->getPayments() as $p) {
365 3
                if ($payment['id'] == $p['id']) {
366
                    $arr[] = $payment;
367
                    break;
368 3
                }
369 3
            }
370 3
        }
371
372
        if (count($arr) > 0) {
373 3
            $this->getCart()->setPayments($arr);
374
375 3
            return true;
376
        }
377 3
378 3
        // 支払条件に一致しない
379
        return false;
380
381
    }
382 3
383 3
    /**
384 3
     * カートブロックに表示するカートを取得します。
385 3
     * ブロックに表示するカートはチェックを行わず、セットされているカートを返します。
386 3
     *
387 3
     * @return \Eccube\Entity\Cart
388
     */
389
    public function getCartObj()
390
    {
391
392 3
        foreach ($this->cart->getCartItems() as $CartItem) {
393 3
394 3
            /** @var \Eccube\Entity\ProductClass $ProductClass */
395
            $ProductClass = $CartItem->getObject();
396
            if (!$ProductClass) {
397
                $this->loadProductClassFromCartItem($CartItem);
398
399
                $ProductClass = $CartItem->getObject();
400
            }
401
402
            if ($ProductClass->getDelFlg()) {
403
                // 商品情報が削除されていたらエラー
404
                $this->setError('cart.product.delete');
405
                // カートから削除
406
                $this->removeProduct($ProductClass->getId());
407 130
            }
408
        }
409 130
410 39
        return $this->cart;
411 39
412 39
    }
413
414 39
    /**
415
     * カートを取得します。
416
     *
417 39
     * @return \Eccube\Entity\Cart
418
     */
419 39
    public function getCart()
420 39
    {
421
        foreach ($this->cart->getCartItems() as $CartItem) {
422
423
            /** @var \Eccube\Entity\ProductClass $ProductClass */
424
            $ProductClass = $CartItem->getObject();
425 39
            if (!$ProductClass) {
426 39
                $this->loadProductClassFromCartItem($CartItem);
427 39
428
                $ProductClass = $CartItem->getObject();
429
            }
430 39
431
            if ($ProductClass->getDelFlg() == Constant::DISABLED) {
432 39
                // 商品情報が有効
433
434
                if (!$this->isProductDisplay($ProductClass)) {
435
                    $this->setError('cart.product.not.status');
436
                } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
437
438
                    $productName = $this->getProductName($ProductClass);
439 130
440
                    // 制限数チェック(在庫不足の場合は、処理の中でカート内商品を削除している)
441
                    $quantity = $this->setProductLimit($ProductClass, $productName, $CartItem->getQuantity());
442
443 130
                    /// 個数が異なれば、新しい数量でカート内商品を更新する
444
                    if ((0 < $quantity) && ($CartItem->getQuantity() != $quantity)) {
445
                        // 個数が異なれば更新
446
                        $CartItem->setQuantity($quantity);
447
                        $this->cart->setCartItem($CartItem);
448
                    }
449
                }
450 5
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
451
            } else {
452 5
                // 商品情報が削除されていたらエラー
453
                $this->setError('cart.product.delete');
454
                // カートから削除
455 5
                $this->removeProduct($ProductClass->getId());
456
            }
457
        }
458
459
        return $this->cart;
460
    }
461
462
    /**
463
     * @param  string $productClassId
464
     * @return \Eccube\Service\CartService
465
     */
466
    public function removeProduct($productClassId)
467
    {
468
        $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...
469
470
        // 支払方法の再設定
471
        if ($this->BaseInfo->getOptionMultipleShipping() == Constant::ENABLED) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
472
473
            // 複数配送対応
474 5
            $productTypes = array();
475
            foreach ($this->getCart()->getCartItems() as $item) {
476
                /* @var $ProductClass \Eccube\Entity\ProductClass */
477
                $ProductClass = $item->getObject();
478
                $productTypes[] = $ProductClass->getProductType();
479
            }
480
481
            // 配送業者を取得
482
            $deliveries = $this->entityManager->getRepository('Eccube\Entity\Delivery')->getDeliveries($productTypes);
483
484
            // 支払方法を取得
485
            $payments = $this->entityManager->getRepository('Eccube\Entity\Payment')->findAllowedPayments($deliveries);
486
487
            $this->getCart()->setPayments($payments);
488
        }
489
490
        return $this;
491
    }
492
493
    /**
494
     * @param  string $error
495
     * @param  string $productName
496 3
     * @return \Eccube\Service\CartService
497
     */
498 3
    public function addError($error = null, $productName = null)
499 3
    {
500
        $this->errors[] = $error;
501 2
        $this->session->getFlashBag()->add('eccube.front.request.error', $error);
502
        if (!is_null($productName)) {
503
            $this->session->getFlashBag()->add('eccube.front.request.product', $productName);
504
        }
505
506
        return $this;
507
    }
508 3
509
    /**
510 3
     * @param  string $productClassId
511
     * @return \Eccube\Service\CartService
512 3
     */
513
    public function upProductQuantity($productClassId)
514
    {
515 3
        $quantity = $this->getProductQuantity($productClassId) + 1;
516
        $this->setProductQuantity($productClassId, $quantity);
517
518 3
        return $this;
519
    }
520
521
    /**
522
     * @param  string $productClassId
523
     * @return \Eccube\Service\CartService
524 17
     */
525
    public function downProductQuantity($productClassId)
526
    {
527 17
        $quantity = $this->getProductQuantity($productClassId) - 1;
528 17
        if ($quantity > 0) {
529
            $this->setProductQuantity($productClassId, $quantity);
530 17
        }
531 17
532
        return $this;
533 17
    }
534
535
    /**
536
     * @return array
537
     */
538
    public function getProductTypes()
539
    {
540
541
        $productTypes = array();
542
        foreach ($this->getCart()->getCartItems() as $item) {
543
            /* @var $ProductClass \Eccube\Entity\ProductClass */
544
            $ProductClass = $item->getObject();
545
            $productTypes[] = $ProductClass->getProductType();
546
        }
547
548
        return array_unique($productTypes);
549
550
    }
551
552
    /**
553
     * @return string[]
554
     */
555
    public function getErrors()
556
    {
557
        return $this->errors;
558
    }
559
560
    /**
561
     * @return string[]
562
     */
563
    public function getMessages()
564
    {
565
        return $this->messages;
566
    }
567
568
    /**
569
     * @param  string $message
570
     * @return \Eccube\Service\CartService
571
     */
572
    public function setMessage($message)
573
    {
574
        $this->messages[] = $message;
575
576
        return $this;
577
    }
578
579
    /**
580
     * @return string
581
     */
582
    public function getError()
583
    {
584
        return $this->error;
585
    }
586
587
    /**
588
     * @param  string $error
589
     * @return \Eccube\Service\CartService
590
     */
591
    public function setError($error = null)
592
    {
593
        $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...
594
        $this->session->getFlashBag()->set('eccube.front.request.error', $error);
595
596
        return $this;
597
    }
598
599
    /**
600
     * 商品名を取得
601
     *
602
     * @param ProductClass $ProductClass
603
     * @return string
604
     */
605
    private function getProductName(ProductClass $ProductClass)
606
    {
607
608
        $productName = $ProductClass->getProduct()->getName();
609
610
        if ($ProductClass->hasClassCategory1()) {
611
            $productName .= " - ".$ProductClass->getClassCategory1()->getName();
612
        }
613
614
        if ($ProductClass->hasClassCategory2()) {
615
            $productName .= " - ".$ProductClass->getClassCategory2()->getName();
616
        }
617
618
        return $productName;
619
    }
620
621
622
    /**
623
     * 非公開商品の場合、カートから削除
624
     *
625
     * @param ProductClass $ProductClass
626
     * @return bool
627
     */
628
    private function isProductDisplay(ProductClass $ProductClass)
629
    {
630
631
        if ($ProductClass->getProduct()->getStatus()->getId() !== Disp::DISPLAY_SHOW) {
632
            // 非公開の商品はカートから削除
633
            $this->removeProduct($ProductClass->getId());
634
635
            return false;
636
        }
637
638
        return true;
639
    }
640
641
642
    /**
643
     * 在庫数と販売制限数のチェック
644
     * 在庫数または販売制限数以上の個数が設定されていれば、それぞれの個数にセットし、
645
     * 在庫数と販売制限数ともに個数が超えていれば、少ない方を適用させてメッセージを表示する
646
     *
647
     * @param ProductClass $ProductClass
648
     * @param $productName
649
     * @param $quantity
650
     * @return int チェック後に更新した個数
651
     */
652
    private function setProductLimit(ProductClass $ProductClass, $productName, $quantity)
653
    {
654
655
        /**
656
         * 実際の在庫は ProductClass::ProductStock だが、購入時にロックがかかるため、
657
         * ここでは ProductClass::stock で在庫のチェックをする
658
         */
659
660
        // 在庫数(在庫無制限の場合、null)
661
        $stock = $ProductClass->getStock();
662
        // 在庫無制限(在庫無制限の場合、1)
663
        $stockUnlimited = $ProductClass->getStockUnlimited();
664
665
        // 販売制限数(設定されていなければnull)
666
        $saleLimit = $ProductClass->getSaleLimit();
667
668
        if ($stockUnlimited) {
669
            // 在庫無制限
670
671
            if ($saleLimit && $saleLimit < $quantity) {
672
                // 販売制限数を超えていれば販売制限数をセット
673
                $this->addError('cart.over.sale_limit', $productName);
674
675
                return $saleLimit;
676
            }
677
        } else {
678
            // 在庫制限あり
679
680
            if ($stock < 1) {
681
                // 在庫がなければカートから削除
682
                $this->addError('cart.zero.stock', $productName);
683
                $this->removeProduct($ProductClass->getId());
684
685
                return 0;
686
            } else {
687
                // 在庫数チェックと販売制限数チェックどちらを適用するか設定
688
                $message = 'cart.over.stock';
689
                if ($saleLimit) {
690
                    if ($stock > $saleLimit) {
691
                        // 販売制限数チェック
692
                        $limit = $saleLimit;
693
                        $message = 'cart.over.sale_limit';
694
                    } else {
695
                        // 在庫数チェック
696
                        $limit = $stock;
697
                    }
698
                } else {
699
                    // 在庫数チェック
700
                    $limit = $stock;
701
                }
702
703
                if ($limit < $quantity) {
704
                    // 在庫数、販売制限数を超えていれば購入可能数までをセット
705
                    $this->addError($message, $productName);
706
707
                    return $limit;
708
                }
709
            }
710
        }
711
712
        return $quantity;
713
    }
714
715
}
716