Failed Conditions
Push — master ( 044ed2...482608 )
by Adrien
03:59
created

AccountingClosing   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 4
eloc 16
dl 0
loc 28
ccs 8
cts 16
cp 0.5
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 26 4
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 Mezzio\Session\SessionInterface;
13
14
abstract class AccountingClosing implements FieldInterface
15
{
16 1
    public static function build(): iterable
17
    {
18 1
        yield 'accountingClosing' => fn () => [
19 1
            'type' => _types()->getOutput(Transaction::class),
20 1
            'description' => 'Generate the closing entries at the end of an accounting period',
21 1
            'args' => [
22 1
                'date' => _types()->get('Date'),
23 1
            ],
24 1
            'resolve' => function ($root, array $args, SessionInterface $session): ?Transaction {
25
                global $container;
26
27
                $user = User::getCurrent();
28
                if (!$user || $user->getRole() !== User::ROLE_ADMINISTRATOR) {
29
                    throw new Exception('Seul un administrateur peut effectuer le bouclement comptable');
30
                }
31
32
                /** @var Accounting $accounting */
33
                $accounting = $container->get(Accounting::class);
34
35
                if ($args['date']) {
36
                    $transaction = $accounting->close($args['date']);
37
38
                    return $transaction;
39
                }
40
41
                throw new Exception('Date de bouclement invalide');
42 1
            },
43 1
        ];
44
    }
45
}
46