FactoryGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 51
ccs 20
cts 22
cp 0.9091
rs 10
c 1
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A generateString() 0 7 3
A generate() 0 28 4
A getGenerateFilename() 0 3 1
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