Failed Conditions
Pull Request — experimental/3.1 (#2424)
by chihiro
42:25
created

SaleLimitValidator::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
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\ValidatableItemProcessor;
8
use Eccube\Service\PurchaseFlow\PurchaseContext;
9
10
/**
11
 * 販売制限数チェック.
12
 */
13
class SaleLimitValidator extends ValidatableItemProcessor
14
{
15
    protected function validate(ItemInterface $item, PurchaseContext $context)
16
    {
17
        if (!$item->isProduct()) {
18
            return;
19
        }
20
21
        $limit = $item->getProductClass()->getSaleLimit();
22
        if (is_null($limit)) {
23
            return;
24
        }
25
26
        $quantity = $item->getQuantity();
27
        if ($limit < $quantity) {
28
            throw new ItemValidateException('cart.over.sale_limit', ['%product%' => $item->getProductClass()->getProduct()->getName()]);
29
        }
30
    }
31
32
    protected function handle(ItemInterface $item, PurchaseContext $context)
33
    {
34
        $limit = $item->getProductClass()->getSaleLimit();
35
        $item->setQuantity($limit);
36
    }
37
}
38