Failed Conditions
Pull Request — experimental/sf (#29)
by Kentaro
50:12 queued 39:05
created

StockValidator::validate()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 2
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 9.3888
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Service\PurchaseFlow\Processor;
15
16
use Eccube\Entity\ItemInterface;
17
use Eccube\Service\PurchaseFlow\PurchaseContext;
18
use Eccube\Service\PurchaseFlow\ItemValidator;
19
20
/**
21
 * 在庫制限チェック.
22
 */
23
class StockValidator extends ItemValidator
24
{
25
    /**
26
     * @param ItemInterface $item
27
     * @param PurchaseContext $context
28
     *
29
     * @throws \Eccube\Service\PurchaseFlow\InvalidItemException
30
     */
31 81
    protected function validate(ItemInterface $item, PurchaseContext $context)
32
    {
33 81
        if (!$item->isProduct()) {
34
            return;
35
        }
36 81
        if ($item->getProductClass()->isStockUnlimited()) {
37 30
            return;
38
        }
39 54
        $stock = $item->getProductClass()->getStock();
40 54
        $quantity = $item->getQuantity();
41 54
        if ($stock == 0) {
42 7
            $this->throwInvalidItemException('cart.zero.stock', $item->getProductClass());
43
        }
44 53
        if ($stock < $quantity) {
45 10
            $this->throwInvalidItemException('cart.over.stock', $item->getProductClass());
46
        }
47
    }
48
49 17
    protected function handle(ItemInterface $item, PurchaseContext $context)
50
    {
51 17
        $stock = $item->getProductClass()->getStock();
52 17
        $item->setQuantity($stock);
53
    }
54
}
55