-
Notifications
You must be signed in to change notification settings - Fork 17
/
UpdateShippedMutation.php
283 lines (247 loc) · 8.92 KB
/
UpdateShippedMutation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\Api\GraphQL\Mutation;
use Doctrine\ORM\EntityManager;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\Master\OrderStatus;
use Eccube\Entity\Order;
use Eccube\Entity\Shipping;
use Eccube\Repository\Master\OrderStatusRepository;
use Eccube\Repository\ShippingRepository;
use Eccube\Service\MailService;
use Eccube\Service\OrderStateMachine;
use GraphQL\Type\Definition\Type;
use Plugin\Api\GraphQL\Error\InvalidArgumentException;
use Plugin\Api\GraphQL\Mutation;
use Plugin\Api\GraphQL\Type\Definition\DateTimeType;
use Plugin\Api\GraphQL\Types;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validation;
class UpdateShippedMutation implements Mutation
{
/**
* @var EccubeConfig
*/
private $eccubeConfig;
/**
* @var EntityManager
*/
private $entityManager;
/**
* @var MailService
*/
private $mailService;
/**
* @var OrderStateMachine
*/
private $orderStateMachine;
/**
* @var OrderStatusRepository
*/
private $orderStatusRepository;
/**
* @var Types
*/
private $types;
/**
* @var ShippingRepository
*/
private $shippingRepository;
public function __construct(
EccubeConfig $eccubeConfig,
EntityManager $entityManager,
MailService $mailService,
OrderStateMachine $orderStateMachine,
OrderStatusRepository $orderStatusRepository,
Types $types,
ShippingRepository $shippingRepository
) {
$this->eccubeConfig = $eccubeConfig;
$this->entityManager = $entityManager;
$this->mailService = $mailService;
$this->orderStateMachine = $orderStateMachine;
$this->orderStatusRepository = $orderStatusRepository;
$this->types = $types;
$this->shippingRepository = $shippingRepository;
}
public function getName()
{
return 'updateShipped';
}
public function getMutation()
{
return [
'type' => $this->types->get(Shipping::class),
'args' => [
'id' => [
'type' => Type::nonNull(Type::id()),
'description' => trans('api.update_shipped.args.description.id'),
],
'shipping_date' => [
'type' => DateTimeType::dateTime(),
'description' => trans('api.update_shipped.args.description.shipping_date'),
],
'shipping_delivery_name' => [
'type' => Type::string(),
'description' => trans('api.update_shipped.args.description.shipping_delivery_name'),
],
'tracking_number' => [
'type' => Type::string(),
'description' => trans('api.update_shipped.args.description.tracking_number'),
],
'note' => [
'type' => Type::string(),
'description' => trans('api.update_shipped.args.description.note'),
],
'is_send_mail' => [
'type' => Type::boolean(),
'defaultValue' => false,
'description' => trans('api.update_shipped.args.description.is_send_mail'),
],
],
'resolve' => [$this, 'updateShipped'],
];
}
public function updateShipped($root, $args)
{
// 引数の検証
$this->validateArgs($args);
/** @var Shipping $Shipping */
$Shipping = $this->shippingRepository->find($args['id']);
// 出荷処理が可能かの検証
$this->validateShippable($Shipping);
// args で Shipping の出荷済み処理
$this->updateShippingShippedWithArgs($Shipping, $args);
// Order の出荷済み処理
$this->updateOrderShipped($Shipping->getOrder());
// is_send_mail が true の場合は発送通知メールを送信する
if (array_key_exists('is_send_mail', $args) && $args['is_send_mail']) {
$this->sendShippingNotifyMail($Shipping);
}
$this->entityManager->flush();
return $Shipping;
}
/**
* 引数の検証
*
* @throws InvalidArgumentException
*/
private function validateArgs(array $args): void
{
$validator = Validation::createValidator();
$constraint = $this->getConstraint();
$violations = $validator->validate($args, $constraint);
if (count($violations)) {
$message = '';
/** @var ConstraintViolationInterface $violation */
foreach ($violations as $violation) {
$message .= sprintf('%s: %s;', $violation->getPropertyPath(), $violation->getMessage());
}
throw new InvalidArgumentException($message);
}
}
private function getConstraint(): Constraint
{
return new Assert\Collection([
'fields' => [
'id' => new Assert\GreaterThan(0),
'shipping_date' => new Assert\DateTime(),
'shipping_delivery_name' => new Assert\Length([
'max' => $this->eccubeConfig['eccube_mtext_len'],
]),
'tracking_number' => new Assert\Length([
'max' => $this->eccubeConfig['eccube_mtext_len'],
]),
'note' => new Assert\Length([
'max' => $this->eccubeConfig['eccube_ltext_len'],
]),
'is_send_mail' => new Assert\Choice([true, false]),
],
'allowMissingFields' => true,
]);
}
/**
* 出荷処理が可能かの検証
*
* @throws InvalidArgumentException
*/
private function validateShippable(?Shipping $Shipping): void
{
// Shipping が存在しない場合
if (is_null($Shipping)) {
throw new InvalidArgumentException('id: No Shipping found;');
}
// Shipping がすでに出荷済みの場合
if ($Shipping->isShipped()) {
throw new InvalidArgumentException('id: Already shipped;');
}
/** @var OrderStatus $OrderStatus */
$OrderStatus = $this->orderStatusRepository->find(OrderStatus::DELIVERED);
// Order が出荷済みへ変更できない場合
if (!$this->orderStateMachine->can($Shipping->getOrder(), $OrderStatus)) {
throw new InvalidArgumentException('id: The associated order cannot be shipped;');
}
}
/**
* args で Shipping の出荷済み処理
*/
private function updateShippingShippedWithArgs(Shipping $Shipping, array $args): void
{
// Shipping を出荷済みに変更
$Shipping->setShippingDate($args['shipping_date'] ?? new \DateTime());
// shipping_delivery_name が指定されている場合は配送社名を更新
if (array_key_exists('shipping_delivery_name', $args)) {
$Shipping->setShippingDeliveryName($args['shipping_delivery_name']);
}
// tracking_number が指定されている場合は配送番号を更新
if (array_key_exists('tracking_number', $args)) {
$Shipping->setTrackingNumber($args['tracking_number']);
}
// note が指定されている場合は出荷用メモ欄を更新
if (array_key_exists('note', $args)) {
$Shipping->setNote($args['note']);
}
}
/**
* Order の出荷済み処理
*/
private function updateOrderShipped(Order $Order): void
{
// Order に紐づく全ての Shipping が出荷済みか
$allShipped = array_reduce($Order->getShippings()->toArray(), function ($carry, $Shipping) {
return $carry && $Shipping->isShipped();
}, true);
/** @var OrderStatus $OrderStatus */
$OrderStatus = $this->orderStatusRepository->find(OrderStatus::DELIVERED);
// Shipping がすべて出荷済みなら Order を出荷済みに変更
if ($allShipped) {
$this->orderStateMachine->apply($Order, $OrderStatus);
}
}
/**
* 発送通知メールを送信する
*
* @throws InvalidArgumentException
*/
private function sendShippingNotifyMail(Shipping $Shipping): void
{
try {
$this->mailService->sendShippingNotifyMail($Shipping);
$Shipping->setMailSendDate(new \DateTime());
} catch (\Exception $e) {
// メール送信エラー
throw new InvalidArgumentException('is_send_mail: Error sending email;');
}
}
}