Passed
Push — master ( 520bbe...0c2dd0 )
by Bruno
03:09
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)) {
0 ignored issues
show
Bug introduced by
It seems like $indexFields can also be of type null; however, parameter $value of count() does only seem to accept Countable|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

22
        if (!count(/** @scrutinizer ignore-type */ $indexFields)) {
Loading history...
23
            throw new Exception("You must provide at least one field to a full text index");
24
        }
25 1
        $generator->postCreateCode[] =
26
            "if (!App::environment('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("\$parts = array_filter(explode(' ', str_replace(',', '', \$needle)));\n" .
68
                "\$search = implode(',', \$parts);\n" .
69
                "return \$query->whereRaw(\"MATCH ($indexFieldsString) AGAINST (? IN NATURAL LANGUAGE MODE)\", [\$search]);"
70
            );
71
72
        $method->addParameter('query')
73
            ->setType('\\Illuminate\\Database\\Eloquent\\Builder');
74
        $method->addParameter('needle')
75
            ->setType('string');
76
    }
77
78
    public static function processModelFieldDirective(
79
        ModelGenerator $generator,
80
        \GraphQL\Type\Definition\FieldDefinition $field,
81
        \Formularium\Field $fieldFormularium,
82
        \GraphQL\Language\AST\DirectiveNode $directive
83
    ): void {
84
        // nothing
85
    }
86
87
    public static function processModelRelationshipDirective(
88
        ModelGenerator $generator,
89
        \GraphQL\Type\Definition\FieldDefinition $field,
90
        \GraphQL\Language\AST\DirectiveNode $directive,
91
        \Formularium\Datatype $datatype = null
92
    ): ?\Formularium\Datatype {
93
        return null;
94
    }
95
}
96