|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
|
8
|
|
|
use Application\Enum\Site; |
|
9
|
|
|
use Application\Model\Card; |
|
10
|
|
|
use Application\Model\Collection; |
|
11
|
|
|
use Application\Service\Importer; |
|
12
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
|
|
15
|
|
|
class CreateCards implements FieldInterface |
|
16
|
|
|
{ |
|
17
|
1 |
|
public static function build(): iterable |
|
18
|
|
|
{ |
|
19
|
1 |
|
yield 'createCards' => fn () => [ |
|
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 ($root, array $args): array { |
|
28
|
|
|
/** @var Site $site */ |
|
29
|
|
|
$site = $root['site']; |
|
30
|
|
|
|
|
31
|
|
|
// Check ACL |
|
32
|
|
|
$object = new Card(); |
|
33
|
|
|
$object->setSite($site); |
|
34
|
|
|
Helper::throwIfDenied($object, 'create'); |
|
35
|
|
|
|
|
36
|
|
|
// Get optional collection |
|
37
|
|
|
if ($args['collection'] ?? false) { |
|
38
|
|
|
$collection = $args['collection']->getEntity(); |
|
39
|
|
|
Helper::throwIfDenied($collection, 'update'); |
|
40
|
|
|
} else { |
|
41
|
|
|
$collection = null; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Do it |
|
45
|
|
|
$excel = $args['excel']; |
|
46
|
|
|
$images = $args['images']; |
|
47
|
|
|
|
|
48
|
|
|
$importer = new Importer($site); |
|
49
|
|
|
$cards = $importer->import($excel, $images, $collection); |
|
50
|
|
|
|
|
51
|
|
|
_em()->flush(); |
|
52
|
|
|
|
|
53
|
|
|
return $cards; |
|
54
|
1 |
|
}, |
|
55
|
1 |
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|