ApplicationCollection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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\GenericMetadata $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\ApplicationMetadata $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
     * Class constructor generation part.
76
     *
77
     * @param \samsoncms\api\generator\metadata\ApplicationMetadata $metadata Entity metadata
78
     */
79
    protected function createConstructor($metadata)
80
    {
81
        $constructorCode = <<<'EOD'
82
    /**
83
     * Generic SamsonCMS application collection constructor
84
     *
85
     * @param RenderInterface $renderer View render object
86
     * @param QueryInterface $query Query object
87
     */
88
    public function __construct($renderer, $query = null, $pager = null)
89
    {
90
        parent::__construct($renderer, $query, $pager);
91
92
        // Generic of fields
93
        $this->fields = array({{fields}});
94
    }
95
EOD;
96
        // Iterate all application fields and create generic constructor for them
97
        $genericFields = [];
98
        foreach ($metadata->showFieldsInList as $fieldID) {
99
            // Create constructor for custom type or if it not exists then use cms defined type
100
            $genericFields[] = $this->createCollectionField(
101
                $metadata->customTypeFields[$fieldID],
102
                $metadata->fields[$fieldID],
103
                $metadata->fieldDescriptions[$fieldID],
104
                $metadata->allFieldCmsTypes[$fieldID],
105
                self::DEFAULT_CUSTOM_TYPE_CSS,
106
                self::DEFAULT_CUSTOM_TYPE_EDITABLE,
107
                self::DEFAULT_CUSTOM_TYPE_SORTABLE
108
            );
109
        }
110
111
        $constructorCode = str_replace(
112
            '{{fields}}',
113
            implode(',', array_merge(
114
                    $genericFields,
115
                    array("\n\t\t\t" . 'new ' . self::DEFAULT_GENERIC_CONTROL_TYPE . '()' . "\n\t\t"))
116
            ),
117
            $constructorCode);
118
119
        $this->generator->text($constructorCode);
120
    }
121
122
    /**
123
     * Create generic constructor for collection.
124
     *
125
     * @param        $customType
126
     * @param        $name
127
     * @param        $description
128
     * @param int    $type
129
     * @param string $css
130
     * @param string $editable
131
     * @param string $sortable
132
     *
133
     * @return string
134
     */
135
    public function createCollectionField(
136
        $customType,
137
        $name,
138
        $description,
139
        $type,
140
        $css = self::DEFAULT_CUSTOM_TYPE_CSS,
141
        $editable = self::DEFAULT_CUSTOM_TYPE_CSS,
142
        $sortable = self::DEFAULT_CUSTOM_TYPE_CSS
143
    )
144
    {
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...
145
146
        // If custom type is exists then use it or use default generic type
147
        if ($customType) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
148
149
            // If field has namespace then use it or use default namespace
150
            $class = preg_match('/\\\/', $customType) ? $customType : self::DEFAULT_CUSTOM_TYPE_NAMESPACE . $customType;
151
        } else {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
152
153
            $class = self::DEFAULT_GENERIC_TYPE;
154
        }
155
156
157
        return "\n\t\t\tnew {$class}('$name', t('$description', true), $type, '$css', $editable, $sortable)";
158
    }
159
}
160
//[PHPCOMPRESSOR(remove,end)]
161