ExportAccountingReport::build()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3.9566

Importance

Changes 0
Metric Value
eloc 18
c 0
b 0
f 0
dl 0
loc 30
ccs 10
cts 19
cp 0.5263
rs 9.6666
cc 3
nc 1
nop 0
crap 3.9566
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Model\Account;
8
use Application\Repository\AccountRepository;
9
use Application\Service\Exporter\AccountingReport;
10
use Ecodev\Felix\Api\Field\FieldInterface;
11
use Ecodev\Felix\Api\Scalar\DateType;
12
use GraphQL\Type\Definition\Type;
13
use Mezzio\Session\SessionInterface;
14
15
abstract class ExportAccountingReport implements FieldInterface
16
{
17 1
    public static function build(): iterable
18
    {
19 1
        yield 'exportAccountingReport' => fn () => [
20 1
            'type' => Type::nonNull(Type::string()),
21 1
            'description' => 'Prepare an accounting report and return the URL to download it',
22 1
            'args' => [
23 1
                'date' => Type::nonNull(_types()->get(DateType::class)),
24 1
                'datePrevious' => _types()->get(DateType::class),
25 1
                'showBudget' => Type::nonNull(Type::boolean()),
26 1
            ],
27 1
            'resolve' => function ($root, array $args, SessionInterface $session): string {
28
                global $container;
29
30
                /** @var AccountingReport $exporter */
31
                $exporter = $container->get(AccountingReport::class);
32
33
                if ($args['date']) {
34
                    $exporter->setDate($args['date']);
35
                }
36
                if ($args['datePrevious']) {
37
                    $exporter->setDatePrevious($args['datePrevious']);
38
                }
39
                $exporter->showBudget($args['showBudget']);
40
41
                // Select root accounts
42
                /** @var AccountRepository $accountRepository */
43
                $accountRepository = _em()->getRepository(Account::class);
44
                $rootAccountsQuery = $accountRepository->getRootAccountsQuery();
45
46
                return $exporter->export($rootAccountsQuery->getResult());
47 1
            },
48 1
        ];
49
    }
50
}
51