Passed
Push — main ( b7649a...398f47 )
by Michiel
06:32
created

ParallelTask::main()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 32
ccs 0
cts 17
cp 0
rs 9.4222
c 0
b 0
f 0
cc 5
nc 7
nop 0
crap 30
1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 *
19
 * @package phing.tasks.ext
20
 */
21
22
namespace Phing\Task\Optional;
23
24
use MehrAlsNix;
25
use Phing\Exception\BuildException;
26
use Phing\Task\System\SequentialTask;
27
28
/**
29
 * Uses the DocBlox_Parallel library to run nested Phing tasks concurrently.
30
 *
31
 * WARNING: this task is highly experimental!
32
 *
33
 * @author  Michiel Rook <[email protected]>
34
 * @package phing.tasks.ext
35
 * @see     https://github.com/phpdocumentor/Parallel
36
 * @since   2.4.10
37
 */
38
class ParallelTask extends SequentialTask
39
{
40
    /**
41
     * Maximum number of threads / processes
42
     *
43
     * @var int
44
     */
45
    private $threadCount = 2;
46
47
    /**
48
     * Sets the maximum number of threads / processes to use
49
     *
50
     * @param int $threadCount
51
     */
52
    public function setThreadCount($threadCount)
53
    {
54
        $this->threadCount = $threadCount;
55
    }
56
57
    public function init()
58
    {
59
    }
60
61
    public function main()
62
    {
63
        if (!class_exists('MehrAlsNix\Parallel\Worker')) {
64
            throw new BuildException(
65
                'ParallelTask depends on DocBlox being installed and on include_path.',
66
                $this->getLocation()
67
            );
68
        }
69
70
        $mgr = new MehrAlsNix\Parallel\Manager();
71
        $mgr->setProcessLimit($this->threadCount);
72
73
        foreach ($this->nestedTasks as $task) {
74
            $worker = new MehrAlsNix\Parallel\Worker(
75
                [$task, 'perform'],
76
                [$task]
77
            );
78
79
            $mgr->addWorker($worker);
80
        }
81
82
        $mgr->execute();
83
84
        /**
85
         * @var MehrAlsNix\Parallel\Worker $nestedTask
86
         */
87
        foreach ($mgr as $nestedTask) {
88
            if ($nestedTask->getError() === "") {
89
                continue;
90
            }
91
92
            throw new BuildException($nestedTask->getError());
93
        }
94
    }
95
}
96