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

OrderNoProcessor   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.1%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 25
cts 39
cp 0.641
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C process() 0 58 15
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