Passed
Push — master ( 73a53e...f91dab )
by Bruno
07:25 queued 04:03
created

processModelRelationshipDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 27
ccs 17
cts 17
cp 1
rs 9.6333
cc 1
nc 1
nop 3
crap 1
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Illuminate\Support\Str;
6
use Modelarium\Datatypes\RelationshipFactory;
7
use Modelarium\Exception\DirectiveException;
8
use Modelarium\Laravel\Targets\Interfaces\MigrationDirectiveInterface;
9
use Modelarium\Laravel\Targets\ModelGenerator;
10
use Modelarium\Laravel\Targets\SeedGenerator;
11
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
12
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface;
13
use Modelarium\Laravel\Targets\MigrationCodeFragment;
14
use Modelarium\Laravel\Targets\MigrationGenerator;
15
16
class BelongsToManyDirective implements MigrationDirectiveInterface, ModelDirectiveInterface, SeedDirectiveInterface
17
{
18
    public static function processMigrationTypeDirective(
19
        MigrationGenerator $generator,
20
        \GraphQL\Language\AST\DirectiveNode $directive
21
    ): void {
22
        throw new DirectiveException("Directive not supported here");
23
    }
24
25
    public static function processMigrationFieldDirective(
26
        MigrationGenerator $generator,
27
        \GraphQL\Type\Definition\FieldDefinition $field,
28
        \GraphQL\Language\AST\DirectiveNode $directive,
29
        MigrationCodeFragment $code
30
    ): void {
31
        throw new DirectiveException("Directive not supported here");
32
    }
33
34 1
    public static function processMigrationRelationshipDirective(
35
        MigrationGenerator $generator,
36
        \GraphQL\Type\Definition\FieldDefinition $field,
37
        \GraphQL\Language\AST\DirectiveNode $directive,
38
        MigrationCodeFragment $code
39
    ): void {
40 1
        $lowerName = mb_strtolower($generator->getInflector()->singularize($field->name));
41
42 1
        $type1 = $generator->getLowerName();
43 1
        $type2 = $lowerName;
44
45
        // we only generate once, so use a comparison for that
46 1
        if (strcasecmp($type1, $type2) < 0) {
47 1
            $generator->generateManyToManyTable($type1, $type2);
48
        }
49 1
    }
50
51
    public static function processModelTypeDirective(
52
        ModelGenerator $generator,
53
        \GraphQL\Language\AST\DirectiveNode $directive
54
    ): void {
55
        // nothing
56
    }
57
58 1
    public static function processModelFieldDirective(
59
        ModelGenerator $generator,
60
        \GraphQL\Type\Definition\FieldDefinition $field,
61
        \Formularium\Field $fieldFormularium,
62
        \GraphQL\Language\AST\DirectiveNode $directive
63
    ): void {
64
        // nothing
65 1
    }
66
67 1
    public static function processModelRelationshipDirective(
68
        ModelGenerator $generator,
69
        \GraphQL\Type\Definition\FieldDefinition $field,
70
        \GraphQL\Language\AST\DirectiveNode $directive
71
    ): string {
72 1
        $lowerName = mb_strtolower($generator->getInflector()->singularize($field->name));
73 1
        $lowerNamePlural = $generator->getInflector()->pluralize($lowerName);
74
75 1
        $sourceTypeName = $generator->getLowerName();
76 1
        $targetTypeName = $lowerName;
77 1
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
78 1
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
79
80 1
        $targetClass = Str::studly($generator->getInflector()->singularize($field->name));
81 1
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
82 1
        $relationship = RelationshipFactory::RELATIONSHIP_MANY_TO_MANY;
83 1
        $isInverse = true;
84 1
        $generator->class->addMethod($lowerNamePlural)
85 1
            ->setPublic()
86 1
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany')
87 1
            ->setBody("return \$this->belongsToMany($targetClass::class);");
88
        
89 1
        return $generator->getRelationshipDatatypeName(
90 1
            $relationship,
91
            $isInverse,
92
            $sourceTypeName,
93
            $targetTypeName
94
        );
95
    }
96
97 1
    public static function processSeedFieldDirective(
98
        SeedGenerator $generator,
99
        \GraphQL\Type\Definition\FieldDefinition $field,
100
        \GraphQL\Language\AST\DirectiveNode $directive
101
    ): void {
102 1
        $type1 = $generator->getLowerName();
103 1
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
104
105 1
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
106 1
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
107 1
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
108
        }
109 1
    }
110
111 1
    protected static function makeManyToManySeed(string $sourceModel, string $targetModel, string $relationship): string
0 ignored issues
show
Unused Code introduced by
The parameter $sourceModel is not used and could be removed. ( Ignorable by Annotation )

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

111
    protected static function makeManyToManySeed(/** @scrutinizer ignore-unused */ string $sourceModel, string $targetModel, string $relationship): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
    {
113
        return <<<EOF
114
115
        try {
116 1
            \${$targetModel}Items = App\\Models\\$targetModel::all();
117 1
            \$model->{$relationship}()->attach(
118 1
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
119
            );
120
        }
121
        catch (\InvalidArgumentException \$e) {
122 1
            \$model->{$relationship}()->attach(
123 1
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
124
            );
125
        }
126
EOF;
127
    }
128
}
129