Completed
Push — master ( 526d6e...2376dc )
by Akihito
03:04
created

MapContainer::getLastMapPids()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ackintosh\Snidel;
3
4
use Ackintosh\Snidel\Map;
5
use Ackintosh\Snidel\Exception\MapContainerException;
6
7
class MapContainer
8
{
9
    /** @var array */
10
    private $args;
11
12
    /** @var Snidel\Map[] */
13
    private $maps = array();
14
15
    /** @var int */
16
    private $concurrency;
17
18
    /**
19
     * @param   array       $args
20
     * @param   callable    $callable
21
     * @param   int         $concurrency
22
     */
23
    public function __construct(Array $args, $callable, $concurrency)
24
    {
25
        $this->args = $args;
26
        $this->maps[] = new Map($callable, $concurrency);
27
        $this->concurrency = $concurrency;
28
    }
29
30
    /**
31
     * stacks map object
32
     *
33
     * @param   callable                $callable
34
     * @return  Snidel\MapContainer     $this
35
     */
36
    public function then($callable)
37
    {
38
        $this->maps[] = new Map($callable, $this->concurrency);
39
        return $this;
40
    }
41
42
    /**
43
     * returns first map
44
     *
45
     * @return Snidel\Map
46
     */
47
    public function getFirstMap()
48
    {
49
        return $this->maps[0];
50
    }
51
52
    /**
53
     * returns args
54
     *
55
     * @return array
56
     */
57
    public function getFirstArgs()
58
    {
59
        return $this->args;
60
    }
61
62
    /**
63
     * returns array of PID last map owned
64
     *
65
     * @return array
66
     */
67
    public function getLastMapPids()
68
    {
69
        return $this->maps[(count($this->maps) - 1)]->getChildPids();
70
    }
71
72
    /**
73
     * maps are at that time processing its function or not
74
     *
75
     * @return bool
76
     */
77
    public function isProcessing()
78
    {
79
        foreach ($this->maps as $m) {
80
            if ($m->isProcessing()) {
81
                return true;
82
            }
83
        }
84
85
        return false;
86
    }
87
88
    /**
89
     * count up the number of completed
90
     *
91
     * @param   int     $childPid
92
     * @return  void
93
     */
94
    public function countTheCompleted($childPid)
95
    {
96
        foreach ($this->maps as $m) {
97
            if (!$m->hasChild($childPid)) {
98
                continue;
99
            }
100
            $m->countTheCompleted();
101
        }
102
    }
103
104
    /**
105
     * returns next map
106
     *
107
     * @param   int     $childPid
108
     * @return  Snidel\Map
109
     */
110
    public function nextMap($childPid)
111
    {
112
        try {
113
            $currentIndex = $this->getMapIndex($childPid);
114
        } catch (MapContainerException $e) {
115
            throw $e;
116
        }
117
        if (isset($this->maps[$currentIndex + 1])) {
118
            return $this->maps[$currentIndex + 1];
119
        }
120
121
        return;
122
    }
123
124
    /**
125
     * returns array index of map that owns $childPid
126
     *
127
     * @param   int     $childPid
128
     * @return  int
129
     */
130
    private function getMapIndex($childPid)
131
    {
132
        foreach ($this->maps as $index => $m) {
133
            if (!$m->hasChild($childPid)) {
134
                continue;
135
            }
136
137
            return $index;
138
        }
139
140
        throw new MapContainerException('childPid not found. pid: ' . $childPid);
141
    }
142
}
143