Console   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 59
c 0
b 0
f 0
wmc 15
lcom 1
cbo 0
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A read() 0 4 1
A write() 0 3 1
A error() 0 5 1
A writeLine() 0 3 1
A getArguments() 0 3 1
A getOption() 0 7 2
B optionPosition() 0 11 5
A hasOption() 0 3 1
A getScriptName() 0 3 1
1
<?php
2
namespace rtens\domin\delivery\cli;
3
4
class Console {
5
6
    private $argv;
7
8
    public function __construct(array $argv) {
9
        $this->argv = $argv;
10
    }
11
12
    public function read($prompt = '') {
13
        $this->write($prompt);
14
        return trim(fgets(STDIN));
15
    }
16
17
    public function write($string) {
18
        echo $string;
19
    }
20
21
    public function error($message) {
22
        $stderr = fopen('php://stderr', 'w');
23
        fwrite($stderr, $message);
24
        fclose($stderr);
25
    }
26
27
    public function writeLine($string = '') {
28
        $this->write($string . PHP_EOL);
29
    }
30
31
    public function getArguments() {
32
        return array_values(array_slice($this->argv, 1));
33
    }
34
35
    public function getOption($name, $default = null) {
36
        if (!$this->hasOption($name)) {
37
            return $default;
38
        }
39
40
        return $this->getArguments()[$this->optionPosition($name)];
41
    }
42
43
    private function optionPosition($name) {
44
        $arguments = $this->getArguments();
45
46
        foreach ($arguments as $i => $arg) {
47
            if (substr($arg, 0, 1) == '-' && ltrim($arg, '-') == $name && array_key_exists($i + 1, $arguments)) {
48
                return $i + 1;
49
            }
50
        }
51
52
        return -1;
53
    }
54
55
    public function hasOption($name) {
56
        return $this->optionPosition($name) > -1;
57
    }
58
59
    public function getScriptName() {
60
        return $this->argv[0];
61
    }
62
}