Processes::addProcess()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace PHPChunkit;
6
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\Process;
9
10
class Processes
11
{
12
    /**
13
     * @var ChunkResults
14
     */
15
    private $chunkResults;
16
17
    /**
18
     * @var OutputInterface
19
     */
20
    private $output;
21
22
    /**
23
     * @var int
24
     */
25
    private $numParallelProcesses;
26
27
    /**
28
     * @var bool
29
     */
30
    private $verbose;
31
32
    /**
33
     * @var bool
34
     */
35
    private $stop;
36
37
    /**
38
     * @var array
39
     */
40
    private $processes = [];
41
42 1
    public function __construct(
43
        ChunkResults $chunkResults,
44
        OutputInterface $output,
45
        int $numParallelProcesses = 1,
46
        bool $verbose = false,
47
        bool $stop = false)
48
    {
49 1
        $this->chunkResults = $chunkResults;
50 1
        $this->output = $output;
51 1
        $this->numParallelProcesses = $numParallelProcesses;
52 1
        $this->verbose = $verbose;
53 1
        $this->stop = $stop;
54 1
    }
55
56
    public function addProcess(Process $process)
57
    {
58
        $this->processes[] = $process;
59
    }
60
61
    public function wait()
62
    {
63
        if (count($this->processes) < $this->numParallelProcesses) {
64
            return;
65
        }
66
67
        while (count($this->processes)) {
68
            foreach ($this->processes as $i => $process) {
69
                $chunkNum = $i + 1;
70
71
                if ($process->isRunning()) {
72
                    continue;
73
                }
74
75
                unset($this->processes[$i]);
76
77
                $this->chunkResults->addCode($code = $process->getExitCode());
78
79
                if ($code > 0) {
80
                    $this->chunkResults->incrementNumChunkFailures();
81
82
                    $this->output->writeln(sprintf('Chunk #%s <error>FAILED</error>', $chunkNum));
83
84
                    $this->output->writeln('');
85
                    $this->output->write($process->getOutput());
86
87
                    if ($this->stop) {
88
                        return $code;
89
                    }
90 View Code Duplication
                } else {
91
                    $this->output->writeln(sprintf('Chunk #%s <info>PASSED</info>', $chunkNum));
92
93
                    if ($this->verbose) {
94
                        $this->output->writeln('');
95
                        $this->output->write($process->getOutput());
96
                    }
97
                }
98
            }
99
        }
100
    }
101
}
102