CreateExport   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 0
Metric Value
wmc 5
eloc 34
dl 0
loc 68
ccs 33
cts 35
cp 0.9429
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 25 2
A create() 0 37 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Api\Input\CreateExportInputType;
9
use Application\Enum\ExportFormat;
10
use Application\Model\Export;
11
use Application\Repository\ExportRepository;
12
use Application\Service\Exporter\Exporter;
13
use Ecodev\Felix\Api\Exception;
14
use Ecodev\Felix\Api\Field\FieldInterface;
15
use Ecodev\Felix\Utility;
16
use GraphQL\Type\Definition\Type;
17
18
/**
19
 * Custom implementation to inject lots of cards via collection without loading each cards.
20
 */
21
class CreateExport implements FieldInterface
22
{
23 1
    public static function build(): iterable
24
    {
25 1
        yield 'createExport' => fn () => [
26 1
            'type' => Type::nonNull(_types()->getOutput(Export::class)),
27 1
            'description' => 'Create a new export',
28 1
            'args' => [
29 1
                'input' => Type::nonNull(_types()->get(CreateExportInputType::class)),
30 1
            ],
31 1
            'resolve' => function ($root, array $args): Export {
32
                global $container;
33
34
                /** @var Exporter $exporter */
35 1
                $exporter = $container->get(Exporter::class);
36
37 1
                $cardCount = 0;
38 1
                $export = self::create($args, $cardCount);
39
40
                // Do small export right now, do big one async
41 1
                if ($cardCount < 200) {
42 1
                    $export = $exporter->export($export);
43
                } else {
44
                    $exporter->exportAsync($export);
45
                }
46
47 1
                return $export;
48 1
            },
49 1
        ];
50
    }
51
52 2
    public static function create(array $args, int &$cardCount = 0): Export
53
    {
54
        // Check ACL
55 2
        $export = new Export();
56 2
        $input = $args['input'];
57
58 2
        $collectionIds = Utility::modelToId($input['collections']);
59 2
        $cardIds = Utility::modelToId($input['cards']);
60 2
        unset($input['collections'], $input['cards']);
61
62
        // Be sure that site is set first
63 2
        Helper::hydrate($export, ['site' => $input['site']]);
64
65
        // Check ACL
66 2
        Helper::throwIfDenied($export, 'create');
67
68
        // Do it
69 2
        Helper::hydrate($export, $input);
70
71 2
        _em()->persist($export);
72 2
        _em()->flush();
73
74
        // Actually inject all selected cards into export (either hand-picked or via collection)
75
        /** @var ExportRepository $exportRepository */
76 2
        $exportRepository = _em()->getRepository(Export::class);
77 2
        $cardCount = $exportRepository->updateCards($export, $collectionIds, $cardIds);
78
79
        global $container;
80
81
        /** @var array */
82 2
        $config = $container->get('config');
83 2
        $exportPptxMaximumCardCount = $config['exportPptxMaximumCardCount'];
84 2
        if ($export->getFormat() === ExportFormat::Pptx && $cardCount > $exportPptxMaximumCardCount) {
85
            throw new Exception("L'export en PPTX est limité à $exportPptxMaximumCardCount fiches, mais la sélection actuelle est de $cardCount fiches.");
86
        }
87
88 2
        return $export;
89
    }
90
}
91