Completed
Push — master ( dc6e13...340cc5 )
by Adrien
07:39
created

Invoice::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3.004

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 1
nop 0
dl 0
loc 25
rs 9.7666
c 0
b 0
f 0
ccs 12
cts 13
cp 0.9231
crap 3.004
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Exception;
8
use Application\Api\Field\FieldInterface;
9
use Application\Model\User;
10
use Application\Service\Invoicer;
11
use GraphQL\Type\Definition\Type;
12
use Zend\Expressive\Session\SessionInterface;
13
14
abstract class Invoice implements FieldInterface
15
{
16 1
    public static function build(): array
17
    {
18
        return [
19 1
            'name' => 'invoice',
20 1
            'type' => Type::nonNull(Type::boolean()),
21 1
            'description' => 'Create transactions and send a notification to invoice the given user. It may do nothing at all if not necessary',
22
            'args' => [
23 1
                'id' => Type::nonNull(_types()->getId(User::class)),
24
            ],
25
            'resolve' => function ($root, array $args, SessionInterface $session): bool {
26 1
                global $container;
27
28
                // TODO: poor way to do ACL check, we should find something better integrated in real ACL
29 1
                $user = User::getCurrent();
30 1
                if (!$user || !in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
31
                    throw new Exception('Not allowed to send invoices');
32
                }
33
34
                /** @var Invoicer $invoicer */
35 1
                $invoicer = $container->get(Invoicer::class);
36 1
                $userToInvoice = $args['id']->getEntity();
37
38 1
                $invoicer->invoice($userToInvoice);
39
40 1
                return true;
41 1
            },
42
        ];
43
    }
44
}
45