AccountingClosing::build()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
ccs 8
cts 16
cp 0.5
rs 9.7666
c 0
b 0
f 0
cc 4
nc 1
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Model\Transaction;
8
use Application\Model\User;
9
use Application\Service\Accounting;
10
use Ecodev\Felix\Api\Exception;
11
use Ecodev\Felix\Api\Field\FieldInterface;
12
use Ecodev\Felix\Api\Scalar\DateType;
13
use Mezzio\Session\SessionInterface;
14
15
abstract class AccountingClosing implements FieldInterface
16
{
17 1
    public static function build(): iterable
18
    {
19 1
        yield 'accountingClosing' => fn () => [
20 1
            'type' => _types()->getOutput(Transaction::class),
21 1
            'description' => 'Generate the closing entries at the end of an accounting period',
22 1
            'args' => [
23 1
                'date' => _types()->get(DateType::class),
24 1
            ],
25 1
            'resolve' => function ($root, array $args, SessionInterface $session): ?Transaction {
26
                global $container;
27
28
                $user = User::getCurrent();
29
                if (!$user || $user->getRole() !== User::ROLE_ADMINISTRATOR) {
30
                    throw new Exception('Seul un administrateur peut effectuer le bouclement comptable');
31
                }
32
33
                /** @var Accounting $accounting */
34
                $accounting = $container->get(Accounting::class);
35
36
                if ($args['date']) {
37
                    $transaction = $accounting->close($args['date']);
38
39
                    return $transaction;
40
                }
41
42
                throw new Exception('Date de bouclement invalide');
43 1
            },
44 1
        ];
45
    }
46
}
47