Completed
Push — master ( d54861...72c40c )
by Akihito
02:30
created

Fork::executeTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 6
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\Result;
7
use Ackintosh\Snidel\Exception\SharedMemoryControlException;
8
9
class Fork
10
{
11
    /** @var int */
12
    private $pid;
13
14
    /** @var \Ackintosh\Snidel\Pcntl */
15
    private $pcntl;
16
17
    /** @var \Ackintosh\Snidel\DataRepository */
18
    private $dataRepository;
19
20
    /** @var int */
21
    private $status;
22
23
    /** @var callable */
24
    private $callable;
25
26
    /** @var array */
27
    private $args;
28
29
    /** @var \Ackintosh\Snidel\Result */
30
    private $result;
31
32
    /** @var string */
33
    private $tag;
34
35
    /** @var \Ackintosh\Snidel\Task */
36
    private $task;
37
38
    /** @var string */
39
    private $serializedTask;
40
41
    /**
42
     * @param   int     $pid
43
     */
44
    public function __construct($pid)
45
    {
46
        $this->pid              = $pid;
47
        $this->pcntl            = new Pcntl();
48
        $this->dataRepository   = new DataRepository();
49
    }
50
51
    /**
52
     * set exit status
53
     *
54
     * @param   int     $status
55
     * @return  void
56
     */
57
    public function setStatus($status)
58
    {
59
        $this->status = $status;
60
    }
61
62
    /**
63
     * return pid
64
     *
65
     * @return  int
66
     */
67
    public function getPid()
68
    {
69
        return $this->pid;
70
    }
71
72
    /**
73
     * return exit status
74
     *
75
     * @return int
76
     */
77
    public function getStatus()
78
    {
79
        return $this->status;
80
    }
81
82
    /**
83
     * @param   \Ackintosh\Snidel\Task
84
     * @return  void
85
     */
86
    public function setTask($task)
87
    {
88
        $this->task = $task;
89
    }
90
91
    /**
92
     * set callable
93
     *
94
     * @param   callable    $callable
95
     * @return  void
96
     */
97
    public function setCallable($callable)
98
    {
99
        $this->callable = $callable instanceof \Closure ? '*Closure*' : $callable;
100
    }
101
102
    public function getCallable()
103
    {
104
        return $this->callable;
105
    }
106
107
    /**
108
     * set arguments
109
     *
110
     * @param   array   $args
111
     * @return  void
112
     */
113
    public function setArgs($args)
114
    {
115
        $this->args = $args;
116
    }
117
118
    public function getArgs()
119
    {
120
        return $this->args;
121
    }
122
123
    /**
124
     * @return bool
125
     */
126
    public function hasFinishedSuccessfully()
127
    {
128
        return $this->pcntl->wifexited($this->status) && $this->pcntl->wexitstatus($this->status) === 0;
129
    }
130
131
    /**
132
     * @return bool
133
     */
134
    public function hasNotFinishedSuccessfully()
135
    {
136
        return !$this->hasFinishedSuccessfully();
137
    }
138
139
140
    /**
141
     * load result
142
     *
143
     * @return void
144
     * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException
145
     */
146
    public function loadResult()
147
    {
148
        try {
149
            $this->setResult($this->dataRepository->load($this->pid)->readAndDelete());
150
        } catch (SharedMemoryControlException $e) {
151
            throw $e;
152
        }
153
    }
154
155
    /**
156
     *
157
     * @param   \Ackintosh\Snidel\Result
158
     * @return  void
159
     */
160
    public function setResult($result)
161
    {
162
        $this->result = $result;
163
    }
164
165
    /**
166
     * return result
167
     *
168
     * @return \Ackintosh\Snidel\Result
169
     */
170
    public function getResult()
171
    {
172
        return $this->result;
173
    }
174
175
    public function hasNoResult()
176
    {
177
        return $this->result === null;
178
    }
179
180
    /**
181
     * @param   string  $tag
182
     * @return  void
183
     */
184
    public function setTag($tag)
185
    {
186
        $this->tag = $tag;
187
    }
188
189
    /**
190
     * @return  string
191
     */
192
    public function getTag()
193
    {
194
        return $this->tag;
195
    }
196
197
    public function executeTask()
198
    {
199
        ob_start();
200
        $result = new Result();
201
        $result->setReturn(call_user_func_array($this->task->getCallable(), $this->task->getArgs()));
202
        $result->setOutput(ob_get_clean());
203
        $this->result = $result;
204
    }
205
206
    public function serialize()
207
    {
208
        $this->serializedTask = $this->task->serialize();
209
        unset($this->task);
210
211
        return serialize($this);
212
    }
213
214
    /**
215
     * @param   string  $serializedFork
216
     * @return  \Ackintosh\Snidel\Fork
217
     */
218
    public static function unserialize($serializedFork)
219
    {
220
        $fork = unserialize($serializedFork);
221
        $fork->unserializeTask();
222
223
        return $fork;
224
    }
225
226
    /**
227
     * @return  void
228
     */
229
    private function unserializeTask()
230
    {
231
        $task = unserialize($this->serializedTask);
232
        $this->task = $task->unserialize();
233
        unset($this->serializedTask);
234
    }
235
}
236