Passed
Push — master ( efc350...73a53e )
by Bruno
08:12
created

MigrationUniqueIndexDirective   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 14.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 57
ccs 3
cts 21
cp 0.1429
rs 10
c 1
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A processMigrationRelationshipDirective() 0 7 1
A processModelTypeDirective() 0 4 1
A processMigrationTypeDirective() 0 5 1
A processModelRelationshipDirective() 0 6 1
A processModelFieldDirective() 0 16 1
A processMigrationFieldDirective() 0 7 1
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\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
8
use Modelarium\Laravel\Targets\ModelGenerator;
9
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
10
use Modelarium\Laravel\Targets\MigrationCodeFragment;
11
use Modelarium\Laravel\Targets\MigrationGenerator;
12
13
class MigrationUniqueIndexDirective implements ModelDirectiveInterface, MigrationDirectiveInterface
14
{
15
    public static function processModelTypeDirective(
16
        ModelGenerator $generator,
17
        \GraphQL\Language\AST\DirectiveNode $directive
18
    ): void {
19
    }
20
21
    public static function processModelFieldDirective(
22
        ModelGenerator $generator,
23
        \GraphQL\Type\Definition\FieldDefinition $field,
24
        \Formularium\Field $fieldFormularium,
25
        \GraphQL\Language\AST\DirectiveNode $directive
26
    ): void {
27
        $fieldName = $field->name;
28
        $studlyName = $generator->getStudlyName();
29
        $generator->class->addMethod('from' . Str::studly($fieldName))
30
            ->setPublic()
31
            ->setStatic()
32
            ->setReturnType('\\App\\Models\\' . $studlyName)
33
            ->setReturnNullable()
34
            ->addComment("Factory from the $fieldName unique index")
35
            ->setBody("return {$studlyName}::firstWhere('$fieldName', \$value);")
36
            ->addParameter('value');
37
    }
38
39
    public static function processModelRelationshipDirective(
40
        ModelGenerator $generator,
41
        \GraphQL\Type\Definition\FieldDefinition $field,
42
        \GraphQL\Language\AST\DirectiveNode $directive
43
    ): string {
44
        return '';
45
    }
46
47
    public static function processMigrationTypeDirective(
48
        MigrationGenerator $generator,
49
        \GraphQL\Language\AST\DirectiveNode $directive
50
    ): void {
51
        throw new DirectiveException("Invalid directive on type");
52 1
    }
53
54
    public static function processMigrationFieldDirective(
55
        MigrationGenerator $generator,
56
        \GraphQL\Type\Definition\FieldDefinition $field,
57
        \GraphQL\Language\AST\DirectiveNode $directive,
58 1
        MigrationCodeFragment $code
59 1
    ): void {
60
        $code->appendExtraLine('$table->unique("' . $field->name . '");');
61
    }
62
63
    public static function processMigrationRelationshipDirective(
64
        MigrationGenerator $generator,
65
        \GraphQL\Type\Definition\FieldDefinition $field,
66
        \GraphQL\Language\AST\DirectiveNode $directive,
67
        MigrationCodeFragment $code
68
    ): void {
69
        $code->appendExtraLine('$table->unique("' . $field->name . '");');
70
    }
71
}
72