Failed Conditions
Push — experimental/sf ( 267a6e...28d741 )
by Ryo
1408:20 queued 1400:20
created

UpdatePointEventSubscriber   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 90%

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
ccs 45
cts 50
cp 0.9
wmc 19
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getMailService() 0 4 1
C preUpdate() 0 81 12
A isUsePoint() 0 8 2
A isAddPoint() 0 8 2
A getSubscribedEvents() 0 6 1
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\ORM\Event\LifecycleEventArgs;
18
use Doctrine\ORM\Event\PreUpdateEventArgs;
19
use Doctrine\ORM\Events;
20
use Eccube\Entity\Customer;
21
use Eccube\Entity\Master\OrderStatus;
22
use Eccube\Entity\Order;
23
use Eccube\Service\MailService;
24
use Symfony\Component\DependencyInjection\ContainerInterface;
25
26
class UpdatePointEventSubscriber implements EventSubscriber
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
27
{
28
    /**
29
     * @var ContainerInterface
30
     */
31
    protected $container;
32
33 1279
    public function __construct(ContainerInterface $container)
34
    {
35 1279
        $this->container = $container;
36
    }
37
38
    public function getMailService()
39
    {
40
        return $this->container->get(MailService::class);
41
    }
42
43 455
    public function preUpdate(LifecycleEventArgs $eventArgs)
44
    {
45 455
        if (!$eventArgs->getObject() instanceof Order) {
46 455
            return;
47
        }
48
49 205
        $Order = $eventArgs->getObject();
50 205
        $Customer = $Order->getCustomer();
51
        // 非会員の場合、処理は無効にする
52 205
        if (!$Customer) {
53 16
            return;
54
        }
55
56
        /** @var PreUpdateEventArgs $eventArgs */
57 189
        if ($eventArgs->hasChangedField('OrderStatus')) {
58 66
            $addCustomerPoint = 0;
59
60
            /** @var OrderStatus $prevStatus */
61 66
            $prevStatus = $eventArgs->getOldValue('OrderStatus');
62
63
            /** @var Order $newOrder */
64 66
            $newOrder = $eventArgs->getEntity();
65
            /** @var OrderStatus $newStatus */
66 66
            $newStatus = $newOrder->getOrderStatus();
67
68
            /*
69
             * 2.13系の処理を移植
70
             * @see https://github.com/EC-CUBE/eccube-2_13/blob/eccube-2.13.5/data/class/helper/SC_Helper_Purchase.php#L1134-L1163
71
             */
72
73
            // 使用ポイントの更新
74
            // 変更前の対応状況が利用対象の場合、変更前の使用ポイント分を戻す
75 66
            if ($this->isUsePoint($prevStatus)) {
76 66
                if ($eventArgs->hasChangedField('use_point')) {
77 4
                    $addCustomerPoint += $eventArgs->getOldValue('use_point');
78
                } else {
79 63
                    $addCustomerPoint += $newOrder->getUsePoint();
80
                }
81
            }
82
83
            // 変更後の対応状況が利用対象の場合、変更後の使用ポイント分を引く
84 66
            if ($this->isUsePoint($newStatus)) {
85 66
                $addCustomerPoint -= $newOrder->getUsePoint();
86
            }
87
88
            // 加算ポイントの更新
89
            // 変更前の対応状況が加算対象の場合、変更前の加算ポイント分を戻す
90 66
            if ($this->isAddPoint($prevStatus)) {
91 1
                if ($eventArgs->hasChangedField('add_point')) {
92
                    $addCustomerPoint -= $eventArgs->getOldValue('add_point');
93
                } else {
94 1
                    $addCustomerPoint -= $newOrder->getAddPoint();
95
                }
96
            }
97
98
            // 変更後の対応状況が加算対象の場合、変更後の加算ポイント分を足す
99 66
            if ($this->isAddPoint($newStatus)) {
100 3
                $addCustomerPoint += $newOrder->getAddPoint();
101
            }
102
103 66
            if ($addCustomerPoint != 0) {
104
                /** @var Customer $Customer */
105 5
                $Customer = $newOrder->getCustomer();
106 5
                $newPoint = $Customer->getPoint() + $addCustomerPoint;
107 5
                if ($newPoint < 0) {
108
                    // ポイントがマイナスになるためメールを送信する
109
                    $mailService = $this->getMailService();
110
                    $mailService->sendPointNotifyMail($newOrder, $Customer->getPoint(), $addCustomerPoint);
111
                }
112 5
                $Customer->setPoint($newPoint);
113 5
                $entityManager = $eventArgs->getObjectManager();
114
                // この時点で Customer は Doctrine の更新対象となっていないので, 更新対象に設定する
115 5
                $meta = $entityManager->getClassMetadata(Customer::class);
116
                // Customer の変更内容を設定する
117 5
                $entityManager->getUnitOfWork()->recomputeSingleEntityChangeSet($meta, $Customer);
118
                // Customer の ChangeSet を scheduleExtraUpdate に設定する
119 5
                $changeSet = $entityManager->getUnitOfWork()->getEntityChangeSet($Customer);
120 5
                $entityManager->getUnitOfWork()->scheduleExtraUpdate($Customer, $changeSet);
121
            }
122
        }
123
    }
124
125 66
    protected function isUsePoint(OrderStatus $Status)
126
    {
127 66
        if ($Status->getId() == OrderStatus::CANCEL) {
128 5
            return false;
129
        }
130
131 66
        return true;
132
    }
133
134 66
    protected function isAddPoint(OrderStatus $Status)
135
    {
136 66
        if ($Status->getId() == OrderStatus::DELIVERED) {
137 3
            return true;
138
        }
139
140 66
        return false;
141
    }
142
143
    /**
144
     * Returns an array of events this subscriber wants to listen to.
145
     *
146
     * @return array
147
     */
148 1279
    public function getSubscribedEvents()
0 ignored issues
show
introduced by
Declare public methods first, then protected ones and finally private ones
Loading history...
149
    {
150
        return [
151 1279
            Events::preUpdate,
152
        ];
153
    }
154
}
155