Failed Conditions
Pull Request — experimental/sf (#3241)
by k-yamamura
43:19 queued 23:09
created

OrderNoProcessor::process()   C

Complexity

Conditions 15
Paths 4

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 28.2341

Importance

Changes 0
Metric Value
cc 15
nc 4
nop 2
dl 0
loc 58
ccs 22
cts 36
cp 0.6111
crap 28.2341
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 59
    public function __construct(EccubeConfig $eccubeConfig, OrderRepository $orderRepository)
43
    {
44 59
        $this->eccubeConfig = $eccubeConfig;
45 59
        $this->orderRepository = $orderRepository;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
52
    {
53 6
        $Order = $itemHolder;
54
55 6
        if ($Order instanceof \Eccube\Entity\Order) {
56 6
            if ($Order->getOrderNo()) {
57
                return ProcessResult::success();
58
            }
59
60 6
            $format = $this->eccubeConfig['eccube_order_no_format'];
61 6
            if (empty($format)) {
62
                // フォーマットが設定されていなければ受注IDが設定される
63
                $Order->setOrderNo($Order->getId());
64
            } else {
65
                do {
66 6
                    $orderNo = preg_replace_callback('/\{(.*)}/U', function ($matches) use ($Order) {
67 6
                        if (count($matches) == 2) {
68 6
                            switch ($matches[1]) {
69
                                case 'yyyy':
70 6
                                    return date('Y');
71
                                case 'yy':
72
                                    return date('y');
73
                                case 'mm':
74 6
                                    return date('m');
75
                                case 'dd':
76 6
                                    return date('d');
77
                                default:
78 6
                                    $res = explode(',', $matches[1]);
79 6
                                    if (count($res) == 2 && is_numeric($res[1])) {
80 6
                                        if ($res[0] == 'id') {
81 6
                                            return sprintf("%0{$res[1]}d", $Order->getId());
82
                                        } elseif ($res[0] == 'random') {
83
                                            $rondom = substr(mt_rand(1, 999999999), 0, $res[1]);
84
85
                                            return sprintf("%0{$res[1]}d", $rondom);
86
                                        } elseif ($res[0] == 'randomalphanum') {
87
                                            return strtoupper(StringUtil::random(5));
88
                                        }
89
                                    }
90
91
                                    return '';
92
                            }
93
                        }
94
95
                        return '';
96 6
                    }, $format);
97
98 6
                    $tempOrder = $this->orderRepository->findOneBy([
99 6
                        'order_no' => $orderNo,
100
                    ]);
101 6
                } while ($tempOrder);
102
103 6
                $Order->setOrderNo($orderNo);
104
            }
105
        }
106
107 6
        return ProcessResult::success();
108
    }
109
}
110