Passed
Push — master ( 528c5a...0afffa )
by Sylvain
07:58
created

CreateOrder::build()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.0146

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 29
nc 1
nop 0
dl 0
loc 50
ccs 25
cts 27
cp 0.9259
crap 6.0146
rs 8.8337
c 1
b 0
f 0
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\Model\User;
11
use Application\Repository\UserRepository;
12
use Application\Service\Invoicer;
13
use Application\Service\MessageQueuer;
14
use Ecodev\Felix\Api\Field\FieldInterface;
15
use Ecodev\Felix\Service\Mailer;
16
use Ecodev\Felix\Utility;
17
use GraphQL\Type\Definition\Type;
18
use Mezzio\Session\SessionInterface;
19
20
abstract class CreateOrder implements FieldInterface
21
{
22 7
    public static function build(): array
23
    {
24
        return [
25 1
            'name' => 'createOrder',
26 1
            'type' => _types()->getOutput(Order::class),
27 1
            'description' => 'Make an order to the shop.',
28
            'args' => [
29 1
                'input' => Type::nonNull(_types()->get('OrderInput')),
30
            ],
31 1
            'resolve' => function ($root, array $args, SessionInterface $session): ?Order {
32
                global $container;
33
                /** @var Mailer $mailer */
34 7
                $mailer = $container->get(Mailer::class);
35
36
                /** @var MessageQueuer $messageQueuer */
37 7
                $messageQueuer = $container->get(MessageQueuer::class);
38
39 7
                Helper::throwIfDenied(new Order(), 'create');
40
41 6
                $input = $args['input'];
42 6
                $input['orderLines'] = array_map(fn ($line) => Utility::entityIdToModel($line), $input['orderLines']);
43 5
                $input['country'] = isset($input['country']) ? $input['country']->getEntity() : null;
44
45
                /** @var Invoicer $invoicer */
46 5
                $invoicer = $container->get(Invoicer::class);
47 5
                $order = $invoicer->createOrder($input);
48
49 4
                _em()->flush();
50
51 4
                if ($order) {
52 4
                    _em()->refresh($order);
53
54
                    // Notify people now if payment is not instantaneous
55 4
                    if ($order->getPaymentMethod() !== PaymentMethodType::DATATRANS) {
56 4
                        $user = $order->getOwner();
57 4
                        if ($user) {
58 4
                            $message = $messageQueuer->queueUserPendingOrder($user, $order);
59 4
                            $mailer->sendMessageAsync($message);
60
                        }
61
62
                        /** @var UserRepository $repository */
63 4
                        $repository = _em()->getRepository(User::class);
0 ignored issues
show
Unused Code introduced by
The assignment to $repository is dead and can be removed.
Loading history...
64 4
                        foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
65
                            $message = $messageQueuer->queueAdminPendingOrder($adminEmail, $order);
66
                            $mailer->sendMessageAsync($message);
67
                        }
68
                    }
69
                }
70
71 4
                return $order;
72 1
            },
73
        ];
74
    }
75
}
76