Passed
Push — master ( 1d70e3...37a26d )
by Bruno
03:13
created

processMigrationFieldDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 7
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Illuminate\Support\Str;
6
use Modelarium\Exception\DirectiveException;
7
use Modelarium\Exception\Exception;
8
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
9
use Modelarium\Laravel\Targets\ModelGenerator;
10
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
11
use Modelarium\Laravel\Targets\MigrationCodeFragment;
12
use Modelarium\Laravel\Targets\MigrationGenerator;
13
use Modelarium\Parser;
14
15
class MigrationUniqueIndexDirective implements ModelDirectiveInterface, MigrationDirectiveInterface
16
{
17
    public static function processModelTypeDirective(
18
        ModelGenerator $generator,
19
        \GraphQL\Language\AST\DirectiveNode $directive
20
    ): void {
21
    }
22
23
    public static function processModelFieldDirective(
24
        ModelGenerator $generator,
25
        \GraphQL\Type\Definition\FieldDefinition $field,
26
        \Formularium\Field $fieldFormularium,
27
        \GraphQL\Language\AST\DirectiveNode $directive
28
    ): void {
29
        $fieldName = $field->name;
30
        $studlyName = $generator->getStudlyName();
31
        $generator->class->addMethod('from' . Str::studly($fieldName))
32
            ->setPublic()
33
            ->setStatic()
34
            ->setReturnType('\\App\\Models\\' . $studlyName)
35
            ->setReturnNullable()
36
            ->addComment("Factory from the $fieldName unique index")
37
            ->setBody("return {$studlyName}::firstWhere('$fieldName', \$value);")
38
            ->addParameter('value');
39
    }
40
41
    public static function processModelRelationshipDirective(
42
        ModelGenerator $generator,
43
        \GraphQL\Type\Definition\FieldDefinition $field,
44
        \GraphQL\Language\AST\DirectiveNode $directive,
45
        \Formularium\Datatype $datatype = null
46
    ): ?\Formularium\Datatype {
47
        return null;
48
    }
49
50
    public static function processMigrationTypeDirective(
51
        MigrationGenerator $generator,
52
        \GraphQL\Language\AST\DirectiveNode $directive
53
    ): void {
54
        $indexFields = Parser::getDirectiveArgumentByName($directive, 'fields');
55
        
56
        if (!count($indexFields)) {
57
            throw new Exception("You must provide at least one field to an index on a model");
58
        }
59
        $generator->createCode[] = '$table->unique(["' . 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

59
        $generator->createCode[] = '$table->unique(["' . implode('", "', /** @scrutinizer ignore-type */ $indexFields) .'"]);';
Loading history...
60
    }
61
62 1
    public static function processMigrationFieldDirective(
63
        MigrationGenerator $generator,
64
        \GraphQL\Type\Definition\FieldDefinition $field,
65
        \GraphQL\Language\AST\DirectiveNode $directive,
66
        MigrationCodeFragment $code
67
    ): void {
68 1
        $code->appendExtraLine('$table->unique("' . $field->name . '");');
69 1
    }
70
71
    public static function processMigrationRelationshipDirective(
72
        MigrationGenerator $generator,
73
        \GraphQL\Type\Definition\FieldDefinition $field,
74
        \GraphQL\Language\AST\DirectiveNode $directive,
75
        MigrationCodeFragment $code
76
    ): void {
77
        $code->appendExtraLine('$table->unique("' . $field->name . '");');
78
    }
79
}
80