Passed
Pull Request — master (#137)
by
unknown
22:32 queued 18:23
created

CreateOrder::build()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 48
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 6.0184

Importance

Changes 0
Metric Value
eloc 28
dl 0
loc 48
ccs 23
cts 25
cp 0.92
rs 8.8497
c 0
b 0
f 0
cc 6
nc 1
nop 0
crap 6.0184
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
            '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 4
                        foreach ($messageQueuer->getAllEmailsToNotify() as $adminEmail) {
63
                            $message = $messageQueuer->queueAdminPendingOrder($adminEmail, $order);
64
                            $mailer->sendMessageAsync($message);
65
                        }
66
                    }
67
                }
68
69 4
                return $order;
70
            },
71
        ];
72
    }
73
}
74