Passed
Push — master ( 73a53e...f91dab )
by Bruno
07:25 queued 04:03
created

MigrationIndexDirective   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 35
ccs 8
cts 14
cp 0.5714
rs 10
c 1
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A processMigrationFieldDirective() 0 7 1
A processMigrationTypeDirective() 0 15 3
A processMigrationRelationshipDirective() 0 7 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Modelarium\Exception\Exception;
6
use Modelarium\Laravel\Targets\MigrationGenerator;
7
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
8
use Modelarium\Laravel\Targets\MigrationCodeFragment;
9
10
class MigrationIndexDirective implements MigrationDirectiveInterface
11
{
12 1
    public static function processMigrationTypeDirective(
13
        MigrationGenerator $generator,
14
        \GraphQL\Language\AST\DirectiveNode $directive
15
    ): void {
16
        /** @phpstan-ignore-next-line */
17 1
        $values = $directive->arguments[0]->value->values;
0 ignored issues
show
Bug introduced by
Accessing values on the interface GraphQL\Language\AST\ValueNode suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
18
19 1
        $indexFields = [];
20 1
        foreach ($values as $value) {
21 1
            $indexFields[] = $value->value;
22
        }
23 1
        if (!count($indexFields)) {
24
            throw new Exception("You must provide at least one field to an index");
25
        }
26 1
        $generator->createCode[] ='$table->index("' . implode('", "', $indexFields) .'");';
27 1
    }
28
29
    public static function processMigrationFieldDirective(
30
        MigrationGenerator $generator,
31
        \GraphQL\Type\Definition\FieldDefinition $field,
32
        \GraphQL\Language\AST\DirectiveNode $directive,
33
        MigrationCodeFragment $code
34
    ): void {
35
        $code->appendExtraLine('$table->index("' . $field->name . '");');
36
    }
37
38
    public static function processMigrationRelationshipDirective(
39
        MigrationGenerator $generator,
40
        \GraphQL\Type\Definition\FieldDefinition $field,
41
        \GraphQL\Language\AST\DirectiveNode $directive,
42
        MigrationCodeFragment $code
43
    ): void {
44
        $code->appendExtraLine('$table->index("' . $field->name . '");');
45
    }
46
}
47