Completed
Push — master ( 77b1ac...c91bb2 )
by Akihito
02:29
created

Fork::minifyAndSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\Pcntl;
5
use Ackintosh\Snidel\Result;
6
use Ackintosh\Snidel\Task;
7
use Ackintosh\Snidel\TaskFormatter;
8
9
class Fork
10
{
11
    /** @var int */
12
    private $pid;
13
14
    /** @var \Ackintosh\Snidel\Pcntl */
15
    private $pcntl;
16
17
    /** @var int */
18
    private $status;
19
20
    /** @var callable */
21
    private $callable;
22
23
    /** @var array */
24
    private $args;
25
26
    /** @var \Ackintosh\Snidel\Result */
27
    private $result;
28
29
    /** @var string */
30
    private $tag;
31
32
    /** @var \Ackintosh\Snidel\Task */
33
    private $task;
34
35
    /** @var string */
36
    private $serializedTask;
37
38
    /** @var bool */
39
    private $queued = false;
40
41
    /**
42
     * @param   int     $pid
43
     */
44
    public function __construct($pid, $task)
45
    {
46
        $this->pid      = $pid;
47
        $this->task     = $task;
48
        $this->pcntl    = new Pcntl();
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
     * set callable
84
     *
85
     * @param   callable    $callable
86
     * @return  void
87
     */
88
    public function setCallable($callable)
89
    {
90
        $this->callable = $callable instanceof \Closure ? '*Closure*' : $callable;
91
    }
92
93
    public function getCallable()
94
    {
95
        return $this->callable;
96
    }
97
98
    /**
99
     * set arguments
100
     *
101
     * @param   array   $args
102
     * @return  void
103
     */
104
    public function setArgs($args)
105
    {
106
        $this->args = $args;
107
    }
108
109
    public function getArgs()
110
    {
111
        return $this->args;
112
    }
113
114
    /**
115
     * @return bool
116
     */
117
    public function hasFinishedSuccessfully()
118
    {
119
        return $this->pcntl->wifexited($this->status) && $this->pcntl->wexitstatus($this->status) === 0;
120
    }
121
122
    /**
123
     * @return bool
124
     */
125
    public function hasNotFinishedSuccessfully()
126
    {
127
        return !$this->hasFinishedSuccessfully();
128
    }
129
130
    /**
131
     *
132
     * @param   \Ackintosh\Snidel\Result
133
     * @return  void
134
     */
135
    public function setResult($result)
136
    {
137
        $this->result = $result;
138
    }
139
140
    /**
141
     * return result
142
     *
143
     * @return \Ackintosh\Snidel\Result
144
     */
145
    public function getResult()
146
    {
147
        return $this->result;
148
    }
149
150
    public function hasNoResult()
151
    {
152
        return $this->result === null;
153
    }
154
155
    /**
156
     * @param   string  $tag
157
     * @return  void
158
     */
159
    public function setTag($tag)
160
    {
161
        $this->tag = $tag;
162
    }
163
164
    /**
165
     * @return  string
166
     */
167
    public function getTag()
168
    {
169
        return $this->task->getTag();
170
    }
171
172
    /**
173
     * @return  void
174
     */
175
    public function setQueued()
176
    {
177
        $this->queued = true;
178
    }
179
180
    /**
181
     * @return  bool
182
     */
183
    public function isQueued()
184
    {
185
        return $this->queued;
186
    }
187
188
    public function executeTask()
189
    {
190
        ob_start();
191
        $result = new Result();
192
        $result->setReturn(
193
            call_user_func_array(
194
                $this->task->getCallable(),
195
                (is_array($args = $this->task->getArgs())) ? $args : array($args)
196
            )
197
        );
198
        $result->setOutput(ob_get_clean());
199
        $this->result = $result;
200
    }
201
202
    /**
203
     * @param   \Ackintosh\Snidel\Fork  $fork
204
     * @return  string
205
     */
206
    public static function serialize($fork)
207
    {
208
        $cloned = clone $fork;
209
        $cloned->serializeTask();
210
211
        return serialize($cloned);
212
    }
213
214
    /**
215
     * @return  void
216
     */
217
    private function serializeTask()
218
    {
219
        $this->serializedTask = TaskFormatter::serialize($this->task);
220
        unset($this->task);
221
    }
222
223
    public static function minifyAndSerialize($fork)
224
    {
225
        $cloned = clone $fork;
226
        $cloned->minifyAndSerializeTask();
227
228
        return serialize($cloned);
229
    }
230
231
    private function minifyAndSerializeTask()
232
    {
233
        $this->serializedTask = TaskFormatter::minifyAndSerialize($this->task);
234
        unset($this->task);
235
    }
236
237
    /**
238
     * @param   string  $serializedFork
239
     * @return  \Ackintosh\Snidel\Fork
240
     */
241
    public static function unserialize($serializedFork)
242
    {
243
        $fork = unserialize($serializedFork);
244
        $fork->unserializeTask();
245
246
        return $fork;
247
    }
248
249
    /**
250
     * @return  void
251
     */
252
    private function unserializeTask()
253
    {
254
        $this->task = TaskFormatter::unserialize($this->serializedTask);
255
        $this->serializedTask = null;
256
    }
257
258
    /**
259
     * @return  void
260
     */
261
    public function __clone()
262
    {
263
        // to avoid point to same object.
264
        $this->task = clone $this->task;
265
    }
266
}
267