Completed
Push — master ( 53e43c...41b088 )
by Vitaly
03:02
created

VirtualEntity::createDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 22.03.16 at 15:46
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsoncms\api\generator\metadata\Virtual;
10
11
/**
12
 * Virtual entity class generator.
13
 *
14
 * @package samsoncms\api\generator
15
 */
16
class VirtualEntity extends Generic
17
{
18
    /**
19
     * Class definition generation part.
20
     *
21
     * @param Virtual $metadata Entity metadata
22
     */
23
    protected function createDefinition($metadata)
24
    {
25
        /**
26
         * TODO: Parent problem
27
         * Should be changed to merging fields instead of extending with OOP for structure_relation support
28
         * or creating traits and using them on shared parent entities.
29
         */
30
        $parentClass = null !== $metadata->parent
31
            ? $metadata->parent->entityClassName
32
            : '\\'.\samsoncms\api\Entity::class;
33
34
        $this->generator
35
            ->multiComment(array('"' . $metadata->entityRealName . '" database entity class'))
36
            ->defClass($this->className, $parentClass)
37
            ->newLine('use TableTrait;')
38
            ->newLine();;
0 ignored issues
show
Coding Style introduced by
It is generally recommended to place each PHP statement on a line by itself.

Let’s take a look at an example:

// Bad
$a = 5; $b = 6; $c = 7;

// Good
$a = 5;
$b = 6;
$c = 7;
Loading history...
39
    }
40
41
    /**
42
     * Class constants generation part.
43
     *
44
     * @param Virtual $metadata Entity metadata
45
     */
46
    protected function createConstants($metadata)
47
    {
48
        $this->generator
49
            ->commentVar('string', 'Entity full class name, use ::class instead')
50
            ->defClassConst('ENTITY', $metadata->entityClassName)
51
            ->commentVar('string', 'Entity manager full class name')
52
            ->defClassConst('MANAGER', $metadata->entityClassName . 'Query')
53
            ->commentVar('string', 'Entity database identifier')
54
            ->defClassConst('IDENTIFIER', $metadata->entityID)
55
            ->commentVar('string', 'Not transliterated entity name')
56
            ->defClassConst('NAME', $metadata->entityRealName);
57
58
        // Create all entity fields constants storing each additional field metadata
59
        foreach ($metadata->fields as $fieldID => $fieldName) {
60
            $this->generator
61
                ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name')
62
                ->defClassConst('F_' . $fieldName, $fieldName)
63
                ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier')
64
                ->defClassConst('F_' . $fieldName . '_ID', $fieldID);
65
        }
66
    }
67
68
    /**
69
     * Class fields generation part.
70
     *
71
     * @param Virtual $metadata Entity metadata
72
     */
73 View Code Duplication
    protected function createFields($metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        foreach ($metadata->fields as $fieldID => $fieldName) {
76
            $this->generator
77
                ->commentVar($metadata->types[$fieldID], $metadata->fieldDescriptions[$fieldID])
78
                ->defClassVar('$' . $fieldName, 'public');
79
        }
80
    }
81
}
82
//[PHPCOMPRESSOR(remove,end)]