Passed
Push — master ( cab6b5...845ff4 )
by Bruno
04:13 queued 01:09
created

processModelRelationshipDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 1
rs 9.7
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
use Modelarium\Parser;
12
13
class HasManyDirective implements ModelDirectiveInterface, SeedDirectiveInterface
14
{
15
    public static function processModelTypeDirective(
16
        ModelGenerator $generator,
17
        \GraphQL\Language\AST\DirectiveNode $directive
18
    ): void {
19
        // nothing
20
    }
21
22 1
    public static function processModelFieldDirective(
23
        ModelGenerator $generator,
24
        \GraphQL\Type\Definition\FieldDefinition $field,
25
       \Formularium\Field $fieldFormularium,
26
        \GraphQL\Language\AST\DirectiveNode $directive
27
    ): void {
28
        // nothing
29 1
    }
30
31 1
    public static function processModelRelationshipDirective(
32
        ModelGenerator $generator,
33
        \GraphQL\Type\Definition\FieldDefinition $field,
34
        \GraphQL\Language\AST\DirectiveNode $directive
35
    ): string {
36 1
        list($type, $isRequired) = Parser::getUnwrappedType($field->type);
37
38 1
        $sourceTypeName = $generator->getLowerName();
39 1
        $targetTypeName = $type->name;
40 1
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
41 1
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
42 1
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
43
44 1
        $relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY;
45 1
        $isInverse = false;
46 1
        $generator->class->addMethod(ModelGenerator::toTableName($targetTypeName))
47 1
            ->setPublic()
48 1
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\HasMany')
49 1
            ->setBody("return \$this->hasMany($targetTypeName::class);");
50 1
        return $generator->getRelationshipDatatypeName(
51 1
            $relationship,
52
            $isInverse,
53
            $sourceTypeName,
54
            $targetTypeName
55
        );
56
    }
57
58
    public static function processSeedTypeDirective(
59
        SeedGenerator $generator,
60
        \GraphQL\Language\AST\DirectiveNode $directive
61
    ): void {
62
        // empty
63
    }
64
65 1
    public static function processSeedFieldDirective(
66
        SeedGenerator $generator,
67
        \GraphQL\Type\Definition\FieldDefinition $field,
68
        \GraphQL\Language\AST\DirectiveNode $directive
69
    ): void {
70 1
        $type1 = $generator->getLowerName();
71 1
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
72
73 1
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
74
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
75
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
76
        }
77 1
    }
78
79
    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
            \${$targetModel}Items = App\\Models\\$targetModel::all();
85
            \$model->{$relationship}()->attach(
86
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
87
            );
88
        }
89
        catch (\InvalidArgumentException \$e) {
90
            \$model->{$relationship}()->attach(
91
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
92
            );
93
        }
94
EOF;
95
    }
96
}
97