Completed
Push — master ( 5ef5a6...726f17 )
by Jonathan
02:41
created

Processes::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 5
crap 1
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 $numProcesses;
0 ignored issues
show
Unused Code introduced by
The property $numProcesses is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The property numParallelProcesses does not seem to exist. Did you mean processes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The property numParallelProcesses does not seem to exist. Did you mean processes?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
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 {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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