Passed
Push — master ( cab6b5...845ff4 )
by Bruno
04:13 queued 01:09
created

SeedGenerator::generate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
nc 5
nop 0
dl 0
loc 28
ccs 14
cts 15
cp 0.9333
crap 4.0047
rs 9.7998
c 1
b 0
f 0
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
27 8
    public function generate(): GeneratedCollection
28
    {
29 8
        if (!($this->type instanceof ObjectType)) {
30
            throw new Exception('Invalid type on seed generator:' . get_class($this->type));
31
        }
32
33
        /**
34
         * @var \GraphQL\Language\AST\NodeList|null
35
         */
36 8
        $directives = $this->type->astNode->directives;
37 8
        if ($directives) {
38 8
            $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
41
        /**
42
         * @var ObjectType $t
43
         */
44 8
        $t = $this->type;
45 8
        foreach ($t->getFields() as $field) {
46 8
            $directives = $field->astNode->directives;
47 8
            $this->processFieldDirectives($field, $directives, 'Seed');
48
        }
49
50 8
        return new GeneratedCollection(
51 8
            [ new GeneratedItem(
52 8
                GeneratedItem::TYPE_SEED,
53 8
                $this->generateString(),
54 8
                $this->getGenerateFilename()
55
            )]
56
        );
57
    }
58
59
60 9
    public function generateString(): string
61
    {
62 9
        return $this->templateStub(
63 9
            'seed',
64
            [
65 9
                'extraCode' => join("\n", $this->extraCode)
66
            ]
67
        );
68
    }
69
70 8
    public function getGenerateFilename(): string
71
    {
72 8
        return $this->getBasePath('database/seeds/'. $this->studlyName . 'Seeder.php');
73
    }
74
}
75