Passed
Branch master (d88b3b)
by Prateek
03:56
created

Module::getTabTitles()   A

Complexity

Conditions 6
Paths 16

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 16
nop 0
dl 0
loc 19
rs 9.2222
1
<?php
2
namespace Prateekkarki\Laragen\Models;
3
use Prateekkarki\Laragen\Models\TypeResolver;
4
use Illuminate\Support\Str;
5
6
class Module
7
{
8
    protected $name;
9
10
11
    public function __construct($moduleName, $moduleData)
12
    {
13
        $this->name = $moduleName;
14
15
        $this->seoFields = $moduleData['additional_fields']['seo'] ?? config('laragen.options.seo_fields');
0 ignored issues
show
Bug Best Practice introduced by
The property seoFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
16
        $this->genericFields = $moduleData['additional_fields']['generic'] ?? config('laragen.options.generic_fields');
0 ignored issues
show
Bug Best Practice introduced by
The property genericFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
17
        unset($moduleData['additional_fields']);
18
19
        $this->seedableData = $moduleData['data'] ?? false;
0 ignored issues
show
Bug Best Practice introduced by
The property seedableData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
20
        unset($moduleData['data']);
21
22
        $moduleStructure = $moduleData['structure'] ?? (!empty($moduleData) ? $moduleData : ['title' => 'string|max:128']);
23
        unset($moduleData['structure']);
24
25
        // $this->multipleData = [];
26
        $this->multipleData = array_filter($moduleStructure, function($elem) {
0 ignored issues
show
Bug Best Practice introduced by
The property multipleData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
            return (is_array($elem)) ? true : false;
28
        });
29
        
30
        if ($this->genericFields) {
31
            $moduleStructure['sort'] = 'integer';
32
            $moduleStructure['status'] = 'boolean';
33
        }
34
35
        if ($this->seoFields) {
36
            $moduleStructure['seo_title'] = 'string|max:192';
37
            $moduleStructure['seo_keywords'] = 'string|max:256';
38
            $moduleStructure['seo_description'] = 'string|max:500';
39
        }
40
41
        $this->columnsData = [];
0 ignored issues
show
Bug Best Practice introduced by
The property columnsData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        $this->seedData = $moduleData['data'] ?? null;
0 ignored issues
show
Bug Best Practice introduced by
The property seedData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
43
        $this->displayColumns = [];
0 ignored issues
show
Bug Best Practice introduced by
The property displayColumns does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
44
        foreach ($moduleStructure as $column => $typeOptions) {
45
            $data = new TypeResolver($this->name, $column, $typeOptions);
46
            $type = $data->getLaragenType();
47
            $this->columnsData[$column] = $type;
48
            if($type->isDisplay())
49
                $this->displayColumns[] = $type;
50
        }
51
52
        if(sizeof($this->displayColumns)==0){
53
            $this->displayColumns[] = array_values($this->columnsData)[0];
54
        }
55
56
57
    }
58
59
    public function getTabTitles()
60
    {
61
        $tabs = ['General'];
62
        if (sizeof($this->getFilteredColumns(['isParent', 'hasPivot']))) {
63
            $tabs[] = 'Relations';
64
        }
65
        if (sizeof($this->getFilteredColumns('hasFile'))) {
66
            $tabs[] = 'Attachments';
67
        }
68
        if (sizeof($this->getFilteredColumns('hasImage'))) {
69
            $tabs[] = 'Images';
70
        }
71
        if (sizeof($this->getFilteredColumns('isMultipleType'))) {
72
            foreach ($this->getFilteredColumns('isMultipleType') as $type) {
73
                $tabs[] = Str::plural($type->getChildModel());
74
            }
75
        }
76
        $tabs[] = 'Seo';
77
        return $tabs;
78
    }
79
80
    public function getTabs()
81
    {
82
        $tabs = [['general', 'hasOptions']];
83
        if (sizeof($this->getFilteredColumns(['isParent', 'hasPivot']))) {
84
            $tabs[] = ['isParent', 'hasPivot'];
85
        }
86
        if (sizeof($this->getFilteredColumns('hasFile'))) {
87
            $tabs[] = 'hasFile';
88
        }
89
        if (sizeof($this->getFilteredColumns('hasImage'))) {
90
            $tabs[] = 'hasImage';
91
        }
92
        if (sizeof($this->getFilteredColumns('isMultipleType'))) {
93
            foreach ($this->getFilteredColumns('isMultipleType') as $type) {
94
                $tabs[] = Str::plural($type->getChildModel());
95
            }
96
        }
97
        $tabs[] = 'Seo';
98
        return $tabs;
99
    }
100
101
    public function getColumnsData()
102
    {
103
        return $this->columnsData;
104
    }
105
106
    public function getDisplayColumns()
107
    {
108
        return $this->displayColumns;
109
    }
110
111
    public function getPivotalColumns()
112
    {
113
        $relativeTypes = [];
114
        foreach($this->columnsData as $type){
115
            if($type->isRelational()&&$type->hasPivot()){
116
                $relativeTypes[] = $type;
117
            }
118
        }
119
        return $relativeTypes;
120
    }
121
122
    public function getFilteredColumns($options = [], $columnsOnly = false)
123
    {
124
        $filteredTypes = [];
125
        $options = is_array($options) ? $options : [$options];
126
        foreach($this->columnsData as $type){
127
            foreach ($options as $option) {
128
                if($type->$option()){
129
                    $filteredTypes[] = $columnsOnly ? $type->getColumn() : $type;
130
                    break;
131
                }
132
            }
133
        }
134
        return $filteredTypes;
135
    }
136
137
    public function getColumns($onlyNonRelational = false, $columnsOnly = false)
138
    {
139
        $columns = [];
140
        foreach($this->columnsData as $type){
141
            if($onlyNonRelational && $type->isRelational()){
142
                continue;
143
            }
144
            if($columnsOnly){
145
                $columns[] = $type->getColumnKey(); 
146
            }else{
147
                $columns[$type->getColumnKey()] = $type;
148
            }
149
        }
150
        return $columns;
151
    }
152
153
    public function getName()
154
    {
155
        return $this->name;
156
    }
157
158
    public function hasPivotRelations()
159
    {
160
        $hasRelations = false;
161
        foreach($this->columnsData as $column => $type){
162
            if($type->isRelational()&&$type->hasPivot()){
163
                $hasRelations = true;
164
                break;
165
            }
166
        }
167
        return $hasRelations;
168
    }
169
170
    public function getMultipleColumns()
171
    {
172
        return $this->multipleData;
173
    }
174
175
    public function getLastColumn()
176
    {
177
        $keyArray = array_keys($this->getColumns(true, true));
178
        $lastColumn = array_pop($keyArray);
179
        return $lastColumn;
180
    }
181
    
182
    public function getModuleName()
183
    {
184
        return $this->name;
185
    }
186
187
    public function getModuleDisplayName()
188
    {
189
        return Str::title(str_replace('_', '', $this->name));
190
    }
191
192
    public function getModelName()
193
    {
194
        return ucfirst(Str::camel(str_singular($this->name)));
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

194
        return ucfirst(Str::camel(/** @scrutinizer ignore-deprecated */ str_singular($this->name)));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
195
    }
196
197
    public function getModelNamePlural()
198
    {
199
        return ucfirst(Str::camel($this->name));
200
    }
201
202
    public function getModelNameLowercase()
203
    {
204
        return str_singular($this->name);
0 ignored issues
show
Deprecated Code introduced by
The function str_singular() has been deprecated: Str::singular() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

204
        return /** @scrutinizer ignore-deprecated */ str_singular($this->name);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
205
    }
206
}
207