Failed Conditions
Pull Request — experimental/sf (#3241)
by Kiyotaka
276:13 queued 265:05
created

OrderNoProcessor::process()   C

Complexity

Conditions 15
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 16.0422

Importance

Changes 0
Metric Value
cc 15
nc 4
nop 2
dl 0
loc 58
ccs 30
cts 36
cp 0.8333
crap 16.0422
rs 5.9166
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Common\EccubeConfig;
17
use Eccube\Entity\ItemHolderInterface;
18
use Eccube\Repository\OrderRepository;
19
use Eccube\Service\PurchaseFlow\ProcessResult;
20
use Eccube\Service\PurchaseFlow\PurchaseContext;
21
use Eccube\Service\PurchaseFlow\PurchaseProcessor;
22
use Eccube\Util\StringUtil;
23
24
class OrderNoProcessor implements PurchaseProcessor
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
25
{
26
    /**
27
     * @var EccubeConfig
28
     */
29
    private $eccubeConfig;
30
31
    /**
32
     * @var OrderRepository
33
     */
34
    private $orderRepository;
35
36
    /**
37
     * OrderNoProcessor constructor.
38
     *
39
     * @param EccubeConfig $eccubeConfig
0 ignored issues
show
introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
40
     * @param OrderRepository $orderRepository
41
     */
42 79
    public function __construct(EccubeConfig $eccubeConfig, OrderRepository $orderRepository)
43
    {
44 79
        $this->eccubeConfig = $eccubeConfig;
45 79
        $this->orderRepository = $orderRepository;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 26
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
52
    {
53 26
        $Order = $itemHolder;
54
55 26
        if ($Order instanceof \Eccube\Entity\Order) {
56 26
            if ($Order->getOrderNo()) {
57
                return ProcessResult::success();
58
            }
59
60 26
            $format = $this->eccubeConfig['eccube_order_no_format'];
61 26
            if (empty($format)) {
62
                // フォーマットが設定されていなければ受注IDが設定される
63 1
                $Order->setOrderNo($Order->getId());
64
            } else {
65
                do {
66 25
                    $orderNo = preg_replace_callback('/\{(.*)}/U', function ($matches) use ($Order) {
67 24
                        if (count($matches) === 2) {
68 24
                            switch ($matches[1]) {
69
                                case 'yyyy':
70 7
                                    return date('Y');
71
                                case 'yy':
72 2
                                    return date('y');
73
                                case 'mm':
74 8
                                    return date('m');
75
                                case 'dd':
76 8
                                    return date('d');
77
                                default:
78 20
                                    $res = explode(',', str_replace(' ', '', $matches[1]));
79 20
                                    if (count($res) === 2 && is_numeric($res[1])) {
80 16
                                        if ($res[0] === 'id') {
81 12
                                            return sprintf("%0{$res[1]}d", $Order->getId());
82 5
                                        } elseif ($res[0] === 'random') {
83 3
                                            $random = random_int(1, (int) str_repeat('9', $res[1]));
84
85 3
                                            return sprintf("%0{$res[1]}d", $random);
86 3
                                        } elseif ($res[0] === 'random_alnum') {
87 3
                                            return strtoupper(StringUtil::random($res[1]));
88
                                        }
89
                                    }
90
91 4
                                    return $Order->getId();
92
                            }
93
                        }
94
95
                        return $Order->getId();
96 25
                    }, $format);
97
98 25
                    $tempOrder = $this->orderRepository->findOneBy([
99 25
                        'order_no' => $orderNo,
100
                    ]);
101 25
                } while ($tempOrder);
102
103 25
                $Order->setOrderNo($orderNo);
104
            }
105
        }
106
107 26
        return ProcessResult::success();
108
    }
109
}
110