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

SaleLimitValidator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 5
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 16 4
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 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