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\Doctrine\EventSubscriber; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\EventSubscriber; |
17
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
18
|
|
|
use Doctrine\ORM\Events; |
19
|
|
|
use Eccube\Common\EccubeConfig; |
20
|
|
|
use Eccube\Entity\Member; |
21
|
|
|
use Eccube\Request\Context; |
22
|
|
|
|
23
|
|
|
class SaveEventSubscriber implements EventSubscriber |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Context |
27
|
|
|
*/ |
28
|
|
|
protected $requestContext; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var EccubeConfig |
32
|
|
|
*/ |
33
|
|
|
protected $eccubeConfig; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param Context $requestContext |
37
|
|
|
*/ |
38
|
1331 |
|
public function __construct(Context $requestContext, EccubeConfig $eccubeConfig) |
39
|
|
|
{ |
40
|
1331 |
|
$this->requestContext = $requestContext; |
41
|
1331 |
|
$this->eccubeConfig = $eccubeConfig; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
1331 |
|
public function getSubscribedEvents() |
48
|
|
|
{ |
49
|
|
|
return [ |
50
|
1331 |
|
Events::prePersist, |
51
|
1331 |
|
Events::preUpdate, |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param LifecycleEventArgs $args |
57
|
|
|
*/ |
58
|
771 |
|
public function prePersist(LifecycleEventArgs $args) |
59
|
|
|
{ |
60
|
771 |
|
$entity = $args->getObject(); |
61
|
|
|
|
62
|
771 |
|
if (method_exists($entity, 'setCreateDate')) { |
63
|
768 |
|
$entity->setCreateDate(new \DateTime()); |
64
|
|
|
} |
65
|
771 |
|
if (method_exists($entity, 'setUpdateDate')) { |
66
|
770 |
|
$entity->setUpdateDate(new \DateTime()); |
67
|
|
|
} |
68
|
771 |
|
if (method_exists($entity, 'setCurrencyCode')) { |
69
|
427 |
|
$currency = $this->eccubeConfig->get('currency'); |
70
|
427 |
|
$entity->setCurrencyCode($currency); |
71
|
|
|
} |
72
|
771 |
View Code Duplication |
if (method_exists($entity, 'setCreator')) { |
|
|
|
|
73
|
657 |
|
$user = $this->requestContext->getCurrentUser(); |
74
|
657 |
|
if ($user instanceof Member) { |
75
|
47 |
|
$entity->setCreator($user); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param LifecycleEventArgs $args |
82
|
|
|
*/ |
83
|
522 |
|
public function preUpdate(LifecycleEventArgs $args) |
84
|
|
|
{ |
85
|
522 |
|
$entity = $args->getObject(); |
86
|
|
|
|
87
|
522 |
|
if (method_exists($entity, 'setUpdateDate')) { |
88
|
520 |
|
$entity->setUpdateDate(new \DateTime()); |
89
|
|
|
} |
90
|
|
|
|
91
|
522 |
View Code Duplication |
if (method_exists($entity, 'setCreator')) { |
|
|
|
|
92
|
471 |
|
$user = $this->requestContext->getCurrentUser(); |
93
|
471 |
|
if ($user instanceof Member) { |
94
|
63 |
|
$entity->setCreator($user); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.