Passed
Branch dev_2x (3e8772)
by Adrian
01:42
created

EntityBaseGenerator   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 27
c 0
b 0
f 0
dl 0
loc 57
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\CodeGenerator;
5
6
use Nette\PhpGenerator\ClassType;
7
use Nette\PhpGenerator\Dumper;
8
use Nette\PhpGenerator\PhpNamespace;
9
use Sirius\Orm\Blueprint\Mapper;
10
11
class EntityBaseGenerator
12
{
13
    /**
14
     * @var ClassType
15
     */
16
    protected $class;
17
18
    /**
19
     * @var PhpNamespace
20
     */
21
    protected $namespace;
22
23
    /**
24
     * @var Mapper
25
     */
26
    protected $mapper;
27
    /**
28
     * @var Dumper
29
     */
30
    protected $dumper;
31
32
    public function __construct(Mapper $mapper)
33
    {
34
        $this->dumper    = new Dumper();
35
        $this->mapper    = $mapper;
36
        $this->namespace = new PhpNamespace($mapper->getEntityNamespace());
37
        $this->class     = new ClassType($mapper->getEntityClass() . 'Base', $this->namespace);
38
        $this->class->setAbstract(true);
39
    }
40
41
    public function getClass()
42
    {
43
        $this->build();
44
45
        return $this->class;
46
    }
47
48
    protected function build()
49
    {
50
        if ($this->mapper->getEntityStyle() === Mapper::ENTITY_STYLE_PROPERTIES) {
51
            $this->namespace->addUse(\Sirius\Orm\Entity\GenericEntity::class);
52
            $this->class->setExtends('GenericEntity');
53
        } else {
54
            $this->namespace->addUse(\Sirius\Orm\Entity\ClassMethodsEntity::class);
55
            $this->class->setExtends('ClassMethodsEntity');
56
        }
57
58
        $constructor = $this->class->addMethod('__construct')
59
                                   ->setBody('parent::__construct($attributes, $state);');
60
        $constructor->addParameter('attributes')
61
                    ->setType('array')
62
                    ->setDefaultValue([]);
63
        $constructor->addParameter('state')
64
                    ->setType('string')
65
                    ->setDefaultValue(null);
66
67
        $this->class = $this->mapper->getOrm()->applyObservers($this->mapper->getName() . '_base_entity', $this->class);
68
    }
69
}
70