Completed
Pull Request — experimental/3.1 (#2479)
by chihiro
65:40 queued 24:05
created

StockValidator::validate()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
nc 5
nop 2
dl 0
loc 17
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 8.8571
c 1
b 0
f 0
1
<?php
2
3
namespace Eccube\Service\PurchaseFlow\Processor;
4
5
use Eccube\Entity\ItemInterface;
6
use Eccube\Service\PurchaseFlow\ItemValidateException;
7
use Eccube\Service\PurchaseFlow\PurchaseContext;
8
use Eccube\Service\PurchaseFlow\ValidatableItemProcessor;
9
10
/**
11
 * 在庫制限チェック.
12
 */
13
class StockValidator extends ValidatableItemProcessor
14
{
15 18
    protected function validate(ItemInterface $item, PurchaseContext $context)
16
    {
17 18
        if (!$item->isProduct()) {
18 2
            return;
19
        }
20 18
        if ($item->getProductClass()->getStockUnlimited()) {
21 15
            return;
22
        }
23 3
        $stock = $item->getProductClass()->getStock();
24 3
        $quantity = $item->getQuantity();
25 3
        if ($stock == 0) {
26
            throw ItemValidateException::fromProductClass('cart.zero.stock', $item->getProductClass());
27
        }
28 3
        if ($stock < $quantity) {
29 1
            throw ItemValidateException::fromProductClass('cart.over.stock', $item->getProductClass());
30
        }
31
    }
32
33 1
    protected function handle(ItemInterface $item, PurchaseContext $context)
34
    {
35 1
        $stock = $item->getProductClass()->getStock();
36 1
        $item->setQuantity($stock);
37
    }
38
}
39