Failed Conditions
Push — master ( ce2a97...d73709 )
by Adrien
13:38 queued 11:33
created

Import   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 44.44%

Importance

Changes 0
Metric Value
wmc 1
eloc 17
dl 0
loc 29
ccs 8
cts 18
cp 0.4444
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 27 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Model\Organization;
9
use Application\Service\Importer;
10
use Ecodev\Felix\Api\Field\FieldInterface;
11
use GraphQL\Type\Definition\Type;
12
use GraphQL\Upload\UploadType;
13
use Mezzio\Session\SessionInterface;
14
use Psr\Http\Message\UploadedFileInterface;
15
16
abstract class Import implements FieldInterface
17
{
18 1
    public static function build(): iterable
19
    {
20 1
        yield 'import' => fn () => [
21 1
            'type' => Type::nonNull(_types()->get('ImportResult')),
22 1
            'description' => 'Import a CSV file containing users',
23 1
            'args' => [
24 1
                'file' => Type::nonNull(_types()->get(UploadType::class)),
25 1
            ],
26 1
            'resolve' => function ($root, array $args, SessionInterface $session): array {
27
                // Check ACL
28
                $fakeOrganization = new Organization();
29
                Helper::throwIfDenied($fakeOrganization, 'create');
30
31
                /** @var UploadedFileInterface $file */
32
                $file = $args['file'];
33
34
                // Move file to tmp dir
35
                $dir = 'data/tmp/import';
36
                @mkdir($dir);
37
                $path = $dir . '/' . uniqid() . '.csv';
38
                $file->moveTo($path);
39
40
                // Do the thing
41
                $importer = new Importer();
42
                $result = $importer->import($path);
43
44
                return $result;
45 1
            },
46 1
        ];
47
    }
48
}
49