Passed
Push — master ( 253428...340885 )
by Bruno
03:15
created

BaseGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 0
Metric Value
wmc 8
eloc 28
c 0
b 0
f 0
dl 0
loc 74
ccs 23
cts 24
cp 0.9583
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A phpHeader() 0 7 1
A getDirectiveClass() 0 17 4
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\Parser;
8
9
use function Safe\class_implements;
10
use function Safe\date;
11
12
abstract class BaseGenerator implements GeneratorInterface
13
{
14
    use GeneratorNameTrait;
15
16
    /**
17
     * @var string
18
     */
19
    protected $stubDir = null;
20
21
    /**
22
     * @var Parser
23
     */
24
    protected $parser = null;
25
26
    /**
27
     * @var Type
28
     */
29
    protected $type = null;
30
31
    /**
32
     * @param Parser $parser
33
     * @param string $name The target type name.
34
     * @param Type|string $type
35
     */
36 28
    public function __construct(Parser $parser, string $name, $type = null)
37
    {
38 28
        $this->parser = $parser;
39 28
        $this->setBaseName($name);
40
41 28
        if ($type instanceof Type) {
42 10
            $this->type = $type;
43 18
        } elseif (!$type) {
44 18
            $this->type = $parser->getSchema()->getType($name);
45
        } else {
46
            throw new Exception('Invalid model');
47
        }
48 28
    }
49
50 10
    protected function phpHeader(): string
51
    {
52 10
        $date = date('c');
53
        return <<<EOF
54 10
<?php declare(strict_types=1);
55
/** 
56 10
 * This file was automatically generated by Modelarium on $date
57
 */
58
59
EOF;
60
    }
61
62
    /**
63
     * Gets the classname for a directive implementation interface class.
64
     *
65
     * @param string $directive The directive name.
66
     * @param string $type The type, such as 'Seeder' or 'Model'.
67
     * @return string|null
68
     */
69 9
    public function getDirectiveClass(
70
        string $directive,
71
        string $type = ''
72
    ): ?string {
73 9
        $ns = 'Modelarium\\Laravel\\Directives';
74 9
        $className = $ns . '\\' . ucfirst($directive) . 'Directive';
75 9
        if (!$type) {
76 9
            $parts = explode("\\", get_called_class());
77 9
            $type = end($parts);
78 9
            $type = str_replace('Generator', '', $type);
79
        }
80 9
        if (class_exists($className)
81 9
            && array_key_exists('Modelarium\\Laravel\\Targets\\Interfaces\\' . $type . 'DirectiveInterface', class_implements($className))
82
        ) {
83 3
            return $className;
84
        }
85 7
        return null;
86
    }
87
}
88