| Total Complexity | 4 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 | } |
||
| 70 |