ResultData::getOutputData()   A
last analyzed

Complexity

Conditions 4
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 4
nc 2
nop 0
1
<?php
2
3
namespace Robo;
4
5
use Consolidation\AnnotatedCommand\ExitCodeInterface;
6
use Consolidation\AnnotatedCommand\OutputDataInterface;
7
use Robo\State\Data;
8
9
class ResultData extends Data implements ExitCodeInterface, OutputDataInterface
10
{
11
    /**
12
     * @var int
13
     */
14
    protected $exitCode;
15
16
    const EXITCODE_OK = 0;
17
    const EXITCODE_ERROR = 1;
18
    /** Symfony Console handles these conditions; Robo returns the status
19
    code selected by Symfony. These are here for documentation purposes. */
20
    const EXITCODE_MISSING_OPTIONS = 2;
21
    const EXITCODE_COMMAND_NOT_FOUND = 127;
22
23
    /** The command was aborted because the user chose to cancel it at some prompt.
24
    This exit code is arbitrarily the same as EX_TEMPFAIL in sysexits.h, although
25
    note that shell error codes are distinct from C exit codes, so this alignment
26
    not particularly meaningful. */
27
    const EXITCODE_USER_CANCEL = 75;
28
29
    /**
30
     * @param int $exitCode
31
     * @param string $message
32
     * @param array $data
33
     */
34
    public function __construct($exitCode = self::EXITCODE_OK, $message = '', $data = [])
35
    {
36
        $this->exitCode = $exitCode;
37
        parent::__construct($message, $data);
38
    }
39
40
    /**
41
     * @param string $message
42
     * @param array $data
43
     *
44
     * @return static
45
     */
46
    public static function message($message, $data = [])
47
    {
48
        return new self(self::EXITCODE_OK, $message, $data);
49
    }
50
51
    /**
52
     * @param string $message
53
     * @param array $data
54
     *
55
     * @return static
56
     */
57
    public static function cancelled($message = '', $data = [])
58
    {
59
        return new ResultData(self::EXITCODE_USER_CANCEL, $message, $data);
60
    }
61
62
    /**
63
     * @return int
64
     */
65
    public function getExitCode()
66
    {
67
        return $this->exitCode;
68
    }
69
70
    /**
71
     * @return null|string
72
     */
73
    public function getOutputData()
74
    {
75
        if (!empty($this->message) && !isset($this['already-printed']) && isset($this['provide-outputdata'])) {
76
            return $this->message;
77
        }
78
    }
79
80
    /**
81
     * Indicate that the message in this data has already been displayed.
82
     */
83
    public function alreadyPrinted()
84
    {
85
        $this['already-printed'] = true;
86
    }
87
88
    /**
89
     * Opt-in to providing the result message as the output data
90
     */
91
    public function provideOutputdata()
92
    {
93
        $this['provide-outputdata'] = true;
94
    }
95
96
    /**
97
     * @return bool
98
     */
99
    public function wasSuccessful()
100
    {
101
        return $this->exitCode === self::EXITCODE_OK;
102
    }
103
104
    /**
105
     * @return bool
106
     */
107
    public function wasCancelled()
108
    {
109
        return $this->exitCode == self::EXITCODE_USER_CANCEL;
110
    }
111
}
112