CreateCard   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
eloc 20
dl 0
loc 38
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 36 2
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 Ecodev\Felix\Api\Field\FieldInterface;
11
use GraphQL\Type\Definition\Type;
12
13
/**
14
 * Custom implementation to have an optional collection.
15
 */
16
class CreateCard implements FieldInterface
17
{
18 7
    public static function build(): iterable
19
    {
20 1
        yield 'createCard' => fn () => [
21 1
            'type' => Type::nonNull(_types()->getOutput(Card::class)),
22 1
            'description' => 'Create a new card',
23 1
            'args' => [
24 1
                'input' => Type::nonNull(_types()->getInput(Card::class)),
25 1
                'collection' => _types()->getId(Collection::class),
26 1
            ],
27 1
            'resolve' => function ($root, array $args): Card {
28
                // Check ACL
29 7
                $object = new Card();
30
31
                // Be sure that site is set first
32 7
                $input = $args['input'];
33 7
                Helper::hydrate($object, ['site' => $input['site']]);
34
35
                // Check ACL
36 7
                Helper::throwIfDenied($object, 'create');
37
38
                // Do it
39 7
                Helper::hydrate($object, $input);
40
41
                // Affect optional collection
42 7
                if ($args['collection'] ?? false) {
43
                    /** @var Collection $collection */
44 5
                    $collection = $args['collection']->getEntity();
45 3
                    Helper::throwIfDenied($collection, 'linkCard');
46
47 2
                    $object->addCollection($collection);
48
                }
49
50 4
                _em()->persist($object);
51 4
                _em()->flush();
52
53 4
                return $object;
54 1
            },
55 1
        ];
56
    }
57
}
58