Completed
Push — master ( 0143f1...11d9fa )
by Vitaly
03:03
created

VirtualEntity::createMethods()   B

Complexity

Conditions 4
Paths 7

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
rs 8.5806
cc 4
eloc 20
nc 7
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\Field;
10
use samsoncms\api\generator\metadata\VirtualMetadata;
11
12
/**
13
 * Virtual entity class generator.
14
 *
15
 * @package samsoncms\api\generator
16
 */
17
class VirtualEntity extends RealEntity
18
{
19
    /**
20
     * Class uses generation part.
21
     *
22
     * @param RealMetadata $metadata Entity metadata
23
     */
24
    protected function createUses($metadata)
25
    {
26
        $this->generator
27
            ->newLine('use samsonframework\core\ViewInterface;')
28
            ->newLine('use samsonframework\orm\QueryInterface;')
29
            ->newLine();
30
    }
31
32
    /**
33
     * Class definition generation part.
34
     *
35
     * @param VirtualMetadata $metadata Entity metadata
36
     */
37
    protected function createDefinition($metadata)
38
    {
39
        /**
40
         * TODO: Parent problem
41
         * Should be changed to merging fields instead of extending with OOP for structure_relation support
42
         * or creating traits and using them on shared parent entities.
43
         */
44
        $parentClass = null !== $metadata->parent
45
            ? $metadata->parent->entityClassName
46
            : '\\'.\samsoncms\api\Entity::class;
47
48
        $this->generator
49
            ->multiComment(array('"' . $metadata->entityRealName . '" database entity class'))
50
            ->defClass($this->className, $parentClass)
51
            ->newLine('use TableTrait;')
52
            ->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...
53
    }
54
55
    /**
56
     * Class constants generation part.
57
     *
58
     * @param VirtualMetadata $metadata Entity metadata
59
     */
60
    protected function createConstants($metadata)
61
    {
62
        $this->generator
63
            ->commentVar('string', 'Entity full class name, use ::class instead')
64
            ->defClassConst('ENTITY', $metadata->entityClassName)
65
            ->commentVar('string', 'Entity manager full class name')
66
            ->defClassConst('MANAGER', $metadata->entityClassName . 'Query')
67
            ->commentVar('string', 'Entity database identifier')
68
            ->defClassConst('IDENTIFIER', $metadata->entityID)
69
            ->commentVar('string', 'Not transliterated entity name')
70
            ->defClassConst('NAME', $metadata->entityRealName);
71
72
        // Create all entity fields constants storing each additional field metadata
73
        foreach ($metadata->fields as $fieldID => $fieldName) {
74
            $this->generator
75
                ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name')
76
                ->defClassConst('F_' . $fieldName, $fieldName)
77
                ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier')
78
                ->defClassConst('F_' . $fieldName . '_ID', $fieldID);
79
        }
80
    }
81
82
    /**
83
     * Class fields generation part.
84
     *
85
     * @param VirtualMetadata $metadata Entity metadata
86
     */
87 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...
88
    {
89
        foreach ($metadata->fields as $fieldID => $fieldName) {
90
            $this->generator
91
                ->commentVar($metadata->types[$fieldID], $metadata->fieldDescriptions[$fieldID])
92
                ->defClassVar('$' . $fieldName, 'public');
93
        }
94
    }
95
96
    /**
97
     * Class methods generation part.
98
     *
99
     * @param VirtualMetadata $metadata Entity metadata
100
     */
101
    protected function createMethods($metadata)
102
    {
103
        $methods = [];
104
        // Generate Query::where() analog for specific field.
105
        foreach ($metadata->fields as $fieldID => $fieldName) {
106
            try {
107
                // We need only gallery fields
108
                if ($metadata->allFieldCmsTypes[$fieldID] === Field::TYPE_GALLERY) {
109
                    $galleryName = rtrim($fieldName, 'Gallery') . 'Gallery';
110
111
                    $code = "\n\t" . '/**';
112
                    $code .= "\n\t" . ' * Get ' . $fieldName . '(#' . $fieldID . ') gallery collection instance.';
113
                    $code .= "\n\t" . ' * @param ViewInterface $renderer Render instance';
114
                    $code .= "\n\t" . ' *';
115
                    $code .= "\n\t" . ' * @return GalleryCollection Gallery collection instance';
116
                    $code .= "\n\t" . ' */';
117
                    $code .= "\n\t" . 'public function ' . lcfirst($galleryName) . '(ViewInterface $renderer)';
118
                    $code .= "\n\t" . '{';
119
                    $code .= "\n\t\t" . 'return (new GalleryCollection($renderer, $this->query))->materialID($this->id)->materialFieldID(' . $fieldID . ');';
120
                    $code .= "\n\t" . '}';
121
122
                    $methods[] = $code;
123
                }
124
            } catch (\Exception $e) {
125
                throw new \Exception($metadata->entity . ' cms field type for [' . $fieldName . '] not found');
126
            }
127
        }
128
129
        // Add method text to generator
130
        $this->generator->text(implode("\n", $methods));
131
    }
132
}
133
//[PHPCOMPRESSOR(remove,end)]