Passed
Push — main ( 3f9c51...efaa1b )
by Justin
03:15
created

InterceptedCommand::getCommandInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace Modulate\Artisan\Interceptor;
4
5
use Symfony\Component\Console\Application;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class InterceptedCommand
11
{
12
13
    /**
14
     * The command that was called
15
     *
16
     * @var string
17
     */
18
    protected string $command;
19
20
    /**
21
     * The artisan console instance
22
     *
23
     * @var Application
24
     */
25
    protected Application $app;
26
27
    /**
28
     * The input given to the command
29
     *
30
     * @var InputInterface
31
     */
32
    protected InputInterface $input;
33
34
    /**
35
     * The instance where the output of the command will be sent
36
     *
37
     * @var OutputInterface
38
     */
39
    protected OutputInterface $output;
40
41
    /**
42
     * The exit code of the command which will be populated when the CommandFinished event is fired
43
     *
44
     * @var integer|null
45
     */
46
    protected int|null $exitCode = 0;
47
48
    /**
49
     * @param string $command
50
     * @param InputInterface $input
51
     * @param OutputInterface $output
52
     * @param integer|null $exitCode
53
     */
54
    public function __construct(string $command, InputInterface $input, OutputInterface $output, int $exitCode = null)
55
    {
56
        $this->command = $command;
57
        $this->input = $input;
58
        $this->output = $output;
59
        $this->exitCode = $exitCode;
60
    }
61
62
    /**
63
     * Return the command that was intercepted
64
     *
65
     * @return string
66
     */
67
    public function getCommand(): string
68
    {
69
        return $this->command;
70
    }
71
72
    public function getCommandInstance(): Command
73
    {
74
        return $this->getArtisan()->get($this->command);
75
    }
76
77
    /**
78
     * Inject the artisan application into the intercepted command
79
     *
80
     * @param Application $app
81
     * @return void
82
     */
83
    public function setArtisan(Application $app)
84
    {
85
        $this->app = $app;
86
    }
87
88
    /**
89
     * Get the artisan console application
90
     *
91
     * @return Application
92
     */
93
    public function getArtisan(): Application
94
    {
95
        return $this->app;
96
    }
97
98
    /**
99
     * Get the input for the command
100
     *
101
     * @return InputInterface
102
     */
103
    public function getInput(): InputInterface
104
    {
105
        return $this->input;
106
    }
107
108
    /**
109
     * Get an option from the command input
110
     *
111
     * @param string $name
112
     * @return mixed
113
     */
114
    public function getOption(string $name): mixed
115
    {
116
        return $this->getInput()->getOption($name);
117
    }
118
119
    /**
120
     * Get an argument from the command input
121
     *
122
     * @param string $name
123
     * @return mixed
124
     */
125
    public function getArgument(string $name): mixed
126
    {
127
        return $this->getInput()->getArgument($name);
128
    }
129
130
    /**
131
     * Get the output for the command
132
     *
133
     * @return OutputInterface
134
     */
135
    public function getOutput(): OutputInterface
136
    {
137
        return $this->output;
138
    }
139
140
    /**
141
     * Get the exit code of the finished command
142
     *
143
     * @return integer
144
     */
145
    public function getExitCode(): int|null
146
    {
147
        return $this->exitCode;
148
    }
149
150
    /**
151
     * Determines if the command is still running by the lack of an exit code
152
     *
153
     * @return boolean
154
     */
155
    public function hasFinished(): bool
156
    {
157
        return $this->exitCode !== null;
158
    }
159
}
160