Completed
Push — master ( 8a9369...6732b2 )
by kacper
04:04
created

AsyncProcess::hasOnErrorSet()   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
3
4
namespace Async;
5
6
use Symfony\Component\Process\Process;
7
8
/**
9
 * Class AsyncProcess
10
 * @package Async
11
 */
12
class AsyncProcess extends Process
13
{
14
    /**
15
     * @var bool
16
     */
17
    private $hasCallbackSet;
18
    /**
19
     * @var bool
20
     */
21
    private $hasOnErrorSet;
22
23
    /**
24
     * @param callable|null $callback
25
     * @param callable|null $onError
26
     * @throws \RuntimeException
27
     * @throws \Symfony\Component\Process\Exception\RuntimeException
28
     * @throws \Symfony\Component\Process\Exception\LogicException
29
     */
30
    public function startJob(callable $callback = null, callable $onError = null)
31
    {
32
        $this->hasCallbackSet = null !== $callback;
33
        $this->hasOnErrorSet = null !== $callback;
34
35
        $this->start(
36
            function ($type, $buffer) use ($callback, $onError) {
37
38
                // if we can't decode probably child failed to execute
39
                if ($decoded = base64_decode($buffer, true)) {
40
                    /** @var AsyncChildResponse $asyncChildResponse */
41
                    $asyncChildResponse = unserialize($decoded);
42
                } else {
43
                    throw new \RuntimeException('Child process returned: ' . $buffer);
44
                }
45
46
                if (null !== $onError && $asyncChildResponse->getError()) {
47
                    $onError($asyncChildResponse->getError());
48
                }
49
50
                if (null !== $callback) {
51
                    $callback($asyncChildResponse->getJobResult(), $asyncChildResponse->getStdOut());
52
                }
53
            }
54
        );
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function hasCallbackSet()
61
    {
62
        return $this->hasCallbackSet;
63
    }
64
65
    /**
66
     * @return bool
67
     */
68
    public function hasOnErrorSet()
69
    {
70
        return $this->hasOnErrorSet;
71
    }
72
}