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\Card; |
9
|
|
|
use Application\Model\Collection; |
10
|
|
|
use Application\Service\Importer; |
11
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
12
|
|
|
use GraphQL\Type\Definition\Type; |
13
|
|
|
|
14
|
|
|
class CreateCards implements FieldInterface |
15
|
|
|
{ |
16
|
1 |
|
public static function build(): array |
17
|
|
|
{ |
18
|
1 |
|
return [ |
19
|
1 |
|
'name' => 'createCards', |
20
|
1 |
|
'type' => Type::nonNull(Type::listOf(Type::nonNull(_types()->getOutput(Card::class)))), |
21
|
1 |
|
'description' => 'Create multiple cards from an excel files and several images', |
22
|
1 |
|
'args' => [ |
23
|
1 |
|
'excel' => Type::nonNull(_types()->get('Upload')), |
|
|
|
|
24
|
1 |
|
'images' => Type::nonNull(Type::listOf(Type::nonNull(_types()->get('Upload')))), |
25
|
1 |
|
'collection' => _types()->getId(Collection::class), |
26
|
1 |
|
], |
27
|
1 |
|
'resolve' => function (array $root, array $args): array { |
28
|
|
|
$site = $root['site']; |
29
|
|
|
|
30
|
|
|
// Check ACL |
31
|
|
|
$object = new Card(); |
32
|
|
|
$object->setSite($site); |
33
|
|
|
Helper::throwIfDenied($object, 'create'); |
34
|
|
|
|
35
|
|
|
// Get optional collection |
36
|
|
|
if ($args['collection'] ?? false) { |
37
|
|
|
$collection = $args['collection']->getEntity(); |
38
|
|
|
Helper::throwIfDenied($collection, 'update'); |
39
|
|
|
} else { |
40
|
|
|
$collection = null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// Do it |
44
|
|
|
$excel = $args['excel']; |
45
|
|
|
$images = $args['images']; |
46
|
|
|
|
47
|
|
|
$importer = new Importer(); |
48
|
|
|
$cards = $importer->import($excel, $images, $site, $collection); |
49
|
|
|
|
50
|
|
|
_em()->flush(); |
51
|
|
|
|
52
|
|
|
return $cards; |
53
|
1 |
|
}, |
54
|
1 |
|
]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|