Completed
Push — master ( 805de4...131e3d )
by Vitaly
02:35
created

ApplicationCollection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 8
c 2
b 1
f 1
lcom 1
cbo 3
dl 0
loc 144
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A createUses() 0 7 1
A createDefinition() 0 6 1
B createCollectionField() 0 24 3
B createConstructor() 0 42 2
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 24.03.16 at 13:34
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsonphp\generator\Generator;
10
11
/**
12
 * SamsonCMS application collection class generator.
13
 *
14
 * @package samsoncms\application\generator
15
 */
16
class ApplicationCollection extends \samsoncms\api\generator\Generic
17
{
18
    /** Default generic class name */
19
    const DEFAULT_GENERIC_TYPE = 'Generic';
20
21
    /** Default control class name */
22
    const DEFAULT_GENERIC_CONTROL_TYPE = 'Control';
23
24
    /** Custom css selector in generic constructor */
25
    const DEFAULT_CUSTOM_TYPE_CSS = '';
26
27
    /** User can edit field in list of application */
28
    const DEFAULT_CUSTOM_TYPE_EDITABLE = 'false';
29
30
    /** Field can be sortable in list of application */
31
    const DEFAULT_CUSTOM_TYPE_SORTABLE = 'false';
32
33
    /** Default namespace of custom types */
34
    const DEFAULT_CUSTOM_TYPE_NAMESPACE = '\\samsonphp\\cms\\types\\';
35
36
    /**
37
     * Query constructor.
38
     *
39
     * @param Generator $generator
40
     * @param           $metadata
41
     */
42
    public function __construct(Generator $generator, $metadata)
43
    {
44
        parent::__construct($generator, $metadata);
45
46
        $this->className .= 'ApplicationCollection';
47
    }
48
49
    /**
50
     * Class uses generation part.
51
     *
52
     * @param \samsoncms\api\generator\metadata\Generic $metadata Entity metadata
53
     */
54
    protected function createUses($metadata)
55
    {
56
        $this->generator
57
            ->text('use samsoncms\field\Control;')
58
            ->text('use samsoncms\field\Generic;')
59
            ->newLine();
60
    }
61
62
    /**
63
     * Class definition generation part.
64
     *
65
     * @param \samsoncms\api\generator\metadata\Application $metadata Entity metadata
66
     */
67
    protected function createDefinition($metadata)
68
    {
69
        $this->generator
70
            ->multiComment(array('Collection for SamsonCMS application "'.$metadata->name.'"'))
71
            ->defClass($this->className, '\\'.\samsoncms\app\material\Collection::class);
72
    }
73
74
    /**
75
     * Create generic constructor for collection.
76
     *
77
     * @param        $customType
78
     * @param        $name
79
     * @param        $description
80
     * @param int    $type
81
     * @param string $css
82
     * @param string $editable
83
     * @param string $sortable
84
     *
85
     * @return string
86
     */
87
    public function createCollectionField(
88
        $customType,
89
        $name,
90
        $description,
91
        $type,
92
        $css = self::DEFAULT_CUSTOM_TYPE_CSS,
93
        $editable = self::DEFAULT_CUSTOM_TYPE_CSS,
94
        $sortable = self::DEFAULT_CUSTOM_TYPE_CSS
95
    )
96
    {
0 ignored issues
show
Coding Style introduced by
The closing parenthesis and the opening brace of a multi-line function declaration must be on the same line
Loading history...
97
98
        // If custom type is exists then use it or use default generic type
99
        if ($customType) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
100
101
            // If field has namespace then use it or use default namespace
102
            $class = preg_match('/\\\/', $customType) ? $customType : self::DEFAULT_CUSTOM_TYPE_NAMESPACE . $customType;
103
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
104
105
            $class = self::DEFAULT_GENERIC_TYPE;
106
        }
107
108
109
        return "\n\t\t\tnew {$class}('$name', t('$description', true), $type, '$css', $editable, $sortable)";
110
    }
111
112
    /**
113
     * Class constructor generation part.
114
     *
115
     * @param \samsoncms\api\generator\metadata\Application $metadata Entity metadata
116
     */
117
    protected function createConstructor($metadata)
118
    {
119
        $constructorCode = <<<'EOD'
120
    /**
121
     * Generic SamsonCMS application collection constructor
122
     *
123
     * @param RenderInterface $renderer View render object
124
     * @param QueryInterface $query Query object
125
     */
126
    public function __construct($renderer, $query = null, $pager = null)
127
    {
128
        parent::__construct($renderer, $query, $pager);
129
130
        // Generic of fields
131
        $this->fields = array({{fields}});
132
    }
133
EOD;
134
        // Iterate all application fields and create generic constructor for them
135
        $genericFields = [];
136
        foreach ($metadata->showFieldsInList as $fieldID) {
137
            // Create constructor for custom type or if it not exists then use cms defined type
138
            $genericFields[] = $this->createCollectionField(
139
                $metadata->customTypeFields[$fieldID],
140
                $metadata->allFieldIDs[$fieldID],
141
                $metadata->fieldDescriptions[$fieldID],
142
                $metadata->allFieldCmsTypes[$fieldID],
143
                self::DEFAULT_CUSTOM_TYPE_CSS,
144
                self::DEFAULT_CUSTOM_TYPE_EDITABLE,
145
                self::DEFAULT_CUSTOM_TYPE_SORTABLE
146
            );
147
        }
148
149
        $constructorCode = str_replace(
150
            '{{fields}}',
151
            implode(',', array_merge(
152
                    $genericFields,
153
                    array("\n\t\t\t" . 'new ' . self::DEFAULT_GENERIC_CONTROL_TYPE . '()' . "\n\t\t"))
154
            ),
155
            $constructorCode);
156
157
        $this->generator->text($constructorCode);
0 ignored issues
show
Coding Style introduced by
It seems like the identation of this line is off (expected at least 12 spaces, but found 8).
Loading history...
158
    }
159
}
160
//[PHPCOMPRESSOR(remove,end)]
161