Completed
Pull Request — master (#12)
by Harry
11:21
created

Run::poll()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7.7305

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 7
cts 11
cp 0.6364
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 14
nc 5
nop 0
crap 7.7305
1
<?php
2
3
namespace Graze\ParallelProcess;
4
5
use Symfony\Component\Process\Process;
6
7
class Run implements RunInterface
8
{
9
    const ON_SUCCESS  = 1;
10
    const ON_FAILURE  = 2;
11
    const ON_PROGRESS = 3;
12
13
    /** @var Process */
14
    private $process;
15
    /** @var callable|null */
16
    private $onSuccess;
17
    /** @var callable|null */
18
    private $onFailure;
19
    /** @var callable|null */
20
    private $onProgress;
21
    /** @var callable|null */
22
    private $onStart;
23
    /** @var float */
24
    private $started;
25
    /** @var bool */
26
    private $successful = false;
27
    /** @var bool */
28
    private $completed = false;
29
    /** @var string */
30
    private $last = '';
31
    /** @var string */
32
    private $lastType = 'std';
33
    /** @var bool */
34
    private $updateOnPoll = true;
35
    /** @var bool */
36
    private $updateOnProcessOutput = true;
37
38
    /**
39
     * Run constructor.
40
     *
41
     * @param Process       $process
42
     * @param callable|null $onSuccess  When the process finishes and is successful
43
     *                                  function (Process $process, float $duration, string $last, string $lastType) :
44
     *                                  void
45
     * @param callable|null $onFailure  When the process finishes and failed
46
     *                                  function (Process $process, float $duration, string $last, string $lastType) :
47
     *                                  void
48
     * @param callable|null $onProgress Called every check period or a message is returned from the process
49
     *                                  function (Process $process, float $duration, string $last, string $lastType) :
50
     *                                  void
51
     * @param callable|null $onStart    When the process starts
52
     *                                  function (Process $process, float $duration, string $last, string $lastType) :
53
     *                                  void
54
     */
55 36 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
        Process $process,
57 7
        callable $onSuccess = null,
58 9
        callable $onFailure = null,
59 10
        callable $onProgress = null,
60 21
        callable $onStart = null
61
    ) {
62 36
        $this->process = $process;
63 36
        $this->onSuccess = $onSuccess;
64 36
        $this->onFailure = $onFailure;
65 36
        $this->onProgress = $onProgress;
66 36
        $this->onStart = $onStart;
67
    }
68
69
    /**
70
     * Start the process
71
     *
72
     * @return $this
73
     */
74 30
    public function start()
75
    {
76
        if (!$this->process->isRunning()) {
77
            $this->started = microtime(true);
78
            $this->update($this->onStart);
79 29
            $this->process->start(
80 16
                function ($type, $data) {
81 11
                    $this->lastType = $type;
82
                    $this->last = rtrim($data);
83 11
                    if ($this->updateOnProcessOutput) {
84
                        $this->update($this->onProgress);
85
                    }
86 16
                }
87
            );
88 29
            $this->completed = false;
89
        }
90
91 30
        return $this;
92
    }
93
94
    /**
95
     * Poll the process to see if it is still running, and trigger events
96
     *
97
     * @return bool true if the process is currently running (started and not terminated)
98
     */
99 27
    public function poll()
100
    {
101
        if ($this->completed || !$this->hasStarted()) {
102
            return false;
103
        }
104
105
        if ($this->process->isRunning()) {
106 14
            if ($this->updateOnPoll) {
107
                $this->update($this->onProgress);
108
            }
109 14
            return true;
110
        }
111
112 27
        $this->completed = true;
113
114
        if ($this->process->isSuccessful()) {
115 19
            $this->successful = true;
116
            $this->update($this->onSuccess);
117
        } else {
118
            $this->update($this->onFailure);
119 19
        }
120 27
        return false;
121
    }
122
123
    /**
124
     * Return if the underlying process is running
125
     *
126
     * @return bool
127
     */
128
    public function isRunning()
129
    {
130
        return $this->process->isRunning();
131
    }
132
133
    /**
134
     * Call an event callback
135
     *
136
     * @param callable|null $func
137
     */
138 13
    protected function update($func)
139
    {
140
        if (!is_null($func)) {
141
            call_user_func(
142
                $func,
143 13
                $this->process,
144
                microtime(true) - $this->started,
145 13
                $this->last,
146 13
                $this->lastType
147
            );
148
        }
149 2
    }
150
151
    /**
152
     * @return bool
153
     */
154 23
    public function isSuccessful()
155
    {
156 23
        return $this->successful;
157
    }
158
159
    /**
160
     * @return bool
161
     */
162
    public function hasStarted()
163
    {
164
        return $this->process->isStarted();
165
    }
166
167
    /**
168
     * @return Process
169
     */
170 2
    public function getProcess()
171
    {
172 2
        return $this->process;
173
    }
174
175
    /**
176
     * @param bool $updateOnPoll
177
     *
178
     * @return $this
179
     */
180 7
    public function setUpdateOnPoll($updateOnPoll)
181
    {
182 7
        $this->updateOnPoll = $updateOnPoll;
183 7
        return $this;
184
    }
185
186
    /**
187
     * @return bool
188
     */
189 1
    public function isUpdateOnPoll()
190
    {
191 1
        return $this->updateOnPoll;
192
    }
193
194
    /**
195
     * @param bool $updateOnProcessOutput
196
     *
197
     * @return $this
198
     */
199 10
    public function setUpdateOnProcessOutput($updateOnProcessOutput)
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $updateOnProcessOutput exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
200
    {
201 10
        $this->updateOnProcessOutput = $updateOnProcessOutput;
202 10
        return $this;
203
    }
204
205
    /**
206
     * @return bool
207
     */
208 1
    public function isUpdateOnProcessOutput()
209
    {
210 1
        return $this->updateOnProcessOutput;
211
    }
212
}
213