Passed
Push — master ( 83e3bb...9868a0 )
by Tom
02:59
created

KeepOptions::hasErrorKeep()   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 0
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
    /**
34
     * @var Args
35
     */
36
    private $args;
37
38
    /**
39
     * @param Args $args
40
     */
41 4
    public function __construct(Args $args)
42
    {
43 4
        $this->args = $args;
44 4
    }
45
46
    /**
47
     * @param Args $args
48
     * @param Streams $streams
49
     * @return KeepOptions
50
     */
51 1
    public static function bind(Args $args)
52
    {
53 1
        return new self($args);
54
    }
55
56
    /**
57
     * @throws StatusException w/ conflicting arguments
58
     * @return KeepOptions
59
     */
60 1
    public function run()
61
    {
62 1
        list($this->errorKeep, $this->keep, $this->noKeep)
63 1
            = $this->parse($this->args);
64
65 1
        return $this;
66
    }
67
68
    /**
69
     * Parse keep arguments
70
     *
71
     * @param Args $args
72
     * @throws \InvalidArgumentException
73
     * @throws StatusException
74
     * @return array|int
75
     */
76 4
    public function parse(Args $args)
77
    {
78
        /** @var bool $errorKeep keep on errors */
79 4
        $errorKeep = $args->hasOption('error-keep');
80
81
        /** @var bool $keep containers */
82 4
        $keep = $args->hasOption('keep');
83
84
        /** @var bool $noKeep do not keep on errors */
85 4
        $noKeep = $args->hasOption('no-keep');
86
87 4
        if ($keep && $noKeep) {
88 1
            StatusException::status(1, '--keep and --no-keep are exclusive');
89
        }
90
91 3
        if ($keep && $errorKeep) {
92 1
            StatusException::status(1, '--keep and --error-keep are exclusive');
93
        }
94
95 2
        if ($noKeep && $errorKeep) {
96 1
            StatusException::status(1, '--error-keep and --no-keep are exclusive');
97
        }
98
99 1
        return array($errorKeep, $keep, $noKeep);
100
    }
101
102
    /**
103
     * @return bool
104
     */
105 1
    public function hasErrorKeep()
106
    {
107 1
        return true === $this->errorKeep;
108
    }
109
}
110