Failed Conditions
Pull Request — experimental/sf (#3247)
by Kiyotaka
114:07 queued 103:28
created

SaleLimitMultipleValidator::process()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 28

Duplication

Lines 13
Ratio 46.43 %

Code Coverage

Tests 18
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 2
dl 13
loc 28
rs 8.5386
c 0
b 0
f 0
ccs 18
cts 18
cp 1
crap 7
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\Entity\ProductClass;
18
use Eccube\Repository\ProductClassRepository;
19
use Eccube\Service\PurchaseFlow\ItemHolderValidator;
20
use Eccube\Service\PurchaseFlow\PurchaseContext;
21
22
class SaleLimitMultipleValidator extends ItemHolderValidator
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
23
{
24
    /**
25
     * @var ProductClassRepository
26
     */
27
    protected $productClassRepository;
28
29
    /**
30
     * StockProcessor constructor.
31
     *
32
     * @param ProductClassRepository $productClassRepository
33
     */
34 63
    public function __construct(ProductClassRepository $productClassRepository)
35
    {
36 63
        $this->productClassRepository = $productClassRepository;
37
    }
38
39
    /**
40
     * @param ItemHolderInterface $itemHolder
41
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
42
     *
43
     * @throws \Eccube\Service\PurchaseFlow\InvalidItemException
44
     */
45 54
    public function validate(ItemHolderInterface $itemHolder, PurchaseContext $context)
46
    {
47 54
        $OrderItemsByProductClass = [];
48 54 View Code Duplication
        foreach ($itemHolder->getItems() as $Item) {
49 54
            if ($Item->isProduct()) {
50 54
                $id = $Item->getProductClass()->getId();
51 54
                $OrderItemsByProductClass[$id][] = $Item;
52
            }
53
        }
54
55 54
        foreach ($OrderItemsByProductClass as $id => $Items) {
56 54
            $ProductClass = $this->productClassRepository->find($id);
57 54
            $limit = $ProductClass->getSaleLimit();
58 54
            if (null === $limit) {
59 51
                continue;
60
            }
61 4
            $total = 0;
62 4
            foreach ($Items as $Item) {
63 4
                $total += $Item->getQuantity();
64 4
                if ($limit < $total) {
65 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 56 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...
66
                }
67
            }
68
        }
69
    }
70
71 View Code Duplication
    protected function formatProductName(ProductClass $ProductClass)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        $productName = $ProductClass->getProduct()->getName();
74
        if ($ProductClass->hasClassCategory1()) {
75
            $productName .= ' - '.$ProductClass->getClassCategory1()->getName();
76
        }
77
        if ($ProductClass->hasClassCategory2()) {
78
            $productName .= ' - '.$ProductClass->getClassCategory2()->getName();
79
        }
80
81
        return $productName;
82
    }
83
}
84