Completed
Push — sf/improvement-coverage ( b3937e...01a837 )
by Kiyotaka
51:20 queued 45:08
created

SaveEventSubscriber::prePersist()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 21

Duplication

Lines 6
Ratio 28.57 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
nc 24
nop 1
dl 6
loc 21
rs 8.9617
c 0
b 0
f 0
ccs 13
cts 13
cp 1
crap 6
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')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
92 471
            $user = $this->requestContext->getCurrentUser();
93 471
            if ($user instanceof Member) {
94 63
                $entity->setCreator($user);
95
            }
96
        }
97
    }
98
}
99