Failed Conditions
Push — experimental/3.1 ( 965511...751c7a )
by chihiro
21s
created

StockValidator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 17 5
A handle() 0 5 1
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\ValidatableItemProcessor;
8
use Eccube\Service\PurchaseFlow\PurchaseContext;
9
10
/**
11
 * 在庫制限チェック.
12
 */
13
class StockValidator extends ValidatableItemProcessor
14
{
15
    protected function validate(ItemInterface $item, PurchaseContext $context)
16
    {
17
        if (!$item->isProduct()) {
18
            return;
19
        }
20
        if ($item->getProductClass()->getStockUnlimited()) {
21
            return;
22
        }
23
        $stock = $item->getProductClass()->getStock();
24
        $quantity = $item->getQuantity();
25
        if ($stock == 0) {
26
            throw new ItemValidateException('cart.zero.stock', ['%product%' => $item->getProductClass()->getProduct()->getName()]);
27
        }
28
        if ($stock < $quantity) {
29
            throw new ItemValidateException('cart.over.stock', ['%product%' => $item->getProductClass()->getProduct()->getName()]);
30
        }
31
    }
32
33
    protected function handle(ItemInterface $item, PurchaseContext $context)
34
    {
35
        $stock = $item->getProductClass()->getStock();
36
        $item->setQuantity($stock);
37
    }
38
}
39