Passed
Pull Request — master (#1174)
by Diego
07:14
created

InputFileKrajee   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 56
c 1
b 0
f 0
dl 0
loc 202
ccs 62
cts 62
cp 1
rs 10
wmc 15

8 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 3 1
A getPresetModeCfg() 0 23 3
A __construct() 0 24 2
A makePluginDefaultCfg() 0 15 3
A makeInvalidFeedbackClass() 0 9 2
A makeAvatarCfg() 0 23 1
A makePluginInputGroupClassCfg() 0 14 2
A makeMinimalistCfg() 0 9 1
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());
0 ignored issues
show
Bug introduced by
$this->getPresetModeCfg() of type void is incompatible with the type array expected by parameter $arrays of array_merge(). ( Ignorable by Annotation )

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

56
        $this->config = array_merge($this->config, /** @scrutinizer ignore-type */ $this->getPresetModeCfg());
Loading history...
Bug introduced by
Are you sure the usage of $this->getPresetModeCfg() targeting JeroenNoten\LaravelAdmin...jee::getPresetModeCfg() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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 void
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
136 4
            case 'avatar':
137 1
                $modeCfg = $this->makeAvatarCfg();
138 1
                break;
139
140 4
            case 'minimalist':
141 1
                $modeCfg = $this->makeMinimalistCfg();
142 1
                break;
143
144
            default:
145 3
                break;
146
        }
147
148
        // Return the preset mode config.
149
150 4
        return $modeCfg;
151
    }
152
153
    /**
154
     * Generates the plugin configuration for an avatar image upload mode.
155
     *
156
     * @return array
157
     */
158 1
    protected function makeAvatarCfg()
159
    {
160
        // Setup the additional classes for the upload preview zone.
161
162 1
        $previewZoneClasses = ['bg-light', 'd-flex', 'justify-content-center'];
163
164
        // Setup the allowed image extensions.
165
166 1
        $allowedImageExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tif', 'tiff'];
167
168
        // Return the configuration for the avatar mode.
169
170 1
        return [
171 1
            'showUpload' => false,
172 1
            'showClose' => false,
173 1
            'showCaption' => false,
174 1
            'showCancel' => false,
175 1
            'browseOnZoneClick' => true,
176 1
            'allowedFileExtensions' => $allowedImageExtensions,
177 1
            'maxFileCount' => 1,
178 1
            'previewClass' => implode(' ', $previewZoneClasses),
179 1
            'browseLabel' => '',
180 1
            'removeLabel' => '',
181 1
        ];
182
    }
183
184
    /**
185
     * Generates the plugin configuration for a minimalist style.
186
     *
187
     * @return array
188
     */
189 1
    protected function makeMinimalistCfg()
190
    {
191
        // Return the configuration for the avatar mode.
192
193 1
        return [
194 1
            'showPreview' => false,
195 1
            'browseLabel' => '',
196 1
            'removeLabel' => '',
197 1
            'uploadLabel' => '',
198 1
        ];
199
    }
200
201
    /**
202
     * Get the view / contents that represent the component.
203
     *
204
     * @return \Illuminate\View\View|string
205
     */
206 1
    public function render()
207
    {
208 1
        return view('adminlte::components.form.input-file-krajee');
209
    }
210
}
211