Helper::iDeviceModel()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 83
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 74
nc 2
nop 1
dl 0
loc 83
ccs 0
cts 3
cp 0
crap 6
rs 8.7468
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Support;
4
5
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
6
7
class Helper
8
{
9
    /**
10
     * Get file extension for MIME type.
11
     *
12
     * @param  string  $mimeType
13
     * @param  string  $prefix
14
     * @return string|null
15
     */
16
    public static function fileExtensionForMimeType($mimeType, $prefix = '')
17
    {
18
        $extension = ExtensionGuesser::getInstance()->guess($mimeType);
19
20
        if (! is_null($extension)) {
21
            if ('jpeg' == $extension) {
22
                $extension = 'jpg';
23
            }
24
25
            return $prefix.$extension;
26
        }
27
    }
28
29
    /**
30
     * Convert an iOS platform to the device model name.
31
     *
32
     * @see https://www.theiphonewiki.com/wiki/Models
33
     * @see https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types
34
     *
35
     * @param  string  $platform
36
     * @return string
37
     */
38
    public static function iDeviceModel($platform)
39
    {
40
        $list = [
41
            'iPhone1,1' => 'iPhone',
42
            'iPhone1,2' => 'iPhone 3G',
43
            'iPhone2,1' => 'iPhone 3GS',
44
            'iPhone3,1' => 'iPhone 4',
45
            'iPhone3,2' => 'iPhone 4',
46
            'iPhone3,3' => 'iPhone 4',
47
            'iPhone4,1' => 'iPhone 4S',
48
            'iPhone5,1' => 'iPhone 5',
49
            'iPhone5,2' => 'iPhone 5',
50
            'iPhone5,3' => 'iPhone 5c',
51
            'iPhone5,4' => 'iPhone 5c',
52
            'iPhone6,1' => 'iPhone 5s',
53
            'iPhone6,2' => 'iPhone 5s',
54
            'iPhone7,1' => 'iPhone 6 Plus',
55
            'iPhone7,2' => 'iPhone 6',
56
            'iPhone8,1' => 'iPhone 6s',
57
            'iPhone8,2' => 'iPhone 6s Plus',
58
            'iPhone8,4' => 'iPhone SE',
59
            'iPhone9,1' => 'iPhone 7',
60
            'iPhone9,2' => 'iPhone 7 Plus',
61
            'iPhone9,3' => 'iPhone 7',
62
            'iPhone9,4' => 'iPhone 7 Plus',
63
64
            'iPod1,1' => 'iPod touch',
65
            'iPod2,1' => 'iPod touch 2G',
66
            'iPod3,1' => 'iPod touch 3G',
67
            'iPod4,1' => 'iPod touch 4G',
68
            'iPod5,1' => 'iPod touch 5G',
69
            'iPod7,1' => 'iPod touch 6G',
70
71
            'iPad1,1' => 'iPad',
72
            'iPad2,1' => 'iPad 2',
73
            'iPad2,2' => 'iPad 2',
74
            'iPad2,3' => 'iPad 2',
75
            'iPad2,4' => 'iPad 2',
76
            'iPad2,5' => 'iPad mini',
77
            'iPad2,6' => 'iPad mini',
78
            'iPad2,7' => 'iPad mini',
79
            'iPad3,1' => 'iPad 3',
80
            'iPad3,2' => 'iPad 3',
81
            'iPad3,3' => 'iPad 3',
82
            'iPad3,4' => 'iPad 4',
83
            'iPad3,5' => 'iPad 4',
84
            'iPad3,6' => 'iPad 4',
85
            'iPad4,1' => 'iPad Air',
86
            'iPad4,2' => 'iPad Air',
87
            'iPad4,3' => 'iPad Air',
88
            'iPad4,4' => 'iPad mini 2',
89
            'iPad4,5' => 'iPad mini 2',
90
            'iPad4,6' => 'iPad mini 2',
91
            'iPad4,7' => 'iPad mini 3',
92
            'iPad4,8' => 'iPad mini 3',
93
            'iPad4,9' => 'iPad mini 3',
94
            'iPad5,1' => 'iPad mini 4',
95
            'iPad5,2' => 'iPad mini 4',
96
            'iPad5,3' => 'iPad Air 2',
97
            'iPad5,4' => 'iPad Air 2',
98
            'iPad6,3' => 'iPad Pro',
99
            'iPad6,4' => 'iPad Pro',
100
            'iPad6,7' => 'iPad Pro',
101
            'iPad6,8' => 'iPad Pro',
102
103
            'AppleTV2,1' => 'Apple TV 2G',
104
            'AppleTV3,1' => 'Apple TV 3G',
105
            'AppleTV3,2' => 'Apple TV 3G',
106
            'AppleTV5,3' => 'Apple TV 4G',
107
108
            'Watch1,1' => 'Apple Watch',
109
            'Watch1,2' => 'Apple Watch',
110
            'Watch2,6' => 'Apple Watch Series 1',
111
            'Watch2,7' => 'Apple Watch Series 1',
112
            'Watch2,3' => 'Apple Watch Series 2',
113
            'Watch2,4' => 'Apple Watch Series 2',
114
115
            'i386' => 'Simulator',
116
            'x86_64' => 'Simulator',
117
        ];
118
119
        return isset($list[$platform]) ? $list[$platform] : $platform;
120
    }
121
122
    /**
123
     * Get the mail homepage.
124
     *
125
     * @param  string  $address
126
     * @return string|null
127
     */
128
    public static function mailHomepage($address)
129
    {
130
        if (preg_match('#@([a-z0-9.-]+)#i', $address, $match)) {
131
            $list = [
132
                'qq.com' => 'https://mail.qq.com',
133
                'vip.qq.com' => 'https://mail.qq.com',
134
                '126.com' => 'http://www.126.com',
135
                'gmail.com' => 'https://mail.google.com',
136
                '139.com' => 'http://mail.10086.cn',
137
                'wo.cn' => 'http://mail.wo.com.cn',
138
                'sina.com' => 'http://mail.sina.com.cn',
139
                'sina.cn' => 'http://mail.sina.com.cn',
140
                'vip.sina.com' => 'http://mail.sina.com.cn',
141
            ];
142
143
            $domain = strtolower($match[1]);
144
145
            return isset($list[$domain]) ? $list[$domain] : 'http://mail.'.$domain;
146
        }
147
    }
148
149
    /**
150
     * Get the Gravatar URL.
151
     *
152
     * @param  string  $email
153
     * @param  int  $size
154
     * @param  string  $default
155
     * @param  string  $rating
156
     * @return string
157
     *
158
     * @see http://cn.gravatar.com/site/implement/images/
159
     */
160
    public static function gravatar($email, $size = 100, $default = null, $rating = null)
161
    {
162
        if (is_null($default)) {
163
            $default = config('services.gravatar.default');
164
        }
165
        if (is_null($rating)) {
166
            $rating = config('services.gravatar.rating');
167
        }
168
169
        $query = http_build_query(compact('size', 'default', 'rating'));
170
171
        return app('url')->assetFrom(
172
            config('services.gravatar.host', 'http://gravatar.com/avatar'),
173
            md5(strtolower(trim($email))).'?'.$query
174
        );
175
    }
176
177
    /**
178
     * Encrypt ASCII string via XOR.
179
     *
180
     * @param  string  $text
181
     * @param  string|null  $key
182
     * @return string
183
     */
184
    public static function sampleEncrypt($text, $key = null)
185
    {
186
        $text = (string) $text;
187
        if (is_null($key)) {
188
            $key = app('encrypter')->getKey();
189
        }
190
191
        // 生成随机字符串
192
        $random = str_random(strlen($text));
193
194
        // 按字符拼接:随机字符串 + 随机字符串异或原文
195
        $tmp = static::sampleEncryption($text, $random, function ($a, $b) {
196
            return $b.($a ^ $b);
197
        });
198
199
        // 异或 $tmp 和 $key
200
        $result = static::sampleEncryption($tmp, $key);
201
202
        return urlsafe_base64_encode($result);
203
    }
204
205
    /**
206
     * Decrypt string via XOR.
207
     *
208
     * @param  string  $text
209
     * @param  string|null  $key
210
     * @return string
211
     */
212
    public static function sampleDecrypt($text, $key = null)
213
    {
214
        if (is_null($key)) {
215
            $key = app('encrypter')->getKey();
216
        }
217
218
        $tmp = static::sampleEncryption(urlsafe_base64_decode($text), $key);
219
        $tmpLength = strlen($tmp);
220
        $result = '';
221
        for ($i = 0; $i < $tmpLength, ($i + 1) < $tmpLength; $i += 2) {
222
            $result .= $tmp[$i] ^ $tmp[$i + 1];
223
        }
224
225
        return $result;
226
    }
227
228
    /**
229
     * Do a sample XOR encryption.
230
     *
231
     * @param  string  $text
232
     * @param  string  $key
233
     * @param  \Closure|null  $callback `($a, $b, $index)`
234
     * @return string
235
     */
236
    protected static function sampleEncryption($text, $key, $callback = null)
237
    {
238
        // 对 $text 和 $key 的每个字符进行运算。
239
        // $callback 为 null 时默认进行异或运算。
240
        // $callback 参数: 第 i 位 $text, 第 i 位 $key, 下标 i
241
        // $callback 返回 false 时,结束字符循环.
242
243
        $text = (string) $text;
244
        $key = (string) $key;
245
        $keyLength = strlen($key);
246
        if (is_null($callback)) {
247
            $callback = function ($a, $b) {
248
                return $a ^ $b;
249
            };
250
        }
251
252
        $result = '';
253
        $textLength = strlen($text);
254
        for ($i = 0; $i < $textLength; $i++) {
255
            $tmp = $callback($text[$i], $key[$i % $keyLength], $i);
256
            if (false === $tmp) {
257
                break;
258
            }
259
            $result .= $tmp;
260
        }
261
262
        return $result;
263
    }
264
265
    /**
266
     * Convert an integer to a string.
267
     * It is useful to shorten a database ID to URL keyword, e.g. 54329 to 'xkE'.
268
     *
269
     * @param  int $number
270
     * @param  string|null  $characters
271
     * @return string
272
     */
273
    public static function int2string($number, string $characters = null)
274
    {
275
        $number = (int) $number;
276
        if ($number < 0) {
277
            return '';
278
        }
279
280
        if (is_null($characters)) {
281
            $characters = config('support.int2string');
282
        }
283
284
        $charactersLength = strlen($characters);
285
        $string = '';
286
287
        while ($number >= $charactersLength) {
288
            $mod = (int) bcmod($number, $charactersLength);
289
            $number = (int) bcdiv($number, $charactersLength);
290
            $string = $characters[$mod].$string;
291
        }
292
293
        return $characters[$number].$string;
294
    }
295
296
    /**
297
     * Convert an `int2string` generated string back to an integer.
298
     *
299
     * @param  string  $string
300
     * @param  string|null  $characters
301
     * @return int
302
     */
303
    public static function string2int($string, string $characters = null)
304
    {
305
        $string = strrev($string);
306
        $stringLength = strlen($string);
307
308
        if (is_null($characters)) {
309
            $characters = config('support.int2string');
310
        }
311
        $charactersLength = strlen($characters);
312
313
        $number = 0;
314
        for ($i = 0; $i < $stringLength; $i++) {
315
            $index = strpos($characters, $string[$i]);
316
            $number = (int) bcadd($number, bcmul($index, bcpow($charactersLength, $i)));
317
        }
318
319
        return $number;
320
    }
321
}
322