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 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
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 BelongsToDirective 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 4
    public static function processModelFieldDirective(
22
        ModelGenerator $generator,
23
        \GraphQL\Type\Definition\FieldDefinition $field,
24
        \GraphQL\Language\AST\DirectiveNode $directive
25
    ): void {
26
        // nothing
27 4
    }
28
29 4
    public static function processModelRelationshipDirective(
30
        ModelGenerator $generator,
31
        \GraphQL\Type\Definition\FieldDefinition $field,
32
        \GraphQL\Language\AST\DirectiveNode $directive
33
    ): string {
34 4
        $lowerName = mb_strtolower($generator->getInflector()->singularize($field->name));
35 4
        $lowerNamePlural = $generator->getInflector()->pluralize($lowerName);
0 ignored issues
show
Unused Code introduced by
The assignment to $lowerNamePlural is dead and can be removed.
Loading history...
36
37 4
        $sourceTypeName = $generator->getLowerName();
38 4
        $targetTypeName = $lowerName;
39 4
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
40 4
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
41
42 4
        $targetClass = '\\App\\Models\\' . Str::studly($generator->getInflector()->singularize($field->name));
43 4
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
44 4
        $relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY;
45 4
        $isInverse = true;
46 4
        $generator->class->addMethod($lowerName)
47 4
            ->setPublic()
48 4
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\BelongsTo')
49 4
            ->setBody("return \$this->belongsTo($targetClass::class);");
50
51 4
        $relationshipDatatype = "relationship:" . ($isInverse ? "inverse:" : "") .
0 ignored issues
show
introduced by
The condition $isInverse is always true.
Loading history...
52 4
            "$relationship:$sourceTypeName:$targetTypeName";
53
        
54 4
        return $relationshipDatatype;
55
    }
56
57 4
    public static function processSeedFieldDirective(
58
        SeedGenerator $generator,
59
        \GraphQL\Type\Definition\FieldDefinition $field,
60
        \GraphQL\Language\AST\DirectiveNode $directive
61
    ): void {
62 4
        $type1 = $generator->getLowerName();
63 4
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
64
65 4
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
66 4
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
67 4
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
68
        }
69 4
    }
70
71 4
    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 4
            \${$targetModel}Items = App\\Models\\$targetModel::all();
77 4
            \$model->{$relationship}()->attach(
78 4
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
79
            );
80
        }
81
        catch (\InvalidArgumentException \$e) {
82 4
            \$model->{$relationship}()->attach(
83 4
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
84
            );
85
        }
86
EOF;
87
    }
88
}
89