Passed
Push — master ( f71ccb...0ebae0 )
by Bruno
03:45
created

BelongsToManyDirective   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 34
c 1
b 0
f 0
dl 0
loc 74
ccs 32
cts 34
cp 0.9412
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processModelTypeDirective() 0 4 1
A processModelFieldDirective() 0 5 1
A processSeedFieldDirective() 0 11 2
A makeManyToManySeed() 0 13 1
A processModelRelationshipDirective() 0 27 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\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
        return $generator->getRelationshipDatatypeName(
52 1
            $relationship,
53
            $isInverse,
54
            $sourceTypeName,
55
            $targetTypeName
56
        );
57
    }
58
59 1
    public static function processSeedFieldDirective(
60
        SeedGenerator $generator,
61
        \GraphQL\Type\Definition\FieldDefinition $field,
62
        \GraphQL\Language\AST\DirectiveNode $directive
63
    ): void {
64 1
        $type1 = $generator->getLowerName();
65 1
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
66
67 1
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
68 1
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
69 1
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
70
        }
71 1
    }
72
73 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

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