Failed Conditions
Push — master ( ab912b...01f77f )
by Adrien
10:12
created

ImportCamt   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 32
rs 10
ccs 16
cts 16
cp 1
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 30 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\Transaction;
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 ImportCamt implements FieldInterface
17
{
18 1
    public static function build(): array
19
    {
20
        return [
21 1
            'name' => 'importCamt',
22 1
            'type' => Type::nonNull(Type::listOf(Type::nonNull(_types()->getOutput(Transaction::class)))),
23 1
            'description' => 'Import a CAMT file containing BVR transactions',
24
            'args' => [
25 1
                'file' => Type::nonNull(_types()->get(UploadType::class)),
26
            ],
27
            'resolve' => function ($root, array $args, SessionInterface $session): array {
28
29
                // Check ACL
30 1
                $fakeTransaction = new Transaction();
31 1
                Helper::throwIfDenied($fakeTransaction, 'create');
32
33
                /** @var UploadedFileInterface $file */
34 1
                $file = $args['file'];
35
36
                // Move file to tmp dir
37 1
                $dir = 'data/tmp/camt';
38 1
                @mkdir($dir);
39 1
                $path = $dir . '/' . uniqid() . '.xml';
40 1
                $file->moveTo($path);
41
42
                // Do the thing
43 1
                $importer = new Importer();
44 1
                $transactions = $importer->import($path);
45 1
                _em()->flush();
46
47 1
                return $transactions;
48 1
            },
49
        ];
50
    }
51
}
52