Completed
Push — master ( 89be15...80fd60 )
by Igor
11:15
created

FileRulesDescription::isImageMinAndMaxSize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace app\modules\admin\helpers;
4
5
use Yii;
6
use yii\helpers\ArrayHelper;
7
8
/**
9
 * Formatting of validation rules
10
 */
11
class FileRulesDescription
12
{
13
    private static function maxSizeDescription(array $rules): string
14
    {
15
        $rules = ArrayHelper::getValue($rules, 'maxSize');
16
17
        if ($rules === null) {
18
            return '';
19
        }
20
21 11
        $rules = Yii::$app->formatter->asShortSize($rules);
22
        return Yii::t('app.msg', 'Max. file size') . ': ' . $rules . ' ';
23 11
    }
24 11
25
    private static function maxFilesDescription(array $rules): string
26 11
    {
27
        $rules = ArrayHelper::getValue($rules, 'maxFiles');
28 11
29
        if ($rules === null) {
30 11
            return '';
31 8
        }
32
33 3
        return Yii::t('app.msg', 'Max. file number') . ': ' . $rules . ' ';
34 3
    }
35
36
    private static function extensionDescription(array $rules): string
37 11
    {
38
        $rules = ArrayHelper::getValue($rules, 'extensions');
39 11
40
        if ($rules === null) {
41 11
            return '';
42 10
        }
43
44 1
        $rules = strtoupper(implode(', ', $rules));
45
        return Yii::t('app.msg', 'File types') . ': ' . $rules . ' ';
46
    }
47 11
48
    private static function imageSizeDescription(array $rules): array
49 11
    {
50
        $rules = ArrayHelper::getValue($rules, 'imageSize');
51 11
52 8
        if ($rules === null || !count($rules)) {
53
            return [];
54 3
        }
55 3
56
        $text = [];
0 ignored issues
show
Unused Code introduced by
$text is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
57
        switch ($rules) {
58 11
            case self::isImageStrictSize($rules):
59
                $text = self::imageStrictSizeDescription($rules);
60 11
                break;
61
            case self::isImageMinAndMaxSize($rules):
62 11
                $text = self::imageMinAndMaxSizeDescription($rules);
63 1
                break;
64
            case self::isImageMinSize($rules):
65
                $text = array_merge(
66 10
                    self::imageMinSizeDescription($rules), 
67 10
                    self::imageRulesDescription($rules, ['minWidth', 'minHeight'])
68 10
                );
69 10
                break;
70
            case self::isImageMaxSize($rules):
71 10
                $text = array_merge(
72
                    self::imageMaxSizeDescription($rules), 
73 10
                    self::imageRulesDescription($rules, ['maxWidth', 'maxHeight'])
74 1
                );
75 1
                break;
76 9
            default:
77 1
                $text = self::imageRulesDescription($rules);
78 1
                break;
79 1
        }
80 8
81 3
        return $text;
82 3
    }
83 3
84 5
    private static function isImageStrictSize(array $rules): bool
85 1
    {
86 1
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
87 1
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
88
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
89 4
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
90 4
91
        return count($rules) == 4 && ($maxWidth == $minWidth && $maxHeight == $minHeight);
92
    }
93 10
94
    private static function imageStrictSizeDescription(array $rules): array
95
    {
96 10
        return [
97
            Yii::t('app.msg', 'Image size') . ': ' . 
98 10
            ArrayHelper::getValue($rules, 'maxWidth') . 'x' . ArrayHelper::getValue($rules, 'maxHeight') . 'px'
99 10
        ];
100 10
    }
101 10
102
    private static function isImageMinAndMaxSize(array $rules): bool
103 10
    {
104
        return count($rules) == 4;
105
    }
106 9
107
    private static function imageMinAndMaxSizeDescription(array $rules): array
108 9
    {
109
        return [
110
            Yii::t('app.msg', 'Min. size of image') . ': ' . 
111 8
            ArrayHelper::getValue($rules, 'minWidth') . 'x' . ArrayHelper::getValue($rules, 'minHeight') . 'px',
112
            Yii::t('app.msg', 'Max. size of image') . ': ' . 
113 8
            ArrayHelper::getValue($rules, 'maxWidth') . 'x' . ArrayHelper::getValue($rules, 'maxHeight') . 'px'
114 8
        ];
115
    }
116 8
117
    private static function isImageMinSize(array $rules): bool
118
    {
119 5
        $minWidth  = ArrayHelper::getValue($rules, 'minWidth');
120
        $minHeight = ArrayHelper::getValue($rules, 'minHeight');
121 5
122 5
        return (count($rules) == 2 || count($rules) == 3) && $minWidth && $minHeight;
123
    }
124 5
125
    private static function imageMinSizeDescription(array $rules): array
126
    {
127 8
        return [
128
            Yii::t('app.msg', 'Min. size of image') . ': ' . 
129 8
            ArrayHelper::getValue($rules, 'minWidth') . 'x' . ArrayHelper::getValue($rules, 'minHeight') . 'px'
130 4
        ];
131
    }
132
133 8
    private static function isImageMaxSize(array $rules): bool
134 8
    {
135
        $maxWidth  = ArrayHelper::getValue($rules, 'maxWidth');
136 4
        $maxHeight = ArrayHelper::getValue($rules, 'maxHeight');
137 1
138 1
        return (count($rules) == 2 || count($rules) == 3) && $maxWidth && $maxHeight;
139 3
    }
140 1
141 1
    private static function imageMaxSizeDescription(array $rules): array
142 2
    {
143 1
        return [
144 1
            Yii::t('app.msg', 'Max. size of image') . ': ' . 
145 1
            ArrayHelper::getValue($rules, 'maxWidth') . 'x' . ArrayHelper::getValue($rules, 'maxHeight') . 'px'
146 1
        ];
147 4
    }
148
149
    private static function imageRulesDescription(array $rules, array $exclude = []): array
150
    {
151 8
        foreach ($exclude as $item) {
152
            unset($rules[$item]);
153
        }
154
155
        $text = [];
156
        foreach ($rules as $rule => $value) {
157
            switch ($rule) {
158
                case 'minWidth':
159 11
                    $text[] = Yii::t('app.msg', 'Min. width') . ' ' . $value . 'px';
160
                    break;
161 11
                case 'minHeight':
162
                    $text[] = Yii::t('app.msg', 'Min. height') . ' ' . $value . 'px';
163 11
                    break;
164 11
                case 'maxWidth':
165 11
                    $text[] = Yii::t('app.msg', 'Max. width') . ' ' . $value . 'px';
166 11
                    break;
167
                case 'maxHeight':
168 11
                    $text[] = Yii::t('app.msg', 'Max. height') . ' ' . $value . 'px';
169
                    break;
170
            }
171
        }
172
173
        return $text;
174
    }
175
176
    /**
177
     * Get a description of the validation rules in as text
178
     *
179
     * @param array $rules Validation rules
180
     * @return string
181
     */
182
    public static function asDescription(array $rules): string
183
    {
184
        $text = self::imageSizeDescription($rules);
185
        $text[] = self::extensionDescription($rules);
186
        $text[] = self::maxSizeDescription($rules);
187
        $text[] = self::maxFilesDescription($rules);
188
189
        return implode('<br>', array_filter($text));
190
    }
191
}
192