1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
6
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
8
|
|
|
use Application\Enum\PaymentMethod; |
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(): iterable |
21
|
|
|
{ |
22
|
1 |
|
yield 'createOrder' => fn () => [ |
23
|
1 |
|
'type' => _types()->getOutput(Order::class), |
24
|
1 |
|
'description' => 'Make an order to the shop.', |
25
|
1 |
|
'args' => [ |
26
|
1 |
|
'input' => Type::nonNull(_types()->get('OrderInput')), |
27
|
1 |
|
], |
28
|
1 |
|
'resolve' => function ($root, array $args, SessionInterface $session): ?Order { |
29
|
|
|
global $container; |
30
|
|
|
/** @var Mailer $mailer */ |
31
|
7 |
|
$mailer = $container->get(Mailer::class); |
32
|
|
|
|
33
|
|
|
/** @var MessageQueuer $messageQueuer */ |
34
|
7 |
|
$messageQueuer = $container->get(MessageQueuer::class); |
35
|
|
|
|
36
|
7 |
|
Helper::throwIfDenied(new Order(), 'create'); |
37
|
|
|
|
38
|
6 |
|
$input = $args['input']; |
39
|
6 |
|
$input['orderLines'] = array_map(fn ($line) => Utility::entityIdToModel($line), $input['orderLines']); |
40
|
5 |
|
$input['country'] = isset($input['country']) ? $input['country']->getEntity() : null; |
41
|
|
|
|
42
|
|
|
/** @var Invoicer $invoicer */ |
43
|
5 |
|
$invoicer = $container->get(Invoicer::class); |
44
|
5 |
|
$order = $invoicer->createOrder($input); |
45
|
|
|
|
46
|
4 |
|
_em()->flush(); |
47
|
|
|
|
48
|
4 |
|
if ($order) { |
49
|
4 |
|
_em()->refresh($order); |
50
|
|
|
|
51
|
|
|
// Notify people now if payment is not instantaneous |
52
|
4 |
|
if ($order->getPaymentMethod() !== PaymentMethod::Datatrans) { |
53
|
4 |
|
$user = $order->getOwner(); |
54
|
4 |
|
if ($user) { |
55
|
4 |
|
$message = $messageQueuer->queueUserPendingOrder($user, $order); |
56
|
4 |
|
$mailer->sendMessageAsync($message); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) { |
60
|
|
|
$message = $messageQueuer->queueAdminPendingOrder($adminEmail, $order); |
61
|
|
|
$mailer->sendMessageAsync($message); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
return $order; |
67
|
1 |
|
}, |
68
|
1 |
|
]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|