1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
8
|
|
|
use Application\DBAL\Types\PaymentMethodType; |
9
|
|
|
use Application\Model\Order; |
10
|
|
|
use Application\Service\Invoicer; |
11
|
|
|
use Application\Service\MessageQueuer; |
12
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
13
|
|
|
use Ecodev\Felix\Service\Mailer; |
14
|
|
|
use Ecodev\Felix\Utility; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Mezzio\Session\SessionInterface; |
17
|
|
|
|
18
|
|
|
abstract class CreateOrder implements FieldInterface |
19
|
|
|
{ |
20
|
7 |
|
public static function build(): array |
21
|
|
|
{ |
22
|
1 |
|
return [ |
23
|
1 |
|
'name' => 'createOrder', |
24
|
1 |
|
'type' => _types()->getOutput(Order::class), |
25
|
1 |
|
'description' => 'Make an order to the shop.', |
26
|
1 |
|
'args' => [ |
27
|
1 |
|
'input' => Type::nonNull(_types()->get('OrderInput')), |
28
|
1 |
|
], |
29
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): ?Order { |
30
|
|
|
global $container; |
31
|
|
|
/** @var Mailer $mailer */ |
32
|
7 |
|
$mailer = $container->get(Mailer::class); |
33
|
|
|
|
34
|
|
|
/** @var MessageQueuer $messageQueuer */ |
35
|
7 |
|
$messageQueuer = $container->get(MessageQueuer::class); |
36
|
|
|
|
37
|
7 |
|
Helper::throwIfDenied(new Order(), 'create'); |
38
|
|
|
|
39
|
6 |
|
$input = $args['input']; |
40
|
6 |
|
$input['orderLines'] = array_map(fn ($line) => Utility::entityIdToModel($line), $input['orderLines']); |
41
|
5 |
|
$input['country'] = isset($input['country']) ? $input['country']->getEntity() : null; |
42
|
|
|
|
43
|
|
|
/** @var Invoicer $invoicer */ |
44
|
5 |
|
$invoicer = $container->get(Invoicer::class); |
45
|
5 |
|
$order = $invoicer->createOrder($input); |
46
|
|
|
|
47
|
4 |
|
_em()->flush(); |
48
|
|
|
|
49
|
4 |
|
if ($order) { |
50
|
4 |
|
_em()->refresh($order); |
51
|
|
|
|
52
|
|
|
// Notify people now if payment is not instantaneous |
53
|
4 |
|
if ($order->getPaymentMethod() !== PaymentMethodType::DATATRANS) { |
54
|
4 |
|
$user = $order->getOwner(); |
55
|
4 |
|
if ($user) { |
56
|
4 |
|
$message = $messageQueuer->queueUserPendingOrder($user, $order); |
57
|
4 |
|
$mailer->sendMessageAsync($message); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) { |
61
|
|
|
$message = $messageQueuer->queueAdminPendingOrder($adminEmail, $order); |
62
|
|
|
$mailer->sendMessageAsync($message); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
return $order; |
68
|
1 |
|
}, |
69
|
1 |
|
]; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|