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

SeedGenerator::processDirectives()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.9666
cc 3
nc 3
nop 2
crap 3
1
<?php declare(strict_types=1);
2
3
namespace Modelarium\Laravel\Targets;
4
5
use GraphQL\Type\Definition\ObjectType;
6
use Modelarium\BaseGenerator;
7
use Modelarium\Exception\Exception;
8
use Modelarium\GeneratedCollection;
9
use Modelarium\GeneratedItem;
10
use Modelarium\Parser;
11
12
class SeedGenerator extends BaseGenerator
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $stubDir = __DIR__ . "/stubs/";
18
19
    /**
20
     * Extra seed code generated by parser
21
     *
22
     * @var string[]
23
     */
24
    public $extraCode = [];
25
26 8
    public function generate(): GeneratedCollection
27
    {
28 8
        if (!($this->type instanceof ObjectType)) {
29
            throw new Exception('Invalid type on seed generator:' . get_class($this->type));
30
        }
31
        /**
32
         * @var ObjectType $t
33
         */
34 8
        $t = $this->type;
35 8
        foreach ($t->getFields() as $field) {
36 8
            $directives = $field->astNode->directives;
37 8
            $this->processDirectives($field, $directives);
38
        }
39
40 8
        return new GeneratedCollection(
41 8
            [ new GeneratedItem(
42 8
                GeneratedItem::TYPE_SEED,
43 8
                $this->generateString(),
44 8
                $this->getGenerateFilename()
45
            )]
46
        );
47
    }
48
49 8
    public function processDirectives(
50
        \GraphQL\Type\Definition\FieldDefinition $field,
51
        \GraphQL\Language\AST\NodeList $directives
52
    ): void {
53 8
        foreach ($directives as $directive) {
54 8
            $name = $directive->name->value;
55 8
            $className = $this->getDirectiveClass($name);
56 8
            if ($className) {
57 2
                call_user_func(
58 2
                    [$className, 'processSeedFieldDirective'],
59
                    $this,
60
                    $field,
61
                    $directive
62
                );
63
            }
64
        }
65 8
    }
66
67 9
    public function generateString(): string
68
    {
69 9
        return $this->templateStub(
70 9
            'seed',
71
            [
72 9
                'extraCode' => join("\n", $this->extraCode)
73
            ]
74
        );
75
    }
76
77 8
    public function getGenerateFilename(): string
78
    {
79 8
        return $this->getBasePath('database/seeds/'. $this->studlyName . 'Seeder.php');
80
    }
81
}
82