Completed
Push — master ( 15cb6d...86e5be )
by Nikolas
03:31
created

Console   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 12
c 5
b 1
f 1
lcom 1
cbo 0
dl 0
loc 46
rs 10

8 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
B getOption() 0 10 5
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) {
36
        $arguments = $this->getArguments();
37
38
        foreach ($arguments as $i => $arg) {
39
            if (substr($arg, 0, 1) == '-' && ltrim($arg, '-') == $name && array_key_exists($i + 1, $arguments)) {
40
                return $arguments[$i + 1];
41
            }
42
        }
43
        throw new \InvalidArgumentException("No option [$name] given");
44
    }
45
46
    public function getScriptName() {
47
        return $this->argv[0];
48
    }
49
}