Passed
Push — test ( 94154d...f6861c )
by Tom
02:30
created

KeepOptions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use Ktomk\Pipelines\Cli\Args;
8
use Ktomk\Pipelines\Cli\Streams;
9
10
/**
11
 * aggregated args parser for --error-keep, --keep and --no-keep argument
12
 * parsing.
13
 *
14
 * @package Ktomk\Pipelines\Utility\Args
15
 */
16
class KeepOptions
17
{
18
    /**
19
     * @var null|bool
20
     */
21
    public $errorKeep;
22
23
    /**
24
     * @var null|bool
25
     */
26
    public $keep;
27
28
    /**
29
     * @var null|bool
30
     */
31
    public $noKeep;
32
    /**
33
     * @var Streams
34
     */
35
    private $streams;
36
37
    /**
38
     * @var Args
39
     */
40
    private $args;
41
42
    /**
43
     * @param Args $args
44
     * @param Streams $streams
45
     */
46 1
    public function __construct(Args $args, Streams $streams)
47
    {
48 1
        $this->streams = $streams;
49 1
        $this->args = $args;
50 1
    }
51
52
    /**
53
     * @param Args $args
54
     * @param Streams $streams
55
     * @return KeepOptions
56
     */
57 1
    public static function bind(Args $args, Streams $streams)
58
    {
59 1
        return new self($args, $streams);
60
    }
61
62
    /**
63
     * @return null|int non-zero, positive integer in case of error
64
     *                  parsing keep option arguments
65
     */
66 1
    public function run()
67
    {
68 1
        list($status, $this->errorKeep, $this->keep, $this->noKeep)
69 1
            = $this->parse($this->args) + array(null, null, null, null);
70
71 1
        return $status;
72
    }
73
74
    /**
75
     * Parse keep arguments
76
     *
77
     * @param Args $args
78
     * @throws \InvalidArgumentException
79
     * @return array|int
80
     */
81 4
    public function parse(Args $args)
82
    {
83
        /** @var bool $errorKeep keep on errors */
84 4
        $errorKeep = $args->hasOption('error-keep');
85
86
        /** @var bool $keep containers */
87 4
        $keep = $args->hasOption('keep');
88
89
        /** @var bool $noKeep do not keep on errors */
90 4
        $noKeep = $args->hasOption('no-keep');
91
92 4
        if ($keep && $noKeep) {
93 1
            $this->error('pipelines: --keep and --no-keep are exclusive');
94
95 1
            return array(1);
96
        }
97 3
        if ($keep && $errorKeep) {
98 1
            $this->error('pipelines: --keep and --error-keep are exclusive');
99
100 1
            return array(1);
101
        }
102 2
        if ($noKeep && $errorKeep) {
103 1
            $this->error('pipelines: --error-keep and --no-keep are exclusive');
104
105 1
            return array(1);
106
        }
107
108 1
        return array(null, $errorKeep, $keep, $noKeep);
109
    }
110
111 3
    private function error($message)
112
    {
113 3
        $this->streams->err(
114 3
            sprintf("%s\n", $message)
115
        );
116 3
    }
117
}
118