Failed Conditions
Push — master ( 5fbadf...673c47 )
by Adrien
07:14
created

CreateTransaction::build()   A

Complexity

Conditions 5
Paths 1

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 28
nc 1
nop 0
dl 0
loc 43
rs 9.1608
c 0
b 0
f 0
ccs 26
cts 26
cp 1
crap 5
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\Api\Helper;
10
use Application\Model\Transaction;
11
use Application\Model\TransactionLine;
12
use GraphQL\Type\Definition\Type;
13
use Zend\Expressive\Session\SessionInterface;
14
15
abstract class CreateTransaction implements FieldInterface
16
{
17 2
    public static function build(): array
18
    {
19
        return [
20 1
            'name' => 'createTransaction',
21 1
            'type' => Type::nonNull(_types()->getOutput(Transaction::class)),
22 1
            'description' => 'Create a transaction with all its transaction lines. Everything will be read-only forever.',
23
            'args' => [
24 1
                'input' => Type::nonNull(_types()->getInput(Transaction::class)),
25 1
                'lines' => Type::nonNull(Type::listOf(Type::nonNull(_types()->getInput(TransactionLine::class)))),
26
            ],
27
            'resolve' => function ($root, array $args, SessionInterface $session): Transaction {
1 ignored issue
show
Unused Code introduced by
The parameter $session is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

27
            'resolve' => function ($root, array $args, /** @scrutinizer ignore-unused */ SessionInterface $session): Transaction {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
28
                // Do it
29 2
                $transaction = new Transaction();
30 2
                $input = $args['input'];
31 2
                Helper::hydrate($transaction, $input);
32
33
                // Check ACL
34 2
                Helper::throwIfDenied($transaction, 'create');
35
36 2
                $accounts = [];
37 2
                foreach ($args['lines'] as $line) {
38 2
                    $transactionLine = new TransactionLine();
39 2
                    Helper::hydrate($transactionLine, $line);
40 2
                    if (!$transactionLine->getCredit() && !$transactionLine->getDebit()) {
41 1
                        throw new Exception('Cannot create a transaction line without any account');
42
                    }
43 1
                    $accounts[] = $transactionLine->getCredit();
44 1
                    $accounts[] = $transactionLine->getDebit();
45
46 1
                    $transactionLine->setTransaction($transaction);
47 1
                    _em()->persist($transactionLine);
48
                }
49
50 1
                _em()->persist($transaction);
51 1
                _em()->flush();
52
53
                // Be sure to refresh the new account balance that were computed by DB triggers
54 1
                $accounts = array_filter(array_unique($accounts, SORT_REGULAR));
55 1
                foreach ($accounts as $account) {
56 1
                    _em()->refresh($account);
57
                }
58
59 1
                return $transaction;
60 1
            },
61
        ];
62
    }
63
}
64