Passed
Push — master ( c0a95b...c5e7d6 )
by Bruno
04:51
created

processModelRelationshipDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 1

Importance

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

79
    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...
80
    {
81
        return <<<EOF
82
83
        try {
84 2
            \${$targetModel}Items = App\\Models\\$targetModel::all();
85 2
            \$model->{$relationship}()->attach(
86 2
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
87
            );
88
        }
89
        catch (\InvalidArgumentException \$e) {
90 2
            \$model->{$relationship}()->attach(
91 2
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
92
            );
93
        }
94
EOF;
95
    }
96
}
97