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

EagerLoadDirective::processModelFieldDirective()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 0
cts 1
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Directives;
4
5
use Modelarium\Datatypes\Datatype_relationship;
6
use Modelarium\Datatypes\RelationshipFactory;
7
use Modelarium\Exception\DirectiveException;
8
use Modelarium\Laravel\Targets\ModelGenerator;
9
use Modelarium\Laravel\Targets\Interfaces\ModelDirectiveInterface;
10
use Modelarium\Parser;
11
12
class EagerLoadDirective implements ModelDirectiveInterface
13
{
14
    public static function processModelTypeDirective(
15
        ModelGenerator $generator,
16
        \GraphQL\Language\AST\DirectiveNode $directive
17
    ): void {
18
        $target = Parser::getDirectiveArgumentByName($directive, 'tables', []);
19
        var_dump($target);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($target) looks like debug code. Are you sure you do not want to remove it?
Loading history...
20
21
        foreach ($target as $t) {
22
            $generator->with[] = $t;
23
        }
24
    }
25
26
    public static function processModelFieldDirective(
27
        ModelGenerator $generator,
28
        \GraphQL\Type\Definition\FieldDefinition $field,
29
        \Formularium\Field $fieldFormularium,
30
        \GraphQL\Language\AST\DirectiveNode $directive
31
    ): void {
32
    }
33
34
    /**
35
     * Undocumented function
36
     *
37
     * @param ModelGenerator $generator
38
     * @param \GraphQL\Type\Definition\FieldDefinition $field
39
     * @param \GraphQL\Language\AST\DirectiveNode $directive
40
     * @return?\Formularium\Datatype The relationship datatype name. If this directive does not
41
     * handle the datatype, just return an empty string.
42
     *
43
     */
44
    public static function processModelRelationshipDirective(
45
        ModelGenerator $generator,
46
        \GraphQL\Type\Definition\FieldDefinition $field,
47
        \GraphQL\Language\AST\DirectiveNode $directive,
48
        \Formularium\Datatype $datatype = null
49
    ): ?\Formularium\Datatype {
50
        $target = Parser::getDirectiveArgumentByName($directive, 'name');
51
52
        if (!$target) {
53
            if (!$datatype) {
54
                throw new DirectiveException("@eagerLoad must be placed after the relationship directive (e.g. @belongsTo, @hasMany etc)");
55
            }
56
            if (!($datatype instanceof Datatype_relationship)) {
57
                throw new DirectiveException("@eagerLoad got a datatype that is not a relationship");
58
            }
59
60
            $targetSingle = mb_strtolower($generator->getInflector()->singularize($datatype->getTarget()));
61
            $targetPlural = $datatype->getTargetTable();
62
            switch ($datatype->getRelationship()) {
63
                case RelationshipFactory::RELATIONSHIP_ONE_TO_ONE:
64
                case RelationshipFactory::MORPH_ONE_TO_ONE:
65
                    $target = $targetSingle;
66
                    break;
67
                case RelationshipFactory::RELATIONSHIP_ONE_TO_MANY:
68
                case RelationshipFactory::MORPH_ONE_TO_MANY:
69
                    $target = $datatype->getIsInverse() ? $targetSingle : $targetPlural;
70
                    break;
71
                case RelationshipFactory::RELATIONSHIP_MANY_TO_MANY:
72
                case RelationshipFactory::MORPH_MANY_TO_MANY:
73
                    $target = $targetPlural;
74
                    break;
75
            }
76
        }
77
        $generator->with[] = $target;
78
        
79
        return null;
80
    }
81
}
82