FactoryGenerator::generate()   A
last analyzed

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 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 0
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\GeneratedCollection;
8
use Modelarium\GeneratedItem;
9
use Illuminate\Support\Str;
10
use Modelarium\Exception\Exception;
11
use Modelarium\Laravel\Util as LaravelUtil;
12
13
class FactoryGenerator extends BaseGenerator
14
{
15
    /**
16
     * @var string
17
     */
18
    protected $stubDir = __DIR__ . "/stubs/";
19
20 8
    public function generate(): GeneratedCollection
21
    {
22 8
        if (!($this->type instanceof ObjectType)) {
23
            throw new Exception('Invalid type on seed generator:' . get_class($this->type));
24
        }
25
26
        /**
27
         * @var \GraphQL\Language\AST\NodeList<\GraphQL\Language\AST\DirectiveNode>|null
28
         */
29 8
        $directives = $this->type->astNode->directives;
30 8
        if ($directives) {
0 ignored issues
show
introduced by
$directives is of type GraphQL\Language\AST\NodeList, thus it always evaluated to true.
Loading history...
31 8
            $this->processTypeDirectives($directives, 'Factory');
32
        }
33
34
        /**
35
         * @var ObjectType $t
36
         */
37 8
        $t = $this->type;
38 8
        foreach ($t->getFields() as $field) {
39 8
            $directives = $field->astNode->directives;
40 8
            $this->processFieldDirectives($field, $directives, 'Factory');
41
        }
42
        
43 8
        return new GeneratedCollection(
44 8
            [ new GeneratedItem(
45 8
                GeneratedItem::TYPE_FACTORY,
46 8
                $this->generateString(),
47 8
                $this->getGenerateFilename()
48
            )]
49
        );
50
    }
51
52 9
    public function generateString(): string
53
    {
54 9
        $laravelVersion = LaravelUtil::getLaravelVersion();
55 9
        if (Str::startsWith($laravelVersion, '6.') || Str::startsWith($laravelVersion, '7.')) {
56 9
            return $this->templateStub('factory');
57
        }
58
        return $this->templateStub('factory8');
59
    }
60
61 8
    public function getGenerateFilename(): string
62
    {
63 8
        return $this->getBasePath('database/factories/'. $this->studlyName . 'Factory.php');
64
    }
65
}
66