Passed
Push — master ( 1d5fb4...e5c3c4 )
by Sam
06:33 queued 01:55
created

Import   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
wmc 1
eloc 18
dl 0
loc 30
ccs 5
cts 15
cp 0.3333
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 28 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
            '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
                // 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
            },
47
        ];
48
    }
49
}
50