CreateOrder::build()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 47
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 6.0131

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 47
ccs 26
cts 28
cp 0.9286
rs 8.8657
c 0
b 0
f 0
cc 6
nc 1
nop 0
crap 6.0131
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Api\Input\OrderInputType;
9
use Application\Enum\PaymentMethod;
10
use Application\Model\Order;
11
use Application\Service\Invoicer;
12
use Application\Service\MessageQueuer;
13
use Ecodev\Felix\Api\Field\FieldInterface;
14
use Ecodev\Felix\Service\Mailer;
15
use Ecodev\Felix\Utility;
16
use GraphQL\Type\Definition\Type;
17
use Mezzio\Session\SessionInterface;
18
19
abstract class CreateOrder implements FieldInterface
20
{
21 7
    public static function build(): iterable
22
    {
23 1
        yield 'createOrder' => fn () => [
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(OrderInputType::class)),
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() !== PaymentMethod::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