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

HasOneDirective::processModelFieldDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 HasOneDirective 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 3
    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 3
    }
30
31 3
    public static function processModelRelationshipDirective(
32
        ModelGenerator $generator,
33
        \GraphQL\Type\Definition\FieldDefinition $field,
34
        \GraphQL\Language\AST\DirectiveNode $directive
35
    ): string {
36 3
        list($type, $isRequired) = Parser::getUnwrappedType($field->type);
37
38 3
        $sourceTypeName = $generator->getLowerName();
39 3
        $targetTypeName = $type->name;
40 3
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
41 3
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
42 3
        $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 3
        $relationship = RelationshipFactory::RELATIONSHIP_ONE_TO_ONE;
45 3
        $isInverse = false;
46 3
        $generator->class->addMethod(Str::snake(Str::studly($targetTypeName)))
47 3
            ->setPublic()
48 3
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\HasOne')
49 3
            ->setBody("return \$this->hasOne($targetTypeName::class);");
50
51 3
        return $generator->getRelationshipDatatypeName(
52 3
            $relationship,
53
            $isInverse,
54
            $sourceTypeName,
55
            $targetTypeName
56
        );
57
    }
58
59
    public static function processSeedTypeDirective(
60
        SeedGenerator $generator,
61
        \GraphQL\Language\AST\DirectiveNode $directive
62
    ): void {
63
        // empty
64
    }
65
66 3
    public static function processSeedFieldDirective(
67
        SeedGenerator $generator,
68
        \GraphQL\Type\Definition\FieldDefinition $field,
69
        \GraphQL\Language\AST\DirectiveNode $directive
70
    ): void {
71 3
        $type1 = $generator->getLowerName();
72 3
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
73
74 3
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
75
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
76
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
77
        }
78 3
    }
79
80
    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

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