Completed
Push — master ( 94cacb...6d69f5 )
by Akihito
03:32
created

Fork::hasFinishedSuccessfully()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
nop 0
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\DataRepository;
5
use Ackintosh\Snidel\Pcntl;
6
use Ackintosh\Snidel\Exception\SharedMemoryControlException;
7
8
class Fork
9
{
10
    /** @var int */
11
    private $pid;
12
13
    /** @var \Ackintosh\Snidel\Pcntl */
14
    private $pcntl;
15
16
    /** @var \Ackintosh\Snidel\DataRepository */
17
    private $dataRepository;
18
19
    /** @var int */
20
    private $status;
21
22
    /** @var callable */
23
    private $callable;
24
25
    /** @var array */
26
    private $args;
27
28
    /** @var \Ackintosh\Snidel\Result */
29
    private $result;
30
31
    /** @var string */
32
    private $tag;
33
34
    /**
35
     * @param   int     $pid
36
     */
37
    public function __construct($pid)
38
    {
39
        $this->pid              = $pid;
40
        $this->pcntl            = new Pcntl();
41
        $this->dataRepository   = new DataRepository();
42
    }
43
44
    /**
45
     * set exit status
46
     *
47
     * @param   int     $status
48
     * @return  void
49
     */
50
    public function setStatus($status)
51
    {
52
        $this->status = $status;
53
    }
54
55
    /**
56
     * return pid
57
     *
58
     * @return  int
59
     */
60
    public function getPid()
61
    {
62
        return $this->pid;
63
    }
64
65
    /**
66
     * return exit status
67
     *
68
     * @return int
69
     */
70
    public function getStatus()
71
    {
72
        return $this->status;
73
    }
74
75
    /**
76
     * set callable
77
     *
78
     * @param   callable    $callable
79
     * @return  void
80
     */
81
    public function setCallable($callable)
82
    {
83
        $this->callable = $callable instanceof \Closure ? '*Closure*' : $callable;
84
    }
85
86
    public function getCallable()
87
    {
88
        return $this->callable;
89
    }
90
91
    /**
92
     * set arguments
93
     *
94
     * @param   array   $args
95
     * @return  void
96
     */
97
    public function setArgs($args)
98
    {
99
        $this->args = $args;
100
    }
101
102
    public function getArgs()
103
    {
104
        return $this->args;
105
    }
106
107
    /**
108
     * @return bool
109
     */
110
    public function hasFinishedSuccessfully()
111
    {
112
        return $this->pcntl->wifexited($this->status) && $this->pcntl->wexitstatus($this->status) === 0;
113
    }
114
115
    /**
116
     * @return bool
117
     */
118
    public function hasNotFinishedSuccessfully()
119
    {
120
        return !$this->hasFinishedSuccessfully();
121
    }
122
123
124
    /**
125
     * load result
126
     *
127
     * @return void
128
     * @throws \Ackintosh\Snidel\Exception\SharedMemoryControlException
129
     */
130
    public function loadResult()
131
    {
132
        try {
133
            $this->setResult($this->dataRepository->load($this->pid)->readAndDelete());
134
        } catch (SharedMemoryControlException $e) {
135
            throw $e;
136
        }
137
    }
138
139
    /**
140
     *
141
     * @param   \Ackintosh\Snidel\Result
142
     * @return  void
143
     */
144
    public function setResult($result)
145
    {
146
        $this->result = $result;
147
    }
148
149
    /**
150
     * return result
151
     *
152
     * @return \Ackintosh\Snidel\Result
153
     */
154
    public function getResult()
155
    {
156
        return $this->result;
157
    }
158
159
    public function hasNoResult()
160
    {
161
        return $this->result === null;
162
    }
163
164
    /**
165
     * @param   string  $tag
166
     * @return  void
167
     */
168
    public function setTag($tag)
169
    {
170
        $this->tag = $tag;
171
    }
172
173
    /**
174
     * @return  string
175
     */
176
    public function getTag()
177
    {
178
        return $this->tag;
179
    }
180
}
181