Failed Conditions
Pull Request — experimental/sf (#3236)
by Kentaro
144:19 queued 116:23
created

SaleLimitOneValidator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Plugin\PurchaseProcessors\Service\PurchaseFlow\Processor;
15
16
use Eccube\Annotation\CartFlow;
17
use Eccube\Annotation\OrderFlow;
18
use Eccube\Annotation\ShoppingFlow;
19
use Eccube\Entity\ItemInterface;
20
use Eccube\Service\PurchaseFlow\InvalidItemException;
21
use Eccube\Service\PurchaseFlow\PurchaseContext;
22
use Eccube\Service\PurchaseFlow\ItemValidator;
23
24
/**
25
 * 商品を1個のみしか購入できないようにするサンプル
26
 *
27
 * # 使い方
28
 * PurchaseFlowに新しいProcessorを追加する
29
 *
30
 * ## 追加できるプロセッサ
31
 * 以下のクラスまたはインタフェースを継承または実装している必要がある
32
 * * ItemPreprocessor
33
 * * ItemValidator
34
 * * ItemHolderPreprocessor
35
 * * ItemHolderValidator
36
 * * PurchaseProcessor
37
 *
38
 * ## 追加対象のフローの指定方法
39
 * * カートのPurchaseFlowにProcessorを追加する場合はCartFlowアノテーションを追加
40
 * * 購入フローのPurchaseFlowにProcessorを追加する場合はShoppingFlowアノテーションを追加
41
 * * 管理画面でのPurchaseFlowにProcessorを追加する場合はOrderFlowアノテーションを追加
42
 *
43
 * @CartFlow
44
 * @ShoppingFlow
45
 * @OrderFlow
46
 */
47
class SaleLimitOneValidator extends ItemValidator
48
{
49
    /**
50
     * @param ItemInterface $item
51
     * @param PurchaseContext $context
52
     *
53
     * @throws InvalidItemException
54
     */
55
    protected function validate(ItemInterface $item, PurchaseContext $context)
56
    {
57
        if (!$item->isProduct()) {
58
            return;
59
        }
60
61
        $quantity = $item->getQuantity();
62
        if (1 < $quantity) {
63
            $this->throwInvalidItemException('商品は1個しか購入できません。');
64
        }
65
    }
66
67
    protected function handle(ItemInterface $item, PurchaseContext $context)
68
    {
69
        $item->setQuantity(1);
70
    }
71
}
72