Passed
Push — master ( 1b0c60...f71ccb )
by Bruno
03:25
created

processModelRelationshipDirective()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 17
c 0
b 0
f 0
dl 0
loc 26
ccs 18
cts 18
cp 1
rs 9.7
cc 2
nc 2
nop 3
crap 2
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\Laravel\Targets\ModelGenerator;
8
use Modelarium\Laravel\Targets\SeedGenerator;
9
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
10
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface;
11
12
class BelongsToManyDirective implements ModelDirectiveInterface, SeedDirectiveInterface
13
{
14
    public static function processModelTypeDirective(
15
        ModelGenerator $generator,
16
        \GraphQL\Language\AST\DirectiveNode $directive
17
    ): void {
18
        // nothing
19
    }
20
21 1
    public static function processModelFieldDirective(
22
        ModelGenerator $generator,
23
        \GraphQL\Type\Definition\FieldDefinition $field,
24
        \GraphQL\Language\AST\DirectiveNode $directive
25
    ): void {
26
        // nothing
27 1
    }
28
29 1
    public static function processModelRelationshipDirective(
30
        ModelGenerator $generator,
31
        \GraphQL\Type\Definition\FieldDefinition $field,
32
        \GraphQL\Language\AST\DirectiveNode $directive
33
    ): string {
34 1
        $lowerName = mb_strtolower($generator->getInflector()->singularize($field->name));
35 1
        $lowerNamePlural = $generator->getInflector()->pluralize($lowerName);
36
37 1
        $sourceTypeName = $generator->getLowerName();
38 1
        $targetTypeName = $lowerName;
39 1
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
40 1
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
41
42 1
        $targetClass = '\\App\\Models\\' . Str::studly($generator->getInflector()->singularize($field->name));
43 1
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
44 1
        $relationship = RelationshipFactory::RELATIONSHIP_MANY_TO_MANY;
45 1
        $isInverse = true;
46 1
        $generator->class->addMethod($lowerNamePlural)
47 1
            ->setPublic()
48 1
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany')
49 1
            ->setBody("return \$this->belongsToMany($targetClass::class);");
50
        
51 1
        $relationshipDatatype = "relationship:" . ($isInverse ? "inverse:" : "") .
0 ignored issues
show
introduced by
The condition $isInverse is always true.
Loading history...
52 1
            "$relationship:$sourceTypeName:$targetTypeName";
53
        
54 1
        return $relationshipDatatype;
55
    }
56
57 1
    public static function processSeedFieldDirective(
58
        SeedGenerator $generator,
59
        \GraphQL\Type\Definition\FieldDefinition $field,
60
        \GraphQL\Language\AST\DirectiveNode $directive
61
    ): void {
62 1
        $type1 = $generator->getLowerName();
63 1
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
64
65 1
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
66 1
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
67 1
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
68
        }
69 1
    }
70
71 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

71
    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...
72
    {
73
        return <<<EOF
74
75
        try {
76 1
            \${$targetModel}Items = App\\Models\\$targetModel::all();
77 1
            \$model->{$relationship}()->attach(
78 1
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
79
            );
80
        }
81
        catch (\InvalidArgumentException \$e) {
82 1
            \$model->{$relationship}()->attach(
83 1
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
84
            );
85
        }
86
EOF;
87
    }
88
}
89