KeepOptions   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 86
ccs 20
cts 20
cp 1
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 3 1
A __construct() 0 3 1
A hasErrorKeep() 0 3 1
B parse() 0 21 8
A run() 0 5 1
1
<?php
2
3
/* this file is part of pipelines */
4
5
namespace Ktomk\Pipelines\Utility;
6
7
use Ktomk\Pipelines\Cli\Args;
8
9
/**
10
 * aggregated args parser for --error-keep, --keep and --no-keep argument
11
 * parsing.
12
 *
13
 * @package Ktomk\Pipelines\Utility\Args
14
 */
15
class KeepOptions
16
{
17
    /**
18
     * @var null|bool
19
     */
20
    public $errorKeep;
21
22
    /**
23
     * @var null|bool
24
     */
25
    public $keep;
26
27
    /**
28
     * @var Args
29
     */
30
    private $args;
31
32
    /**
33
     * @param Args $args
34
     *
35
     * @return KeepOptions
36
     */
37 1
    public static function bind(Args $args)
38
    {
39 1
        return new self($args);
40
    }
41
42
    /**
43
     * @param Args $args
44
     */
45 8
    public function __construct(Args $args)
46
    {
47 8
        $this->args = $args;
48
    }
49
50
    /**
51
     * @throws StatusException w/ conflicting arguments
52
     *
53
     * @return KeepOptions
54
     */
55 1
    public function run()
56
    {
57 1
        list($this->errorKeep, $this->keep) = $this->parse($this->args);
58
59 1
        return $this;
60
    }
61
62
    /**
63
     * Parse keep arguments
64
     *
65
     * @param Args $args
66
     *
67
     * @throws \InvalidArgumentException
68
     * @throws StatusException
69
     *
70
     * @return array
71
     */
72 8
    public function parse(Args $args)
73
    {
74 8
        $errorKeep = $args->hasOption('error-keep');
75
76 8
        $keep = $args->hasOption('keep');
77
78 8
        $noKeep = $args->hasOption('no-keep');
79
80 8
        if ($keep && $noKeep) {
81 1
            throw new StatusException('--keep and --no-keep are exclusive', 1);
82
        }
83
84 7
        if ($keep && $errorKeep) {
85 1
            throw new StatusException('--keep and --error-keep are exclusive', 1);
86
        }
87
88 6
        if ($noKeep && $errorKeep) {
89 1
            throw new StatusException('--error-keep and --no-keep are exclusive', 1);
90
        }
91
92 5
        return array($errorKeep, $keep && !$noKeep);
93
    }
94
95
    /**
96
     * @return bool
97
     */
98 1
    public function hasErrorKeep()
99
    {
100 1
        return true === $this->errorKeep;
101
    }
102
}
103