Import::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1.1715

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 27
ccs 8
cts 18
cp 0.4444
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1.1715
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\Output\ImportResultType;
9
use Application\Model\Organization;
10
use Application\Service\Importer;
11
use Ecodev\Felix\Api\Field\FieldInterface;
12
use GraphQL\Type\Definition\Type;
13
use GraphQL\Upload\UploadType;
14
use Mezzio\Session\SessionInterface;
15
use Psr\Http\Message\UploadedFileInterface;
16
17
abstract class Import implements FieldInterface
18
{
19 1
    public static function build(): iterable
20
    {
21 1
        yield 'import' => fn () => [
22 1
            'type' => Type::nonNull(_types()->get(ImportResultType::class)),
23 1
            'description' => 'Import a CSV file containing users',
24 1
            'args' => [
25 1
                'file' => Type::nonNull(_types()->get(UploadType::class)),
26 1
            ],
27 1
            'resolve' => function ($root, array $args, SessionInterface $session): array {
28
                // Check ACL
29
                $fakeOrganization = new Organization();
30
                Helper::throwIfDenied($fakeOrganization, 'create');
31
32
                /** @var UploadedFileInterface $file */
33
                $file = $args['file'];
34
35
                // Move file to tmp dir
36
                $dir = 'data/tmp/import';
37
                @mkdir($dir);
38
                $path = $dir . '/' . uniqid() . '.csv';
39
                $file->moveTo($path);
40
41
                // Do the thing
42
                $importer = new Importer();
43
                $result = $importer->import($path);
44
45
                return $result;
46 1
            },
47 1
        ];
48
    }
49
}
50