Passed
Push — master ( f91cc7...aafc79 )
by Adrien
10:59
created

Import   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 31
ccs 6
cts 16
cp 0.375
rs 10
c 1
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 29 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(): array
19
    {
20
        return [
21 1
            'name' => 'import',
22 1
            'type' => Type::nonNull(_types()->get('ImportResult')),
23 1
            'description' => 'Import a CSV file containing users',
24
            'args' => [
25 1
                'file' => Type::nonNull(_types()->get(UploadType::class)),
26
            ],
27 1
            'resolve' => function ($root, array $args, SessionInterface $session): array {
28
29
                // Check ACL
30
                $fakeOrganization = new Organization();
31
                Helper::throwIfDenied($fakeOrganization, 'create');
32
33
                /** @var UploadedFileInterface $file */
34
                $file = $args['file'];
35
36
                // Move file to tmp dir
37
                $dir = 'data/tmp/import';
38
                @mkdir($dir);
39
                $path = $dir . '/' . uniqid() . '.csv';
40
                $file->moveTo($path);
41
42
                // Do the thing
43
                $importer = new Importer();
44
                $result = $importer->import($path);
45
46
                return $result;
47 1
            },
48
        ];
49
    }
50
}
51