Passed
Push — master ( 94154d...6015fa )
by Tom
02:39
created

KeepOptions::bind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
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
     * @throws \InvalidArgumentException
64
     * @return null|int non-zero, positive integer in case of error
65
     *                  parsing keep option arguments
66
     */
67 1
    public function run()
68
    {
69 1
        list($status, $this->errorKeep, $this->keep, $this->noKeep)
70 1
            = $this->parse($this->args) + array(null, null, null, null);
71
72 1
        return $status;
73
    }
74
75
    /**
76
     * Parse keep arguments
77
     *
78
     * @param Args $args
79
     * @throws \InvalidArgumentException
80
     * @return array|int
81
     */
82 4
    public function parse(Args $args)
83
    {
84
        /** @var bool $errorKeep keep on errors */
85 4
        $errorKeep = $args->hasOption('error-keep');
86
87
        /** @var bool $keep containers */
88 4
        $keep = $args->hasOption('keep');
89
90
        /** @var bool $noKeep do not keep on errors */
91 4
        $noKeep = $args->hasOption('no-keep');
92
93 4
        if ($keep && $noKeep) {
94 1
            $this->error('pipelines: --keep and --no-keep are exclusive');
95
96 1
            return array(1);
97
        }
98 3
        if ($keep && $errorKeep) {
99 1
            $this->error('pipelines: --keep and --error-keep are exclusive');
100
101 1
            return array(1);
102
        }
103 2
        if ($noKeep && $errorKeep) {
104 1
            $this->error('pipelines: --error-keep and --no-keep are exclusive');
105
106 1
            return array(1);
107
        }
108
109 1
        return array(null, $errorKeep, $keep, $noKeep);
110
    }
111
112 3
    private function error($message)
113
    {
114 3
        $this->streams->err(
115 3
            sprintf("%s\n", $message)
116
        );
117 3
    }
118
}
119