Completed
Push — experimental/sf ( 6f8ad9...eba3f5 )
by Kiyotaka
71:13 queued 61:12
created

StockMultipleValidator::formatProductName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
ccs 0
cts 7
cp 0
crap 12
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 Eccube\Service\PurchaseFlow\Processor;
15
16
use Eccube\Entity\ItemHolderInterface;
17
use Eccube\Repository\ProductClassRepository;
18
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
19
use Eccube\Service\PurchaseFlow\PurchaseContext;
20
21
class StockMultipleValidator extends ItemHolderValidator
22
{
23
    /**
24
     * @var ProductClassRepository
25
     */
26
    protected $productClassRepository;
27
28
    /**
29
     * StockProcessor constructor.
30
     *
31
     * @param ProductClassRepository $productClassRepository
32
     */
33 64
    public function __construct(ProductClassRepository $productClassRepository)
34
    {
35 64
        $this->productClassRepository = $productClassRepository;
36
    }
37
38
    /**
39
     * @param ItemHolderInterface $itemHolder
40
     * @param PurchaseContext $context
41
     *
42
     * @throws \Eccube\Service\PurchaseFlow\InvalidItemException
43
     */
44 55
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
45
    {
46 55
        $OrderItemsByProductClass = [];
47 55 View Code Duplication
        foreach ($itemHolder->getItems() as $Item) {
48 55
            if ($Item->isProduct()) {
49 55
                $id = $Item->getProductClass()->getId();
50 55
                $OrderItemsByProductClass[$id][] = $Item;
51
            }
52
        }
53
54 55
        foreach ($OrderItemsByProductClass as $id => $Items) {
55 55
            $ProductClass = $this->productClassRepository->find($id);
56 55
            if ($ProductClass->isStockUnlimited()) {
57 24
                continue;
58
            }
59 34
            $stock = $ProductClass->getStock();
60 34
            if ($stock == 0) {
61 3
                $this->throwInvalidItemException('cart.zero.stock', $ProductClass);
0 ignored issues
show
Bug introduced by
It seems like $ProductClass defined by $this->productClassRepository->find($id) on line 55 can also be of type object; however, Eccube\Service\PurchaseF...wInvalidItemException() does only seem to accept null|object<Eccube\Entity\ProductClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
62
            }
63 32
            $total = 0;
64 32
            foreach ($Items as $Item) {
65 32
                $total += $Item->getQuantity();
66 32
                if ($stock < $total) {
67 32
                    $this->throwInvalidItemException('cart.over.stock', $ProductClass);
0 ignored issues
show
Bug introduced by
It seems like $ProductClass defined by $this->productClassRepository->find($id) on line 55 can also be of type object; however, Eccube\Service\PurchaseF...wInvalidItemException() does only seem to accept null|object<Eccube\Entity\ProductClass>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
68
                }
69
            }
70
        }
71
    }
72
}
73