Passed
Push — master ( a8b983...5b3176 )
by Adrien
10:48
created

CreateOrder::build()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 30
nc 1
nop 0
dl 0
loc 51
ccs 28
cts 28
cp 1
crap 6
rs 8.8177
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 4
    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
            'resolve' => function ($root, array $args, SessionInterface $session): ?Order {
32 4
                global $container;
33
                /** @var Mailer $mailer */
34 4
                $mailer = $container->get(Mailer::class);
35
36
                /** @var MessageQueuer $messageQueuer */
37 4
                $messageQueuer = $container->get(MessageQueuer::class);
38
39 4
                Helper::throwIfDenied(new Order(), 'create');
40
41 3
                $input = $args['input'];
42 3
                $input['orderLines'] = array_map(fn ($line) => Utility::entityIdToModel($line), $input['orderLines']);
43 2
                $input['country'] = isset($input['country']) ? $input['country']->getEntity() : null;
44
45
                /** @var Invoicer $invoicer */
46 2
                $invoicer = $container->get(Invoicer::class);
47 2
                $order = $invoicer->createOrder($input);
48
49 2
                _em()->flush();
50
51 2
                if ($order) {
52 2
                    _em()->refresh($order);
53
54
                    // Notify people now if payment is not instantaneous
55 2
                    if ($order->getPaymentMethod() !== PaymentMethodType::DATATRANS) {
56 2
                        $user = $order->getOwner();
57 2
                        if ($user) {
58 2
                            $message = $messageQueuer->queueUserPendingOrder($user, $order);
59 2
                            $mailer->sendMessageAsync($message);
60
                        }
61
62
                        /** @var UserRepository $repository */
63 2
                        $repository = _em()->getRepository(User::class);
64 2
                        $admins = $repository->getAllAdministratorsToNotify();
65 2
                        foreach ($admins as $admin) {
66 2
                            $message = $messageQueuer->queueAdminPendingOrder($admin, $order);
67 2
                            $mailer->sendMessageAsync($message);
68
                        }
69
                    }
70
                }
71
72 2
                return $order;
73 1
            },
74
        ];
75
    }
76
}
77