CommandResult::dataWithExitCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
namespace Consolidation\AnnotatedCommand;
3
4
/**
5
 * Return a CommandResult as the result of a command to pass both an exit
6
 * code and result data from a command.
7
 *
8
 * Usage:
9
 *
10
 *      return CommandResult::dataWithExitCode(new RowsOfFields($rows), 1);
11
 *
12
 * The CommandResult can also be used to unambiguously return just
13
 * an exit code or just output data.
14
 *
15
 * Exit code only:
16
 *
17
 *      return CommandResult::dataWithExitCode(1);
18
 *
19
 * Data only:
20
 *
21
 *      return CommandResult::data(new RowsOfFields($rows));
22
 *
23
 * Historically, it has always been possible to return an integer to indicate
24
 * that the result is an exit code, and other return types (typically array
25
 * / ArrayObjects) indicating actual data with an implicit exit code of 0.
26
 * Using a CommandResult is preferred, though, as it allows the result of the
27
 * function to be unambiguously specified without type-based interpretation.
28
 *
29
 * @package Consolidation\AnnotatedCommand
30
 */
31
class CommandResult implements ExitCodeInterface, OutputDataInterface
32
{
33
    protected $data;
34
    protected $exitCode;
35
36
    protected function __construct($data = null, $exitCode = 0)
37
    {
38
        $this->data = $data;
39
        $this->exitCode = $exitCode;
40
    }
41
42
    public static function exitCode($exitCode)
43
    {
44
        return new self(null, $exitCode);
45
    }
46
47
    public static function data($data)
48
    {
49
        return new self($data);
50
    }
51
52
    public static function dataWithExitCode($data, $exitCode)
53
    {
54
        return new self($data, $exitCode);
55
    }
56
57
    public function getExitCode()
58
    {
59
        return $this->exitCode;
60
    }
61
62
    public function getOutputData()
63
    {
64
        return $this->data;
65
    }
66
67
    public function setOutputData($data)
68
    {
69
        $this->data = $data;
70
    }
71
}
72