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

StockReduceProcessor::process()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 13
nc 6
nop 2
dl 0
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Service\PurchaseFlow\Processor;
4
5
use Doctrine\DBAL\LockMode;
6
use Eccube\Common\Constant;
7
use Eccube\Entity\ItemInterface;
8
use Eccube\Entity\ShipmentItem;
9
use Eccube\Service\PurchaseFlow\ItemProcessor;
10
use Eccube\Service\PurchaseFlow\ProcessResult;
11
use Eccube\Service\PurchaseFlow\PurchaseContext;
12
13
/**
14
 * 在庫制御.
15
 */
16
class StockReduceProcessor implements ItemProcessor
17
{
18
    private $app;
19
20
    /**
0 ignored issues
show
introduced by
Doc comment for parameter "$app" missing
Loading history...
21
     * DeliveryFeeProcessor constructor.
22
     *
23
     * @param $app
0 ignored issues
show
introduced by
Missing parameter name
Loading history...
24
     */
25
    public function __construct($app)
26
    {
27
        $this->app = $app;
28
    }
29
30
    /**
31
     * @param ItemInterface   $item
32
     * @param PurchaseContext $context
33
     *
34
     * @return ProcessResult
35
     *
36
     * @internal param ItemHolderInterface $itemHolder
37
     */
38
    public function process(ItemInterface $item, PurchaseContext $context)
39
    {
40
        if (!$item instanceof ShipmentItem) {
41
            // ShipmentItem 以外の場合は何もしない
42
            return ProcessResult::success();
43
        }
44
45
        // 在庫処理を実装
46
        // warning も作りたい
47
        if (!$item->isProduct()) {
48
            // FIXME 配送明細を考慮する必要がある
49
            return ProcessResult::success();
50
        }
51
        // 在庫が無制限かチェックし、制限ありなら在庫数をチェック
52
        if ($item->getProductClass()->getStockUnlimited() == Constant::DISABLED) {
53
            // 在庫チェックあり
54
            // 在庫に対してロック(select ... for update)を実行
55
            $productStock = $this->app['orm.em']->getRepository('Eccube\Entity\ProductStock')->find(
56
                $item->getProductClass()->getProductStock()->getId(), LockMode::PESSIMISTIC_WRITE
57
            );
58
            // 購入数量と在庫数をチェックして在庫がなければエラー
59
            if ($productStock->getStock() < 1) {
60
                return ProcessResult::fail('在庫エラー');
0 ignored issues
show
Bug introduced by
The method fail() does not seem to exist on object<Eccube\Service\PurchaseFlow\ProcessResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
            } elseif ($item->getQuantity() > $productStock->getStock()) {
62
                return ProcessResult::fail('在庫エラー');
0 ignored issues
show
Bug introduced by
The method fail() does not seem to exist on object<Eccube\Service\PurchaseFlow\ProcessResult>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
            }
64
        }
65
66
        return ProcessResult::success();
67
    }
68
}
69