Completed
Push — 4.0 ( 268f2c...88f012 )
by Hideki
05:48 queued 10s
created

PurchaseFlow/Processor/OrderNoProcessor.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.ec-cube.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\Entity\Order;
19
use Eccube\Repository\OrderRepository;
20
use Eccube\Service\PurchaseFlow\ItemHolderPreprocessor;
21
use Eccube\Service\PurchaseFlow\PurchaseContext;
22
use Eccube\Util\StringUtil;
23
24
class OrderNoProcessor implements ItemHolderPreprocessor
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
40
     * @param OrderRepository $orderRepository
41
     */
42 89
    public function __construct(EccubeConfig $eccubeConfig, OrderRepository $orderRepository)
43
    {
44 89
        $this->eccubeConfig = $eccubeConfig;
45 89
        $this->orderRepository = $orderRepository;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 76
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
52
    {
53 76
        $Order = $itemHolder;
54
55 76
        if ($Order instanceof Order) {
56 76
            if ($Order->getOrderNo()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $Order->getOrderNo() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
57 36
                return;
58
            }
59
60 73
            $format = $this->eccubeConfig['eccube_order_no_format'];
61 73
            if (empty($format)) {
62
                // フォーマットが設定されていなければ受注IDが設定される
63 54
                $Order->setOrderNo($Order->getId());
64
            } else {
65
                do {
66 19
                    $orderNo = preg_replace_callback('/\{(.*)}/U', function ($matches) use ($Order) {
67 18
                        if (count($matches) === 2) {
68 18
                            switch ($matches[1]) {
69
                                case 'yyyy':
70 1
                                    return date('Y');
71
                                case 'yy':
72 2
                                    return date('y');
73
                                case 'mm':
74 2
                                    return date('m');
75
                                case 'dd':
76 2
                                    return date('d');
77
                                default:
78 14
                                    $res = explode(',', str_replace(' ', '', $matches[1]));
79 14
                                    if (count($res) === 2 && is_numeric($res[1])) {
80 10
                                        if ($res[0] === 'id') {
81 6
                                            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 19
                    }, $format);
97
98 19
                    $tempOrder = $this->orderRepository->findOneBy([
99 19
                        'order_no' => $orderNo,
100
                    ]);
101 19
                } while ($tempOrder);
102
103 19
                $Order->setOrderNo($orderNo);
104
            }
105
        }
106
    }
107
}
108