Passed
Push — master ( 6cb615...78a13f )
by Diego
03:31
created

InputFileKrajee::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 1
b 0
f 0
nc 2
nop 11
dl 0
loc 24
ccs 10
cts 10
cp 1
crap 2
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace JeroenNoten\LaravelAdminLte\View\Components\Form;
4
5
use Illuminate\Support\Str;
6
7
class InputFileKrajee extends InputGroupComponent
8
{
9
    /**
10
     * The Krajee file input plugin configuration parameters. Array with
11
     * 'key => value' pairs, where the key should be an existing configuration
12
     * property of the Krajee file input plugin.
13
     *
14
     * @var array
15
     */
16
    public $config;
17
18
    /**
19
     * The plugin preset mode. Used to make specific plugin configuration for
20
     * some particular scenarios. The current supported set of values are:
21
     * 'avatar', 'minimalist'.
22
     *
23
     * @var string
24
     */
25
    public $presetMode;
26
27
    /**
28
     * Create a new component instance.
29
     * Note this component requires the Krajee 'bootstrap-fileinput' plugin.
30
     *
31
     * @return void
32
     */
33 4
    public function __construct(
34
        $name, $id = null, $label = null, $igroupSize = null, $labelClass = null,
35
        $fgroupClass = null, $igroupClass = null, $disableFeedback = null,
36
        $errorKey = null, $config = [], $presetMode = null
37
    ) {
38 4
        parent::__construct(
39 4
            $name, $id, $label, $igroupSize, $labelClass, $fgroupClass,
40 4
            $igroupClass, $disableFeedback, $errorKey
41 4
        );
42
43 4
        $this->config = is_array($config) ? $config : [];
44 4
        $this->presetMode = $presetMode;
45
46
        // Make some default configuration for the underlying plugin.
47
48 4
        $this->makePluginDefaultCfg();
49
50
        // Make the plugin 'inputGroupClass' configuration.
51
52 4
        $this->makePluginInputGroupClassCfg();
53
54
        // Get the preset mode config and merge with the current plugin config.
55
56 4
        $this->config = array_merge($this->config, $this->getPresetModeCfg());
57
    }
58
59
    /**
60
     * Make the class attribute for the invalid feedback block.
61
     *
62
     * @return string
63
     */
64 2
    public function makeInvalidFeedbackClass()
65
    {
66 2
        $classes = ['invalid-feedback', 'd-block'];
67
68 2
        if ($this->presetMode == 'avatar') {
69 1
            $classes[] = 'text-center';
70
        }
71
72 2
        return implode(' ', $classes);
73
    }
74
75
    /**
76
     * Make some default configuration for the plugin, when it's not provided
77
     * by the config property.
78
     *
79
     * @return void
80
     */
81 4
    protected function makePluginDefaultCfg()
82
    {
83
        // By default, force the plugin theme to 'Font Awesome 5'. Note this
84
        // requires the theme files provided by the plugin to be imported.
85
86 4
        if (! isset($this->config['theme'])) {
87 3
            $this->config['theme'] = 'fa5';
88
        }
89
90
        // By default, force the plugin language to the configured application
91
        // locale. However, note you will still need to import the locale
92
        // files provided by the plugin.
93
94 4
        if (! isset($this->config['language'])) {
95 3
            $this->config['language'] = config('app.locale');
96
        }
97
    }
98
99
    /**
100
     * Make the plugin 'inputGroupClass' configuration. These classes will be
101
     * appended to the 'input-group' DOM element that is internally generated
102
     * by the plugin.
103
     *
104
     * @return void
105
     */
106 4
    protected function makePluginInputGroupClassCfg()
107
    {
108
        // Use the parent method to create the input group classes, but
109
        // remove the 'input-group' CSS class to avoid duplication, since it's
110
        // already added by the underlying plugin.
111
112 4
        $inputGroupClasses = Str::of($this->makeInputGroupClass())
113 4
            ->replaceFirst('input-group', '')
114 4
            ->trim();
115
116 4
        if (empty($this->config['inputGroupClass'])) {
117 3
            $this->config['inputGroupClass'] = $inputGroupClasses;
118
        } else {
119 1
            $this->config['inputGroupClass'] .= " {$inputGroupClasses}";
120
        }
121
    }
122
123
    /**
124
     * Get the preset mode configuration.
125
     *
126
     * @return array
127
     */
128 4
    protected function getPresetModeCfg()
129
    {
130 4
        $modeCfg = [];
131
132
        // Check for valid preset mode and generate the related plugin config.
133
134 4
        switch ($this->presetMode) {
135 4
            case 'avatar':
136 1
                $modeCfg = $this->makeAvatarCfg();
137 1
                break;
138
139 4
            case 'minimalist':
140 1
                $modeCfg = $this->makeMinimalistCfg();
141 1
                break;
142
143
            default:
144 3
                break;
145
        }
146
147
        // Return the preset mode config.
148
149 4
        return $modeCfg;
150
    }
151
152
    /**
153
     * Generates the plugin configuration for an avatar image upload mode.
154
     *
155
     * @return array
156
     */
157 1
    protected function makeAvatarCfg()
158
    {
159
        // Setup the additional classes for the upload preview zone.
160
161 1
        $previewZoneClasses = ['bg-light', 'd-flex', 'justify-content-center'];
162
163
        // Setup the allowed image extensions.
164
165 1
        $allowedImageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff'];
166
167
        // Return the configuration for the avatar mode.
168
169 1
        return [
170 1
            'showUpload' => false,
171 1
            'showClose' => false,
172 1
            'showCaption' => false,
173 1
            'showCancel' => false,
174 1
            'browseOnZoneClick' => true,
175 1
            'allowedFileExtensions' => $allowedImageExtensions,
176 1
            'maxFileCount' => 1,
177 1
            'previewClass' => implode(' ', $previewZoneClasses),
178 1
            'browseLabel' => '',
179 1
            'removeLabel' => '',
180 1
        ];
181
    }
182
183
    /**
184
     * Generates the plugin configuration for a minimalist style.
185
     *
186
     * @return array
187
     */
188 1
    protected function makeMinimalistCfg()
189
    {
190
        // Return the configuration for the avatar mode.
191
192 1
        return [
193 1
            'showPreview' => false,
194 1
            'browseLabel' => '',
195 1
            'removeLabel' => '',
196 1
            'uploadLabel' => '',
197 1
        ];
198
    }
199
200
    /**
201
     * Get the view / contents that represent the component.
202
     *
203
     * @return \Illuminate\View\View|string
204
     */
205 1
    public function render()
206
    {
207 1
        return view('adminlte::components.form.input-file-krajee');
208
    }
209
}
210