Completed
Push — master ( bd5ff6...90c2c7 )
by Bernhard
07:04
created

ResolveResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\Resolver;
13
14
use Webmozart\Console\Api\Args\Args;
15
use Webmozart\Console\Api\Args\CannotParseArgsException;
16
use Webmozart\Console\Api\Args\RawArgs;
17
use Webmozart\Console\Api\Command\Command;
18
19
/**
20
 * An intermediate result created during resolving.
21
 *
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
class ResolveResult
27
{
28
    /**
29
     * @var Command
30
     */
31
    private $command;
32
33
    /**
34
     * @var RawArgs
35
     */
36
    private $rawArgs;
37
38
    /**
39
     * @var Args
40
     */
41
    private $parsedArgs;
42
43
    /**
44
     * @var CannotParseArgsException
45
     */
46
    private $parseError;
47
48
    /**
49
     * @var bool
50
     */
51
    private $parsed = false;
52
53
    /**
54
     * Creates a new resolved command.
55
     *
56
     * @param Command $command The command.
57
     * @param RawArgs $rawArgs The raw console arguments.
58
     */
59 216
    public function __construct(Command $command, RawArgs $rawArgs)
60
    {
61 216
        $this->command = $command;
62 216
        $this->rawArgs = $rawArgs;
63 216
    }
64
65
    /**
66
     * Returns the command.
67
     *
68
     * @return Command The command.
69
     */
70 215
    public function getCommand()
71
    {
72 215
        return $this->command;
73
    }
74
75
    /**
76
     * The raw console arguments.
77
     *
78
     * @return RawArgs The raw console arguments.
79
     */
80
    public function getRawArgs()
81
    {
82
        return $this->rawArgs;
83
    }
84
85
    /**
86
     * Returns the parsed console arguments.
87
     *
88
     * @return Args The parsed console arguments or `null` if the console
89
     *              arguments cannot be parsed.
90
     *
91
     * @see isParsable(), getParseError()
92
     */
93 215
    public function getParsedArgs()
94
    {
95 215
        if (!$this->parsed) {
96
            $this->parse();
97
        }
98
99 215
        return $this->parsedArgs;
100
    }
101
102
    /**
103
     * Returns the error that happened during argument parsing.
104
     *
105
     * @return CannotParseArgsException The parse error or `null` if the
106
     *                                  arguments were parsed successfully.
107
     *
108
     * @see isParsable(), getParsedArgs()
109
     */
110 1
    public function getParseError()
111
    {
112 1
        if (!$this->parsed) {
113
            $this->parse();
114
        }
115
116 1
        return $this->parseError;
117
    }
118
119
    /**
120
     * Returns whether the console arguments can be parsed.
121
     *
122
     * @return bool Returns `true` if the console arguments can be parsed and
123
     *              `false` if a parse error occurred.
124
     *
125
     * @see getParsedArgs(), getParseError()
126
     */
127 216
    public function isParsable()
128
    {
129 216
        if (!$this->parsed) {
130 216
            $this->parse();
131
        }
132
133 216
        return null === $this->parseError;
134
    }
135
136 216
    private function parse()
137
    {
138
        try {
139 216
            $this->parsedArgs = $this->command->parseArgs($this->rawArgs);
140 7
        } catch (CannotParseArgsException $e) {
141 7
            $this->parseError = $e;
142
        }
143
144 216
        $this->parsed = true;
145 216
    }
146
}
147