Completed
Pull Request — experimental/3.1 (#2688)
by
unknown
28:04
created

OrderCodePurchaseProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Eccube\Service\PurchaseFlow\Processor;
4
5
use Eccube\Entity\ItemHolderInterface;
6
use Eccube\Service\PurchaseFlow\PurchaseContext;
7
use Eccube\Service\PurchaseFlow\PurchaseProcessor;
8
use Eccube\Service\PurchaseFlow\ProcessResult;
9
use Doctrine\ORM\EntityManager;
10
11
class OrderCodePurchaseProcessor implements PurchaseProcessor
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
12
{
13
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
14
    private $orderNoFormat;
15
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
16
    private $entityManager;
17
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
18
    public function __construct(EntityManager $entityManager, $orderNoFormat)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $entityManager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
introduced by
Missing function doc comment
Loading history...
19
    {
20
        $this->entityManager = $entityManager;
21
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
22
        $this->orderNoFormat = $orderNoFormat;
23
    }
24
    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ItemHolderInterface $itemHolder, PurchaseContext $context)
29
    {
30
        $Order = $itemHolder;
31
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
32
        if ($Order instanceof \Eccube\Entity\Order) {
33
            if ($Order->getCode() == "") {
34
                if (is_null($Order->getId())) {
35
                    $this->entityManager->persist($Order);
36
                    $this->entityManager->flush();
37
                }
38
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
39
                $orderCode = preg_replace_callback('/\${(.*)}/U', function($matches) use ($Order) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
40
                    if (count($matches) == 2) {
41
                        switch ($matches[1]) {
42
                            case "yyyy":
43
                                return date('Y');
44
                            case "mm":
45
                                return date('m');
46
                            case "dd":
47
                                return date('d');
48
                            default:
49
                                $no = explode(',', $matches[1]);
50
                                if (count($no) == 2 && $no[0] == 'number' && is_numeric($no[1])) {
51
                                    return sprintf("%0{$no[1]}d", $Order->getId());
52
                                }
53
                                return "";
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
54
                        }
55
                    }
56
                    
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
57
                    return "";
58
                }, $this->orderNoFormat);
59
                
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
60
                $Order->setCode($orderCode);
61
            }
62
        }
63
        
0 ignored issues
show
introduced by
Please trim any trailing whitespace
Loading history...
64
        return ProcessResult::success();
65
    }
66
}