Completed
Push — master ( 215ee6...5002cd )
by Akihito
02:09
created

ForkContainer::getCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

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 4
nc 2
nop 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\Fork;
5
use Ackintosh\Snidel\ForkCollection;
6
use Ackintosh\Snidel\Pcntl;
7
8
class ForkContainer implements \ArrayAccess
9
{
10
    /** @var \Ackintosh\Snidel\Fork[] */
11
    private $forks = array();
12
13
    /** @var \Ackintosh\Snidel\Pcntl */
14
    private $pcntl;
15
16
    /** @var array */
17
    private $tagsToPids = array();
18
19
    public function __construct()
20
    {
21
        $this->pcntl = new Pcntl();
22
    }
23
24
    /**
25
     * fork process
26
     *
27
     * @return \Ackintosh\Snidel\Fork
28
     * @throws \RuntimeException
29
     */
30
    public function fork($tag = null)
31
    {
32
        $pid = $this->pcntl->fork();
33
        if ($pid === -1) {
34
            throw new \RuntimeException('could not fork a new process');
35
        }
36
37
        $this->forks[$pid] = new Fork($pid);
38
        if ($tag !== null) {
39
            $this->tagsToPids[$pid] = $tag;
40
        }
41
42
        return $this->forks[$pid];
43
    }
44
45
    /**
46
     *
47
     * @param   string  $tag
48
     * @return  bool
49
     */
50
    public function hasTag($tag)
51
    {
52
        return in_array($tag, $this->tagsToPids, true);
53
    }
54
55
    /**
56
     * wait child
57
     *
58
     * @return \Ackintosh\Snidel\Fork
59
     */
60
    public function wait()
61
    {
62
        $status = null;
63
        $childPid = $this->pcntl->waitpid(-1, $status);
64
        $this[$childPid]->setStatus($status);
65
        $this[$childPid]->loadResult();
66
67
        return $this[$childPid];
68
    }
69
70
    public function getCollection($tag = null)
71
    {
72
        if ($tag === null) {
73
            return new ForkCollection($this->forks);
74
        }
75
76
        return $this->getCollectionWithTag($tag);
77
    }
78
79
    /**
80
     * return forks
81
     *
82
     * @param   string  $tag
83
     * @return  \Ackintosh\Snidel\Fork[]
84
     */
85
    private function getCollectionWithTag($tag)
86
    {
87
        return new ForkCollection(
88
            array_filter($this->forks, function ($fork) use ($tag) {
89
                return $this->tagsToPids[$fork->getPid()] === $tag;
90
            })
91
        );
92
    }
93
94
    /**
95
     * @param   mixed   $offset
96
     * @return  bool
97
     */
98
    public function offsetExists($offset)
99
    {
100
        if (isset($this->forks[$offset]) && $this->forks[$offset] !== '') {
101
            return true;
102
        }
103
104
        return false;
105
    }
106
107
    /**
108
     * @param   mixed   $offset
109
     * @return  mixed
110
     */
111
    public function offsetGet($offset)
112
    {
113
        if (!$this->offsetExists($offset)) {
114
            return null;
115
        }
116
117
        return $this->forks[$offset];
118
    }
119
120
    /**
121
     * @param   mixed   $offset
122
     * @return  void
123
     */
124
    public function offsetSet($offset, $value)
125
    {
126
        $this->forks[$offset] = $value;
127
    }
128
129
    /**
130
     * @param   mixed   $offset
131
     * @return  void
132
     */
133
    public function offsetUnset($offset)
134
    {
135
        unset($this->forks[$offset]);
136
    }
137
}
138