Completed
Push — master ( bfc0cc...f0e226 )
by sabaku
04:05
created

Util   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 12
lcom 0
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A trim() 0 7 2
A getHeaderParams() 0 10 3
A parseDir() 0 15 3
A base64Json() 0 3 1
A stringifyHeaders() 0 7 2
A md5Hash() 0 7 1
1
<?php
2
namespace Upyun;
3
class Util {
4
5
    public static function trim($str) {
6
        if(is_array($str)) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
7
            return array_map(array('Util', 'trim'), $str);
8
        } else {
9
            return trim($str);
10
        }
11
    }
12
13
    public static function getHeaderParams($headers) {
14
        $params = [];
15
        foreach ($headers as $header => $value) {
16
            $header = strtolower($header);
17
            if(strpos($header, 'x-upyun-') !== false) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
18
                $params[$header] = $value[0];
19
            }
20
        }
21
        return $params;
22
    }
23
    
24
    public static function parseDir($body) {
25
        $files = array();
26
        if(!$body) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after IF keyword; 0 found
Loading history...
27
            return array('files' => $files, 'is_end' => true);
28
        }
29
30
        $lines = explode("\n", $body);
31
        foreach($lines as $line) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
32
            $file = [];
33
            list($file['name'], $file['type'], $file['size'], $file['time']) = explode("\t", $line, 4);
34
            $files[] = $file;
35
        }
36
37
        return $files;
38
    }
39
40
    public static function base64Json($params) {
41
        return base64_encode(json_encode($params));
42
    }
43
44
    public static function stringifyHeaders($headers) {
45
        $return = array();
46
        foreach ($headers as $key => $value) {
47
            $return[] = "$key: $value";
48
        }
49
        return $return;
50
    }
51
    
52
    public static function md5Hash($resource) {
53
        rewind($resource);
54
        $ctx = hash_init('md5');
55
        hash_update_stream($ctx, $resource);
56
        $md5 = hash_final($ctx);
57
        return $md5;
58
    }
59
}