Passed
Push — master ( 7a0761...b9ca99 )
by Bruno
03:45
created

HasManyDirective   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 65.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
dl 0
loc 81
ccs 23
cts 35
cp 0.6571
rs 10
c 1
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
A processModelTypeDirective() 0 4 1
A processModelFieldDirective() 0 6 1
A processSeedFieldDirective() 0 11 2
A processSeedTypeDirective() 0 4 1
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 Formularium\Factory\DatatypeFactory;
6
use Illuminate\Support\Str;
7
use Modelarium\Datatypes\RelationshipFactory;
8
use Modelarium\Laravel\Targets\ModelGenerator;
9
use Modelarium\Laravel\Targets\SeedGenerator;
10
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
11
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface;
12
use Modelarium\Parser;
13
14
class HasManyDirective implements ModelDirectiveInterface, SeedDirectiveInterface
15
{
16
    public static function processModelTypeDirective(
17
        ModelGenerator $generator,
18
        \GraphQL\Language\AST\DirectiveNode $directive
19
    ): void {
20
        // nothing
21
    }
22
23 1
    public static function processModelFieldDirective(
24
        ModelGenerator $generator,
25
        \GraphQL\Type\Definition\FieldDefinition $field,
26
        \Formularium\Field $fieldFormularium,
27
        \GraphQL\Language\AST\DirectiveNode $directive
28
    ): void {
29
        // nothing
30 1
    }
31
32 1
    public static function processModelRelationshipDirective(
33
        ModelGenerator $generator,
34
        \GraphQL\Type\Definition\FieldDefinition $field,
35
        \GraphQL\Language\AST\DirectiveNode $directive,
36
        \Formularium\Datatype $datatype = null
37
    ): ?\Formularium\Datatype {
38 1
        list($type, $isRequired) = Parser::getUnwrappedType($field->type);
39
40 1
        $sourceTypeName = $generator->getLowerName();
41 1
        $targetTypeName = $type->name;
42 1
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
43 1
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
44 1
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
45
46 1
        $relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_MANY;
47 1
        $isInverse = false;
48 1
        $generator->class->addMethod(ModelGenerator::toTableName($targetTypeName))
49 1
            ->setPublic()
50 1
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\HasMany')
51 1
            ->setBody("return \$this->hasMany($targetTypeName::class);");
52 1
        $datatypeName = $generator->getRelationshipDatatypeName(
53 1
            $relationship,
54
            $isInverse,
55
            $sourceTypeName,
56
            $targetTypeName
57
        );
58 1
        return DatatypeFactory::factory($datatypeName);
59
    }
60
61
    public static function processSeedTypeDirective(
62
        SeedGenerator $generator,
63
        \GraphQL\Language\AST\DirectiveNode $directive
64
    ): void {
65
        // empty
66
    }
67
68 1
    public static function processSeedFieldDirective(
69
        SeedGenerator $generator,
70
        \GraphQL\Type\Definition\FieldDefinition $field,
71
        \GraphQL\Language\AST\DirectiveNode $directive
72
    ): void {
73 1
        $type1 = $generator->getLowerName();
74 1
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
75
76 1
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
77
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
78
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
79
        }
80 1
    }
81
82
    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

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