Completed
Push — master ( 35be3a...ef8e90 )
by Vitaly
02:55
created

TableTrait::createUses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
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\GenericMetadata;
10
use samsoncms\api\generator\metadata\Virtual;
11
use samsonphp\generator\Generator;
12
13
/**
14
 * Table trait class generator. As all entities should
15
 * be able to create Table class instances for their selves
16
 * this class is a trait with methods to receive all available virtual
17
 * entity tables.
18
 *
19
 * @package samsoncms\api\generator
20
 */
21
class TableTrait extends Generic
22
{
23
    /**
24
     * Query constructor.
25
     *
26
     * @param Generator $generator
27
     * @param           $metadata
28
     */
29
    public function __construct(Generator $generator, $metadata)
30
    {
31
        parent::__construct($generator, $metadata);
32
33
        $this->className = 'TableTrait';
34
    }
35
36
    /**
37
     * Class uses generation part.
38
     *
39
     * @param Virtual $metadata Entity metadata
40
     */
41
    protected function createUses($metadata)
42
    {
43
        $this->generator
44
            ->newLine('use samsonframework\core\ViewInterface;')
45
            ->newLine();
46
    }
47
48
    /**
49
     * Class definition generation part.
50
     *
51
     * @param Virtual $metadata Entity metadata
52
     */
53
    protected function createDefinition($metadata)
54
    {
55
        $this->generator
0 ignored issues
show
Bug introduced by
The method defTrait() does not seem to exist on object<samsonphp\generator\Generator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            ->multiComment(array('"TableTrait database entity class'))
57
            ->defTrait($this->className);
58
    }
59
60
    /**
61
     * Class methods generation part.
62
     *
63
     * @param Virtual $metadata Entity metadata
64
     */
65
    protected function createMethods($metadata)
66
    {
67
        $methods = [];
68
        /** @var Virtual $metadataInstance Iterate all metadata entities */
69
        foreach (GenericMetadata::$instances as $metadataInstance) {
70
            if ($metadataInstance->type === Virtual::TYPE_TABLE) {
71
                // Create table virtual entity with correct name ending
72
                $tableEntity = rtrim($metadataInstance->entity, 'Table') . 'Table';
73
74
                $code = "\n\t" . '/**';
75
                $code .= "\n\t" . ' * Create virtual ' . $metadataInstance->entityRealName . ' table instance.';
76
                $code .= "\n\t" . ' * @param ViewInterface $renderer Renderer instance';
77
                $code .= "\n\t" . ' *';
78
                $code .= "\n\t" . ' * @return $this Chaining';
79
                $code .= "\n\t" . ' */';
80
                $code .= "\n\t" . 'public function ' . lcfirst($tableEntity) . '(ViewInterface $renderer)';
81
                $code .= "\n\t" . '{';
82
                $code .= "\n\t\t" . 'return new ' . $tableEntity . '($renderer, $this->id);';
83
                $code .= "\n\t" . '}';
84
85
                $methods[] = $code;
86
            }
87
        }
88
89
        // Add method text to generator
90
        $this->generator->text(implode("\n", $methods));
91
    }
92
}
93
//[PHPCOMPRESSOR(remove,end)]