Passed
Push — master ( 09030e...cab6b5 )
by Bruno
08:31
created

BaseGenerator::processFieldDirectives()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 15
ccs 0
cts 0
cp 0
crap 12
rs 9.9666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Modelarium;
4
5
use GraphQL\Type\Definition\Type;
6
use Modelarium\Exception\Exception;
7
use Modelarium\Exception\SkipGenerationException;
8
use Modelarium\Parser;
9
10
use function Safe\class_implements;
11
use function Safe\date;
12
13
abstract class BaseGenerator implements GeneratorInterface
14
{
15
    use GeneratorNameTrait;
16
17
    /**
18
     * @var string
19
     */
20
    protected $stubDir = null;
21
22
    /**
23
     * @var Parser
24
     */
25
    public $parser = null;
26
27
    /**
28
     * @var Type
29
     */
30
    protected $type = null;
31
32
    /**
33
     * @param Parser $parser
34
     * @param string $name The target type name.
35
     * @param Type|string $type
36 29
     */
37
    public function __construct(Parser $parser, string $name, $type = null)
38 29
    {
39 29
        $this->parser = $parser;
40
        $this->setBaseName($name);
41 29
42 10
        if ($type instanceof Type) {
43 19
            $this->type = $type;
44 19
        } elseif (!$type) {
45
            $this->type = $parser->getSchema()->getType($name);
46
        } else {
47
            throw new Exception('Invalid model');
48 29
        }
49
    }
50 10
51
    protected function phpHeader(): string
52 10
    {
53
        $date = date('c');
54 10
        return <<<EOF
55
<?php declare(strict_types=1);
56 10
/** 
57
 * This file was automatically generated by Modelarium on $date
58
 */
59
60
EOF;
61
    }
62
63
    /**
64
     * Gets the classname for a directive implementation interface class.
65
     *
66
     * @param string $directive The directive name.
67
     * @param string $type The type, such as 'Seeder' or 'Model'.
68
     * @return string|null
69 20
     */
70
    public function getDirectiveClass(
71
        string $directive,
72
        string $type = ''
73 20
    ): ?string {
74 20
        foreach (Modelarium::getGeneratorDirectiveNamespaces() as $ns) {
75 20
            $className = $ns . '\\' . ucfirst($directive) . 'Directive';
76 20
            if (!$type) {
77 20
                $parts = explode("\\", get_called_class());
78 20
                $type = end($parts);
79
                $type = str_replace('Generator', '', $type);
80 20
            }
81 20
            if (class_exists($className)
82
                && array_key_exists('Modelarium\\Laravel\\Targets\\Interfaces\\' . $type . 'DirectiveInterface', class_implements($className))
83 19
            ) {
84
                return $className;
85
            }
86 8
        }
87
        return null;
88
    }
89
90
    /**
91
     * Process all directives from list with directive classes.
92
     *
93
     * @param \GraphQL\Language\AST\NodeList $directives The directive list
94
     * @param string $generatorType The generatorType, like 'Seed' or 'Model'
95
     * @return void
96
     * @throws SkipGenerationException
97
     */
98
    protected function processTypeDirectives(
99
        \GraphQL\Language\AST\NodeList $directives,
100
        string $generatorType
101
    ): void {
102
        foreach ($directives as $directive) {
103
            $name = $directive->name->value;
104
    
105
            $className = $this->getDirectiveClass($name, $generatorType);
106
            if ($className) {
107
                $methodName = "$className::process{$generatorType}TypeDirective";
108
                /** @phpstan-ignore-next-line */
109
                $methodName(
110
                    $this,
111
                    $directive
112
                );
113
            }
114
        }
115
    }
116
117
    /**
118
     * Process all directives from list with directive classes.
119
     *
120
     * @param \GraphQL\Type\Definition\FieldDefinition $field
121
     * @param \GraphQL\Language\AST\NodeList $directives The directive list
122
     * @param string $generatorType The generatorType, like 'Seed' or 'Model'
123
     * @return void
124
     * @throws SkipGenerationException
125
     */
126
    public function processFieldDirectives(
127
        \GraphQL\Type\Definition\FieldDefinition $field,
128
        \GraphQL\Language\AST\NodeList $directives,
129
        string $generatorType
130
    ): void {
131
        foreach ($directives as $directive) {
132
            $name = $directive->name->value;
133
            $className = $this->getDirectiveClass($name);
134
            if ($className) {
135
                $methodName = "$className::process{$generatorType}FieldDirective";
136
                /** @phpstan-ignore-next-line */
137
                $methodName(
138
                    $this,
139
                    $field,
140
                    $directive
141
                );
142
            }
143
        }
144
    }
145
}
146