Completed
Push — master ( 5efdd3...bfd2db )
by Greg
03:21
created

ResultData::alreadyPrinted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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