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

SaleLimitMultipleValidator::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 SaleLimitMultipleValidator extends ItemHolderValidator
22
{
23
    /**
24
     * @var ProductClassRepository
25
     */
26
    protected $productClassRepository;
27
28
    /**
29
     * StockProcessor constructor.
30
     *
31
     * @param ProductClassRepository $productClassRepository
32
     */
33 63
    public function __construct(ProductClassRepository $productClassRepository)
34
    {
35 63
        $this->productClassRepository = $productClassRepository;
36
    }
37
38
    /**
39
     * @param ItemHolderInterface $itemHolder
40
     * @param PurchaseContext $context
41
     *
42
     * @throws \Eccube\Service\PurchaseFlow\InvalidItemException
43
     */
44 54
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
45
    {
46 54
        $OrderItemsByProductClass = [];
47 54 View Code Duplication
        foreach ($itemHolder->getItems() as $Item) {
48 54
            if ($Item->isProduct()) {
49 54
                $id = $Item->getProductClass()->getId();
50 54
                $OrderItemsByProductClass[$id][] = $Item;
51
            }
52
        }
53
54 54
        foreach ($OrderItemsByProductClass as $id => $Items) {
55 54
            $ProductClass = $this->productClassRepository->find($id);
56 54
            $limit = $ProductClass->getSaleLimit();
57 54
            if (null === $limit) {
58 51
                continue;
59
            }
60 4
            $total = 0;
61 4
            foreach ($Items as $Item) {
62 4
                $total += $Item->getQuantity();
63 4
                if ($limit < $total) {
64 4
                    $this->throwInvalidItemException('cart.over.sale_limit', $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...
65
                }
66
            }
67
        }
68
    }
69
}
70