Passed
Push — master ( f71ccb...0ebae0 )
by Bruno
03:45
created

MigrationFulltextIndexDirective   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 57.14%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 54
ccs 12
cts 21
cp 0.5714
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processMigrationFieldDirective() 0 5 1
A processModelFieldDirective() 0 5 1
A processMigrationTypeDirective() 0 20 3
A processModelTypeDirective() 0 5 1
A processModelRelationshipDirective() 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\ModelGenerator;
8
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
9
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
10
11
class MigrationFulltextIndexDirective implements MigrationDirectiveInterface, ModelDirectiveInterface
12
{
13 1
    public static function processMigrationTypeDirective(
14
        MigrationGenerator $generator,
15
        \GraphQL\Language\AST\DirectiveNode $directive
16
    ): void {
17
        /** @phpstan-ignore-next-line */
18 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...
19
20 1
        $indexFields = [];
21 1
        foreach ($values as $value) {
22 1
            $indexFields[] = $value->value;
23
        }
24
25 1
        if (!count($indexFields)) {
26
            throw new Exception("You must provide at least one field to a full text index");
27
        }
28 1
        $generator->postCreateCode[] = "DB::statement('ALTER TABLE " .
29 1
            $generator->getLowerNamePlural() .
30 1
            " ADD FULLTEXT fulltext_index (\"" .
31 1
            implode('", "', $indexFields) .
32 1
            "\")');";
33 1
    }
34
35
    public static function processMigrationFieldDirective(
36
        MigrationGenerator $generator,
37
        \GraphQL\Type\Definition\FieldDefinition $field,
38
        \GraphQL\Language\AST\DirectiveNode $directive
39
    ): void {
40
        // nothing
41
    }
42
43
    public static function processModelTypeDirective(
44
        ModelGenerator $generator,
45
        \GraphQL\Language\AST\DirectiveNode $directive
46
    ): void {
47
        throw new Exception("Primary index is not implemented yet");
48
    }
49
50
    public static function processModelFieldDirective(
51
        ModelGenerator $generator,
52
        \GraphQL\Type\Definition\FieldDefinition $field,
53
        \GraphQL\Language\AST\DirectiveNode $directive
54
    ): void {
55
        // nothing
56
    }
57
58
    public static function processModelRelationshipDirective(
59
        ModelGenerator $generator,
60
        \GraphQL\Type\Definition\FieldDefinition $field,
61
        \GraphQL\Language\AST\DirectiveNode $directive
62
    ): string {
63
        // nothing
64
        return '';
65
    }
66
}
67