Failed Conditions
Pull Request — experimental/3.1 (#2479)
by chihiro
38:20 queued 22:06
created

StockValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 26
ccs 12
cts 14
cp 0.8571
rs 10
c 1
b 0
f 0
wmc 6
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 5 1
B validate() 0 17 5
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
            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