Completed
Push — master ( d5c555...a6749e )
by Elf
02:42
created

Helper::iDeviceModel()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 87
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 76
nc 2
nop 1
dl 0
loc 87
ccs 0
cts 80
cp 0
crap 6
rs 8.6296
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 ElfSundae\Laravel\Support;
4
5
use Illuminate\Support\Str;
6
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
7
8
class Helper
9
{
10
    /**
11
     * Get file extension for MIME type.
12
     *
13
     * @param  string  $mimeType
14
     * @param  string  $prefix
15
     * @return string|null
16
     */
17
    public static function fileExtensionForMimeType($mimeType, $prefix = '')
18
    {
19
        if ($extension = ExtensionGuesser::getInstance()->guess($mimeType)) {
20
            if ($extension === 'jpeg') {
21
                $extension = 'jpg';
22
            }
23
24
            return $prefix.$extension;
25
        }
26
    }
27
28
    /**
29
     * Convert an iOS platform to the device model name.
30
     *
31
     * @see https://www.theiphonewiki.com/wiki/Models
32
     * @see https://support.hockeyapp.net/kb/client-integration-ios-mac-os-x-tvos/ios-device-types
33
     *
34
     * @param  string  $platform
35
     * @return string
36
     */
37
    public static function iDeviceModel($platform)
38
    {
39
        static $iDeviceModels = null;
40
41
        if (is_null($iDeviceModels)) {
42
            $iDeviceModels = [
43
                'i386' => 'Simulator',
44
                'x86_64' => 'Simulator',
45
46
                'iPhone1,1' => 'iPhone',
47
                'iPhone1,2' => 'iPhone 3G',
48
                'iPhone2,1' => 'iPhone 3GS',
49
                'iPhone3,1' => 'iPhone 4',
50
                'iPhone3,2' => 'iPhone 4',
51
                'iPhone3,3' => 'iPhone 4',
52
                'iPhone4,1' => 'iPhone 4S',
53
                'iPhone5,1' => 'iPhone 5',
54
                'iPhone5,2' => 'iPhone 5',
55
                'iPhone5,3' => 'iPhone 5c',
56
                'iPhone5,4' => 'iPhone 5c',
57
                'iPhone6,1' => 'iPhone 5s',
58
                'iPhone6,2' => 'iPhone 5s',
59
                'iPhone7,1' => 'iPhone 6 Plus',
60
                'iPhone7,2' => 'iPhone 6',
61
                'iPhone8,1' => 'iPhone 6s',
62
                'iPhone8,2' => 'iPhone 6s Plus',
63
                'iPhone8,4' => 'iPhone SE',
64
                'iPhone9,1' => 'iPhone 7',
65
                'iPhone9,2' => 'iPhone 7 Plus',
66
                'iPhone9,3' => 'iPhone 7',
67
                'iPhone9,4' => 'iPhone 7 Plus',
68
69
                'iPod1,1' => 'iPod touch',
70
                'iPod2,1' => 'iPod touch 2G',
71
                'iPod3,1' => 'iPod touch 3G',
72
                'iPod4,1' => 'iPod touch 4G',
73
                'iPod5,1' => 'iPod touch 5G',
74
                'iPod7,1' => 'iPod touch 6G',
75
76
                'iPad1,1' => 'iPad',
77
                'iPad2,1' => 'iPad 2',
78
                'iPad2,2' => 'iPad 2',
79
                'iPad2,3' => 'iPad 2',
80
                'iPad2,4' => 'iPad 2',
81
                'iPad2,5' => 'iPad mini',
82
                'iPad2,6' => 'iPad mini',
83
                'iPad2,7' => 'iPad mini',
84
                'iPad3,1' => 'iPad 3',
85
                'iPad3,2' => 'iPad 3',
86
                'iPad3,3' => 'iPad 3',
87
                'iPad3,4' => 'iPad 4',
88
                'iPad3,5' => 'iPad 4',
89
                'iPad3,6' => 'iPad 4',
90
                'iPad4,1' => 'iPad Air',
91
                'iPad4,2' => 'iPad Air',
92
                'iPad4,3' => 'iPad Air',
93
                'iPad4,4' => 'iPad mini 2',
94
                'iPad4,5' => 'iPad mini 2',
95
                'iPad4,6' => 'iPad mini 2',
96
                'iPad4,7' => 'iPad mini 3',
97
                'iPad4,8' => 'iPad mini 3',
98
                'iPad4,9' => 'iPad mini 3',
99
                'iPad5,1' => 'iPad mini 4',
100
                'iPad5,2' => 'iPad mini 4',
101
                'iPad5,3' => 'iPad Air 2',
102
                'iPad5,4' => 'iPad Air 2',
103
                'iPad6,3' => 'iPad Pro',
104
                'iPad6,4' => 'iPad Pro',
105
                'iPad6,7' => 'iPad Pro',
106
                'iPad6,8' => 'iPad Pro',
107
108
                'AppleTV2,1' => 'Apple TV 2G',
109
                'AppleTV3,1' => 'Apple TV 3G',
110
                'AppleTV3,2' => 'Apple TV 3G',
111
                'AppleTV5,3' => 'Apple TV 4G',
112
113
                'Watch1,1' => 'Apple Watch',
114
                'Watch1,2' => 'Apple Watch',
115
                'Watch2,6' => 'Apple Watch Series 1',
116
                'Watch2,7' => 'Apple Watch Series 1',
117
                'Watch2,3' => 'Apple Watch Series 2',
118
                'Watch2,4' => 'Apple Watch Series 2',
119
            ];
120
        }
121
122
        return $iDeviceModels[$platform] ?? $platform;
123
    }
124
125
    /**
126
     * Get the mail homepage.
127
     *
128
     * @param  string  $address
129
     * @return string|null
130
     */
131
    public static function mailHomepage($address)
132
    {
133
        static $mailHomepages = null;
134
135
        if (filter_var($address, FILTER_VALIDATE_EMAIL)) {
136
            if (is_null($mailHomepages)) {
137
                $mailHomepages = [
138
                    'gmail.com' => 'https://mail.google.com',
139
                    'qq.com' => 'https://mail.qq.com',
140
                    'vip.qq.com' => 'https://mail.qq.com',
141
                    '163.com' => 'http://mail.163.com',
142
                    '126.com' => 'http://www.126.com',
143
                    'yeah.net' => 'http://www.yeah.net',
144
                    'sina.com' => 'https://mail.sina.com.cn',
145
                    'sina.cn' => 'https://mail.sina.com.cn',
146
                    'vip.sina.com' => 'https://mail.sina.com.cn',
147
                    'sohu.com' => 'https://mail.sohu.com',
148
                    'vip.sohu.com' => 'https://vip.sohu.com',
149
                    'aliyun.com' => 'https://mail.aliyun.com',
150
                    'tom.com' => 'http://mail.tom.com',
151
                    '139.com' => 'http://mail.10086.cn',
152
                    'wo.cn' => 'https://mail.wo.cn',
153
                    '189.cn' => 'https://mail.189.cn',
154
                ];
155
            }
156
157
            $domain = strtolower(Str::after($address, '@'));
158
159
            return $mailHomepages[$domain] ?? 'http://'.$domain;
160
        }
161
    }
162
163
    /**
164
     * Encrypt ASCII string via XOR.
165
     *
166
     * @param  string  $text
167
     * @param  string|null  $key
168
     * @return string
169
     */
170
    public static function sampleEncrypt($text, $key = null)
171
    {
172
        $text = (string) $text;
173
        if (is_null($key)) {
174
            $key = app('encrypter')->getKey();
175
        }
176
177
        // 生成随机字符串
178
        $random = str_random(strlen($text));
179
180
        // 按字符拼接:随机字符串 + 随机字符串异或原文
181
        $tmp = static::sampleEncryption($text, $random, function ($a, $b) {
182
            return $b.($a ^ $b);
183
        });
184
185
        // 异或 $tmp 和 $key
186
        $result = static::sampleEncryption($tmp, $key);
187
188
        return urlsafe_base64_encode($result);
189
    }
190
191
    /**
192
     * Decrypt string via XOR.
193
     *
194
     * @param  string  $text
195
     * @param  string|null  $key
196
     * @return string
197
     */
198
    public static function sampleDecrypt($text, $key = null)
199
    {
200
        if (is_null($key)) {
201
            $key = app('encrypter')->getKey();
202
        }
203
204
        $tmp = static::sampleEncryption(urlsafe_base64_decode($text), $key);
205
        $tmpLength = strlen($tmp);
206
        $result = '';
207
        for ($i = 0; $i < $tmpLength, ($i + 1) < $tmpLength; $i += 2) {
208
            $result .= $tmp[$i] ^ $tmp[$i + 1];
209
        }
210
211
        return $result;
212
    }
213
214
    /**
215
     * Do a sample XOR encryption.
216
     *
217
     * @param  string  $text
218
     * @param  string  $key
219
     * @param  \Closure|null  $callback `($a, $b, $index)`
220
     * @return string
221
     */
222
    protected static function sampleEncryption($text, $key, $callback = null)
223
    {
224
        // 对 $text 和 $key 的每个字符进行运算。
225
        // $callback 为 null 时默认进行异或运算。
226
        // $callback 参数: 第 i 位 $text, 第 i 位 $key, 下标 i
227
        // $callback 返回 false 时,结束字符循环.
228
229
        $text = (string) $text;
230
        $key = (string) $key;
231
        $keyLength = strlen($key);
232
        if (is_null($callback)) {
233
            $callback = function ($a, $b) {
234
                return $a ^ $b;
235
            };
236
        }
237
238
        $result = '';
239
        $textLength = strlen($text);
240
        for ($i = 0; $i < $textLength; $i++) {
241
            $tmp = $callback($text[$i], $key[$i % $keyLength], $i);
242
            if (false === $tmp) {
243
                break;
244
            }
245
            $result .= $tmp;
246
        }
247
248
        return $result;
249
    }
250
}
251