Completed
Push — master ( 615cf2...e32fe6 )
by Akihito
02:30
created

Fork::loadResult()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

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 2
eloc 5
nc 2
nop 0

1 Method

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