Completed
Pull Request — experimental/sf (#3243)
by Kentaro
49:28 queued 40:44
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\ItemHolderProcessor;
20
use Eccube\Service\PurchaseFlow\ProcessResult;
21
use Eccube\Service\PurchaseFlow\PurchaseContext;
22
23
class SaleLimitMultipleValidator implements ItemHolderProcessor
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
24
{
25
    /**
26
     * @var ProductClassRepository
27
     */
28
    protected $productClassRepository;
29
30
    /**
31
     * StockProcessor constructor.
32
     *
33
     * @param ProductClassRepository $productClassRepository
34
     */
35 63
    public function __construct(ProductClassRepository $productClassRepository)
36
    {
37 63
        $this->productClassRepository = $productClassRepository;
38
    }
39
40
    /**
41
     * @param ItemHolderInterface $itemHolder
42
     * @param PurchaseContext $context
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
43
     *
44
     * @return ProcessResult
45
     */
46 54
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
47
    {
48 54
        $OrderItemsByProductClass = [];
49 54 View Code Duplication
        foreach ($itemHolder->getItems() as $Item) {
50 54
            if ($Item->isProduct()) {
51 54
                $id = $Item->getProductClass()->getId();
52 54
                $OrderItemsByProductClass[$id][] = $Item;
53
            }
54
        }
55
56 54
        foreach ($OrderItemsByProductClass as $id => $Items) {
57 54
            $ProductClass = $this->productClassRepository->find($id);
58 54
            $limit = $ProductClass->getSaleLimit();
59 54
            if (null === $limit) {
60 51
                continue;
61
            }
62 4
            $total = 0;
63 4 View Code Duplication
            foreach ($Items as $Item) {
64 4
                $total += $Item->getQuantity();
65 4
                if ($limit < $total) {
66 3
                    return ProcessResult::warn(trans('cart.over.sale_limit',
67 4
                        ['%product%' => $this->formatProductName($ProductClass)]));
0 ignored issues
show
Documentation introduced by
$ProductClass is of type object|null, but the function expects a object<Eccube\Entity\ProductClass>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Coding Style introduced by
This line of the multi-line function call does not seem to be indented correctly. Expected 20 spaces, but found 24.
Loading history...
68
                }
69
            }
70
        }
71
72 52
        return ProcessResult::success();
73
    }
74
75 3 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...
76
    {
77 3
        $productName = $ProductClass->getProduct()->getName();
78 3
        if ($ProductClass->hasClassCategory1()) {
79 3
            $productName .= ' - '.$ProductClass->getClassCategory1()->getName();
80
        }
81 3
        if ($ProductClass->hasClassCategory2()) {
82
            $productName .= ' - '.$ProductClass->getClassCategory2()->getName();
83
        }
84
85 3
        return $productName;
86
    }
87
}
88