DeleteMutationGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 29
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 20 3
1
<?php
2
3
namespace DeInternetJongens\LighthouseUtils\Generators\Mutations;
4
5
use DeInternetJongens\LighthouseUtils\Generators\Arguments\IdArgumentGenerator;
6
use DeInternetJongens\LighthouseUtils\Models\GraphQLSchema;
7
use GraphQL\Type\Definition\Type;
8
9
class DeleteMutationGenerator
10
{
11
    /**
12
     * Generates a GraphQL mutation that deletes a record
13
     *
14
     * @param string $typeName
15
     * @param Type[] $typeFields
16
     * @return string
17
     */
18
    public static function generate(string $typeName, array $typeFields): string
19
    {
20
        $mutationName = 'delete' . $typeName;
21
        $arguments = IdArgumentGenerator::generate($typeFields);
22
23
        if (count($arguments) < 1) {
24
            return '';
25
        }
26
27
        $mutation = sprintf('    %s(%s)', $mutationName, implode(', ', $arguments));
28
        $mutation .= sprintf(': %1$s @delete', $typeName);
29
30
        if (config('lighthouse-utils.authorization')) {
31
            $permission = sprintf('delete%1$s', $typeName);
32
            $mutation .= sprintf(' @can(if: "%1$s", model: "User")', $permission);
33
        }
34
35
        GraphQLSchema::register($mutationName, $typeName, 'mutation', $permission ?? null);
36
37
        return $mutation;
38
    }
39
}
40