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

SeedGenerator::processDirectives()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 9
c 4
b 0
f 0
dl 0
loc 14
ccs 8
cts 8
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\FormulariumUtils;
9
use Modelarium\GeneratedCollection;
10
use Modelarium\GeneratedItem;
11
use Modelarium\Parser;
12
13
class SeedGenerator extends BaseGenerator
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $stubDir = __DIR__ . "/stubs/";
19
20
    /**
21
     * Extra seed code generated by parser
22
     *
23
     * @var string[]
24
     */
25
    public $extraCode = [];
26 8
27
    public function generate(): GeneratedCollection
28 8
    {
29
        if (!($this->type instanceof ObjectType)) {
30
            throw new Exception('Invalid type on seed generator:' . get_class($this->type));
31
        }
32
33
        /**
34 8
         * @var \GraphQL\Language\AST\NodeList|null
35 8
         */
36 8
        $directives = $this->type->astNode->directives;
37 8
        if ($directives) {
38
            $this->processTypeDirectives($directives, 'Seed');
0 ignored issues
show
Bug introduced by
$directives of type GraphQL\Language\AST\DirectiveNode[] is incompatible with the type GraphQL\Language\AST\NodeList expected by parameter $directives of Modelarium\BaseGenerator::processTypeDirectives(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

38
            $this->processTypeDirectives(/** @scrutinizer ignore-type */ $directives, 'Seed');
Loading history...
39
        }
40 8
41 8
        /**
42 8
         * @var ObjectType $t
43 8
         */
44 8
        $t = $this->type;
45
        foreach ($t->getFields() as $field) {
46
            $directives = $field->astNode->directives;
47
            $this->processFieldDirectives($field, $directives, 'Seed');
48
        }
49 8
50
        return new GeneratedCollection(
51
            [ new GeneratedItem(
52
                GeneratedItem::TYPE_SEED,
53 8
                $this->generateString(),
54 8
                $this->getGenerateFilename()
55 8
            )]
56 8
        );
57 8
    }
58
59 8
60 8
    public function generateString(): string
61
    {
62
        return $this->templateStub(
63
            'seed',
64
            [
65
                'extraCode' => join("\n", $this->extraCode)
66 8
            ]
67
        );
68 9
    }
69
70 9
    public function getGenerateFilename(): string
71 9
    {
72
        return $this->getBasePath('database/seeds/'. $this->studlyName . 'Seeder.php');
73 9
    }
74
}
75