Completed
Push — master ( 15d2ac...024d05 )
by Akihito
02:47
created

Fork   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 3

Importance

Changes 10
Bugs 0 Features 1
Metric Value
wmc 24
c 10
b 0
f 1
lcom 3
cbo 3
dl 0
loc 229
rs 10

22 Methods

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