Passed
Push — master ( 2d2e7e...f06b9e )
by Bruno
03:45
created

processModelFieldDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 0
cts 1
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Modelarium\Exception\DirectiveException;
6
use Modelarium\Exception\Exception;
7
use Modelarium\Laravel\Targets\MigrationGenerator;
8
use Modelarium\Laravel\Targets\ModelGenerator;
9
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
10
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
11
use Modelarium\Laravel\Targets\MigrationCodeFragment;
12
use Modelarium\Parser;
13
14
class MigrationFulltextIndexDirective implements MigrationDirectiveInterface, ModelDirectiveInterface
15
{
16 1
    public static function processMigrationTypeDirective(
17
        MigrationGenerator $generator,
18
        \GraphQL\Language\AST\DirectiveNode $directive
19
    ): void {
20 1
        $indexFields = Parser::getDirectiveArgumentByName($directive, 'fields');
21
22 1
        if (!count($indexFields)) {
23
            throw new Exception("You must provide at least one field to a full text index");
24
        }
25 1
        $generator->postCreateCode[] =
26
            "if (env('APP_ENV') !== 'testing') {
27 1
            DB::statement('ALTER TABLE " . $generator->getTableName()  .
28 1
                " ADD FULLTEXT fulltext_index (`" . implode('`, `', $indexFields) . "`)');
0 ignored issues
show
Bug introduced by
It seems like $indexFields can also be of type null; however, parameter $pieces of implode() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
                " ADD FULLTEXT fulltext_index (`" . implode('`, `', /** @scrutinizer ignore-type */ $indexFields) . "`)');
Loading history...
29
            }";
30 1
    }
31
32
    public static function processMigrationFieldDirective(
33
        MigrationGenerator $generator,
34
        \GraphQL\Type\Definition\FieldDefinition $field,
35
        \GraphQL\Language\AST\DirectiveNode $directive,
36
        MigrationCodeFragment $code
37
    ): void {
38
        throw new DirectiveException("Directive not supported here");
39
    }
40
41
    public static function processMigrationRelationshipDirective(
42
        MigrationGenerator $generator,
43
        \GraphQL\Type\Definition\FieldDefinition $field,
44
        \GraphQL\Language\AST\DirectiveNode $directive,
45
        MigrationCodeFragment $code
46
    ): void {
47
        throw new DirectiveException("Directive not supported here");
48
    }
49
50
    public static function processModelTypeDirective(
51
        ModelGenerator $generator,
52
        \GraphQL\Language\AST\DirectiveNode $directive
53
    ): void {
54
        $indexFields = Parser::getDirectiveArgumentByName($directive, 'fields');
55
        $indexFieldsString = join(', ', $indexFields);
56
        
57
        $method = $generator->class->addMethod('scopeFulltext')
58
            ->setPublic()
59
            ->setComment('
60
Scope a query to use the fulltext index
61
62
@param  \Illuminate\Database\Eloquent\Builder  $query
63
@param  string $needle
64
@return \Illuminate\Database\Eloquent\Builder
65
           ')
66
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Builder')
67
            ->setBody("return \$query->whereRaw(\"MATCH ($indexFieldsString) AGAINST (? IN NATURAL LANGUAGE MODE)\", [\$needle]);");
68
        
69
        $method->addParameter('query')
70
            ->setType('\\Illuminate\\Database\\Eloquent\\Builder');
71
        $method->addParameter('needle')
72
            ->setType('string');
73
    }
74
75
    public static function processModelFieldDirective(
76
        ModelGenerator $generator,
77
        \GraphQL\Type\Definition\FieldDefinition $field,
78
        \Formularium\Field $fieldFormularium,
79
        \GraphQL\Language\AST\DirectiveNode $directive
80
    ): void {
81
        // nothing
82
    }
83
84
    public static function processModelRelationshipDirective(
85
        ModelGenerator $generator,
86
        \GraphQL\Type\Definition\FieldDefinition $field,
87
        \GraphQL\Language\AST\DirectiveNode $directive,
88
        \Formularium\Datatype $datatype = null
89
    ): ?\Formularium\Datatype {
90
        return null;
91
    }
92
}
93