|
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 { |
|
|
|
|
|
|
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
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.