Completed
Push — master ( f25de2...487eea )
by Markus
03:35
created

Utils   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 95.56%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 16
c 1
b 1
f 0
lcom 0
cbo 0
dl 0
loc 102
ccs 43
cts 45
cp 0.9556
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isWindows() 0 4 1
A isCli() 0 4 1
B parseSourceId() 0 22 5
A whoAmI() 0 11 2
B numbersToRangeText() 0 25 5
A addRangeText() 0 11 2
1
<?php
2
namespace Mathielen\ImportEngineBundle;
3
4
class Utils
5
{
6
7
    /**
8
     * @return array
9
     */
10 4
    public static function parseSourceId($sourceId)
11
    {
12 4
        if (is_file($sourceId)) {
13
            return $sourceId;
14 4
        } elseif (preg_match('/^[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+/', $sourceId)) {
15 1
            $parsedSourceId = parse_url($sourceId);
16 1
            if (isset($parsedSourceId['query'])) {
17 1
                parse_str($parsedSourceId['query'], $parsedSourceId['query']);
18
            }
19 1
            $pathTokens = explode('.', $parsedSourceId['path']);
20 1
            $method = array_pop($pathTokens);
21 1
            $service = join('.', $pathTokens);
22
23
            return array(
24 1
                'service' => $service,
25 1
                'method' => $method,
26 1
                'arguments' => isset($parsedSourceId['query'])?$parsedSourceId['query']:null
27
            );
28
        }
29
30 3
        return $sourceId;
31
    }
32
33
    /**
34
     * @return bool
35
     */
36 5
    public static function isWindows()
37
    {
38 5
        return (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
39
    }
40
41
    /**
42
     * @return bool
43
     */
44 1
    public static function isCli()
45
    {
46 1
        return php_sapi_name() == "cli";
47
    }
48
49
    /**
50
     * @return string
51
     */
52 5
    public static function whoAmI()
53
    {
54 5
        if (self::isWindows()) {
55
            $user = getenv("username");
56
        } else {
57 5
            $processUser = posix_getpwuid(posix_geteuid());
58 5
            $user = $processUser['name'];
59
        }
60
61 5
        return $user;
62
    }
63
64
    /**
65
     * @return array
66
     */
67 5
    public static function numbersToRangeText(array $numbers)
68
    {
69 5
        if (empty($numbers)) {
70 1
            return [];
71
        }
72
73 4
        $ranges = [];
74 4
        sort($numbers);
75
76 4
        $currentRange = [];
77 4
        foreach ($numbers as $number) {
78 4
            if (!empty($currentRange) && current($currentRange) !== $number-1) {
79 2
                self::addRangeText($ranges, $currentRange);
80
81 2
                $currentRange = [];
82
            }
83
84 4
            $currentRange[] = $number;
85 4
            end($currentRange);
86
        }
87
88 4
        self::addRangeText($ranges, $currentRange);
89
90 4
        return $ranges;
91
    }
92
93 4
    private static function addRangeText(array &$ranges, array $currentRange)
94
    {
95 4
        $lastItem = current($currentRange);
96
97 4
        if (count($currentRange) === 1) {
98 2
            $ranges[] = $lastItem;
99
        } else {
100 2
            $firstItem = reset($currentRange);
101 2
            $ranges[] = $firstItem . '-' . $lastItem;
102
        }
103 4
    }
104
105
}
106