Completed
Push — master ( ac3291...615cf2 )
by Akihito
02:33
created

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