Test Failed
Push — main ( bf77ba...401323 )
by Teodoro
03:31 queued 41s
created

Task::stop()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
/** @noinspection UnserializeExploitsInspection */
4
5
namespace Tleckie\Async;
6
7
use Symfony\Component\Process\Process;
8
9
/**
10
 * Class Task
11
 *
12
 * @package Tleckie\Async
13
 * @author  Teodoro Leckie Westberg <[email protected]>
14
 */
15
class Task implements TaskInterface
16
{
17
    /** @var int */
18
    private int $id;
19
20
    /** @var int */
21
    private int $pid;
22
23
    /** @var Process */
24
    private Process $process;
25
26
    /** @var callable[] */
27
    private array $success = [];
28
29
    /** @var callable[] */
30
    private array $error = [];
31
32
    /** @var Encoder */
33
    private Encoder $encoder;
34
35
    /** @var mixed */
36
    private mixed $output = null;
37
38
    /** @var mixed */
39
    private mixed $errorOutput = null;
40
41
    /**
42
     * Task constructor.
43
     *
44
     * @param Process $process
45
     * @param Encoder $encoder
46
     * @param int     $id
47
     */
48
    public function __construct(
49
        Process $process,
50
        Encoder $encoder,
51
        int $id
52
    )
53
    {
54
        $this->process = $process;
55
        $this->encoder = $encoder;
56
        $this->id = $id;
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function then(callable $callback): self
63
    {
64
        $this->success[] = $callback;
65
66
        return $this;
67
    }
68
69
    public function catch(callable $callback): self
70
    {
71
        $this->error[] = $callback;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function handle(): self
80
    {
81
        $output = [$this->output()];
82
83
        foreach ($this->success as $callback) {
84
            $this->call($callback, $output);
85
        }
86
87
        return $this;
88
    }
89
90
    /**
91
     * @inheritdoc
92
     */
93
    public function output(): mixed
94
    {
95
        if (!$this->output) {
96
            $output = $this->process->getOutput();
97
            $this->output = $this->encoder->decode($output) ?? $output;
98
        }
99
100
        return $this->output;
101
    }
102
103
    /**
104
     * @param callable $callback
105
     * @param array    $arguments
106
     */
107
    private function call(callable $callback, array $arguments): void
108
    {
109
        call_user_func_array($callback, $arguments);
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function error(): self
116
    {
117
        $output = [$this->getErrorOutput()];
118
        foreach ($this->error as $callback) {
119
            $this->call($callback, $output);
120
        }
121
122
        return $this;
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function getErrorOutput(): mixed
129
    {
130
        if (!$this->errorOutput) {
131
            $output = $this->process->getErrorOutput();
132
            $this->errorOutput = $this->encoder->decode($output) ?? $output;
133
        }
134
135
        return $this->errorOutput;
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141
    public function id(): int
142
    {
143
        return $this->id;
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149
    public function pid(): int
150
    {
151
        return $this->pid;
152
    }
153
154
    /**
155
     * @inheritdoc
156
     */
157
    public function start(): self
158
    {
159
        $this->process->start();
160
161
        $this->pid = $this->process->getPid();
162
163
        return $this;
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169
    public function isRunning(): bool
170
    {
171
        return $this->process->isRunning();
172
    }
173
174
    /**
175
     * @inheritdoc
176
     */
177
    public function isSuccessful(): bool
178
    {
179
        return $this->process->isSuccessful();
180
    }
181
182
    /**
183
     * @inheritdoc
184
     */
185
    public function isTerminated(): bool
186
    {
187
        return $this->process->isTerminated();
188
    }
189
}
190