Passed
Push — master ( df5659...59487d )
by Bruno
04:26 queued 01:12
created

MorphToDirective::processModelFieldDirective()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 12
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 27
ccs 11
cts 13
cp 0.8462
crap 4.0582
rs 9.8666
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Faker\Provider\File;
6
use Formularium\ExtradataParameter;
7
use Formularium\Field;
8
use GraphQL\Type\Definition\ObjectType;
9
use GraphQL\Type\Definition\UnionType;
10
use Illuminate\Support\Str;
11
use Modelarium\Exception\Exception;
12
use Modelarium\Parser;
13
use Modelarium\Datatypes\RelationshipFactory;
14
use Modelarium\Laravel\Targets\ModelGenerator;
15
use Modelarium\Laravel\Targets\SeedGenerator;
16
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
17
use Modelarium\Laravel\Targets\Interfaces\SeedDirectiveInterface;
18
19
class MorphToDirective implements ModelDirectiveInterface, SeedDirectiveInterface
20
{
21
    public static function processModelTypeDirective(
22
        ModelGenerator $generator,
23
        \GraphQL\Language\AST\DirectiveNode $directive
24
    ): void {
25
        // nothing
26
    }
27
28 2
    public static function processModelFieldDirective(
29
        ModelGenerator $generator,
30
        \GraphQL\Type\Definition\FieldDefinition $field,
31
        \Formularium\Field $fieldFormularium,
32
        \GraphQL\Language\AST\DirectiveNode $directive
33
    ): void {
34 2
        list($type, $isRequired) = Parser::getUnwrappedType($field->type);
35 2
        $typeName = $type->name;
36
37 2
        if (!($type instanceof UnionType)) {
38
            throw new Exception("$typeName is declared as @morphTo target but it is not a union type.");
39
        }
40 2
        $unionTypes = $type->getTypes();
41 2
        $morphableTargets = [];
42 2
        foreach ($unionTypes as $t) {
43 2
            if (!($t instanceof ObjectType)) {
44
                throw new Exception("$typeName is declared in a @morphTo union but it's not an object type.");
45
            }
46
47
            /**
48
             * @var ObjectType $t
49
             */
50 2
            $morphableTargets[] = $t->name;
51
        }
52
53 2
        $fieldFormularium->getExtradata('morphTo')
54 2
            ->appendParameter(new ExtradataParameter('targetModels', implode(',', $morphableTargets)));
55 2
    }
56
57 2
    public static function processModelRelationshipDirective(
58
        ModelGenerator $generator,
59
        \GraphQL\Type\Definition\FieldDefinition $field,
60
        \GraphQL\Language\AST\DirectiveNode $directive
61
    ): string {
62 2
        $lowerName = mb_strtolower($generator->getInflector()->singularize($field->name));
63
64 2
        $sourceTypeName = $generator->getLowerName();
65 2
        $targetTypeName = $lowerName;
66 2
        $relationship = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $relationship is dead and can be removed.
Loading history...
67 2
        $isInverse = false;
0 ignored issues
show
Unused Code introduced by
The assignment to $isInverse is dead and can be removed.
Loading history...
68 2
        $generateRandom = true; // TODO
0 ignored issues
show
Unused Code introduced by
The assignment to $generateRandom is dead and can be removed.
Loading history...
69
70 2
        $relationship = RelationshipFactory::MORPH_ONE_TO_MANY; // TODO
71 2
        $isInverse = true;
72 2
        $generator->class->addMethod($field->name)
73 2
            ->setReturnType('\\Illuminate\\Database\\Eloquent\\Relations\\MorphTo')
74 2
            ->setPublic()
75 2
            ->setBody("return \$this->morphTo();");
76
77 2
        return $generator->getRelationshipDatatypeName(
78 2
            $relationship,
79
            $isInverse,
80
            $sourceTypeName,
81
            $targetTypeName
82
        );
83
    }
84
85 2
    public static function processSeedFieldDirective(
86
        SeedGenerator $generator,
87
        \GraphQL\Type\Definition\FieldDefinition $field,
88
        \GraphQL\Language\AST\DirectiveNode $directive
89
    ): void {
90 2
        $type1 = $generator->getLowerName();
91 2
        $type2 = mb_strtolower($generator->getInflector()->singularize($field->name));
92
93 2
        if (strcasecmp($type1, $type2) < 0) { // TODO: check this, might not work
94 2
            $relationship = mb_strtolower($generator->getInflector()->pluralize($field->name));
95 2
            $generator->extraCode[] = self::makeManyToManySeed($type1, $type2, $relationship);
96
        }
97 2
    }
98
99 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

99
    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...
100
    {
101
        return <<<EOF
102
103
        try {
104 2
            \${$targetModel}Items = App\\Models\\$targetModel::all();
105 2
            \$model->{$relationship}()->attach(
106 2
                \${$targetModel}Items->random(rand(1, 3))->pluck('id')->toArray()
107
            );
108
        }
109
        catch (\InvalidArgumentException \$e) {
110 2
            \$model->{$relationship}()->attach(
111 2
                \${$targetModel}Items->random(1)->pluck('id')->toArray()
112
            );
113
        }
114
EOF;
115
    }
116
}
117