Completed
Push — master ( 27f7d3...3aad4e )
by Akihito
02:33
created

Fork::getCallable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\DataRepository;
5
use Ackintosh\Snidel\Pcntl;
6
use Ackintosh\Snidel\Exception\SharedMemoryControlException;
7
8
class Fork
9
{
10
    /** @var int */
11
    private $pid;
12
13
    /** @var \Ackintosh\Snidel\Pcntl */
14
    private $pcntl;
15
16
    /** @var \Ackintosh\Snidel\DataRepository */
17
    private $dataRepository;
18
19
    /** @var int */
20
    private $status;
21
22
    /** @var callable */
23
    private $callable;
24
25
    /** @var array */
26
    private $args;
27
28
    /** @var \Ackintosh\Snidel\Result */
29
    private $result;
30
31
    /**
32
     * @param   int     $pid
33
     */
34
    public function __construct($pid)
35
    {
36
        $this->pid              = $pid;
37
        $this->pcntl            = new Pcntl();
38
        $this->dataRepository   = new DataRepository();
39
    }
40
41
    /**
42
     * set exit status
43
     *
44
     * @param   int     $status
45
     * @return  void
46
     */
47
    public function setStatus($status)
48
    {
49
        $this->status = $status;
50
    }
51
52
    /**
53
     * return pid
54
     *
55
     * @return  int
56
     */
57
    public function getPid()
58
    {
59
        return $this->pid;
60
    }
61
62
    /**
63
     * return exit status
64
     *
65
     * @return int
66
     */
67
    public function getStatus()
68
    {
69
        return $this->status;
70
    }
71
72
    /**
73
     * set callable
74
     *
75
     * @param   callable    $callable
76
     * @return  void
77
     */
78
    public function setCallable($callable)
79
    {
80
        $this->callable = $callable instanceof \Closure ? '*Closure*' : $callable;
81
    }
82
83
    public function getCallable()
84
    {
85
        return $this->callable;
86
    }
87
88
    /**
89
     * set arguments
90
     *
91
     * @param   array   $args
92
     * @return  void
93
     */
94
    public function setArgs($args)
95
    {
96
        $this->args = $args;
97
    }
98
99
    public function getArgs()
100
    {
101
        return $this->args;
102
    }
103
    /**
104
     * @return bool
105
     */
106
    public function isSuccessful()
107
    {
108
        return $this->pcntl->wifexited($this->status) && $this->pcntl->wexitstatus($this->status) === 0;
109
    }
110
111
    /**
112
     * load result
113
     *
114
     * @return void
115
     * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException
116
     */
117
    public function loadResult()
118
    {
119
        try {
120
            $this->result = $this->dataRepository->load($this->pid)->readAndDelete();
121
        } catch (SharedMemoryControlException $e) {
122
            throw $e;
123
        }
124
    }
125
126
    /**
127
     * return result
128
     *
129
     * @return \Ackintosh\Snidel\Result
130
     */
131
    public function getResult()
132
    {
133
        return $this->result;
134
    }
135
}
136