Passed
Pull Request — master (#233)
by Fabien
02:33
created

ParallelProcessHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 83
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 12 3
A __construct() 0 3 1
A getProcessResults() 0 17 5
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Processes\Handler;
4
5
use Churn\Collections\FileCollection;
6
use Churn\Factories\ProcessFactory;
7
use Illuminate\Support\Collection;
0 ignored issues
show
Bug introduced by
The type Illuminate\Support\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
9
class ParallelProcessHandler implements ProcessHandler
10
{
11
    /**
12
     * Collection of running processes.
13
     * @var Collection
14
     */
15
    private $runningProcesses;
16
17
    /**
18
     * Collection of files.
19
     * @var FileCollection
20
     */
21
    private $filesCollection;
22
23
    /**
24
     * Array of completed processes.
25
     * @var array
26
     */
27
    private $completedProcessesArray;
28
29
    /**
30
     * Process Factory.
31
     * @var ProcessFactory
32
     */
33
    private $processFactory;
34
35
    /**
36
     * Number of parallel jobs to run.
37
     * @var integer
38
     */
39
    private $numberOfParallelJobs;
40
41
    /**
42
     * ProcessManager constructor.
43
     * @param int $numberOfParallelJobs Number of parallel jobs to run.
44
     */
45
    public function __construct(int $numberOfParallelJobs)
46
    {
47
        $this->numberOfParallelJobs = $numberOfParallelJobs;
48
    }
49
50
    /**
51
     * Run the processes to gather information.
52
     * @param FileCollection $filesCollection Collection of files.
53
     * @param ProcessFactory $processFactory  Process Factory.
54
     * @return Collection
55
     */
56
    public function process(
57
        FileCollection $filesCollection,
58
        ProcessFactory $processFactory
59
    ): Collection {
60
        $this->filesCollection = $filesCollection;
61
        $this->processFactory = $processFactory;
62
        $this->runningProcesses = new Collection;
63
        $this->completedProcessesArray = [];
64
        while ($filesCollection->hasFiles() || $this->runningProcesses->count()) {
65
            $this->getProcessResults($this->numberOfParallelJobs);
66
        }
67
        return new Collection($this->completedProcessesArray);
68
    }
69
70
    /**
71
     * Get the results of the processes.
72
     * @param integer $numberOfParallelJobs Number of parallel jobs to run.
73
     * @return void
74
     */
75
    private function getProcessResults(int $numberOfParallelJobs): void
76
    {
77
        $index = $this->runningProcesses->count();
78
        for (; $index < $numberOfParallelJobs && $this->filesCollection->hasFiles() > 0; $index++) {
79
            $file = $this->filesCollection->getNextFile();
80
            $process = $this->processFactory->createGitCommitProcess($file);
81
            $process->start();
82
            $this->runningProcesses->put($process->getKey(), $process);
83
            $process = $this->processFactory->createCyclomaticComplexityProcess($file);
84
            $process->start();
85
            $this->runningProcesses->put($process->getKey(), $process);
86
        }
87
88
        foreach ($this->runningProcesses as $file => $process) {
89
            if ($process->isSuccessful()) {
90
                $this->runningProcesses->forget($process->getKey());
91
                $this->completedProcessesArray[$process->getFileName()][$process->getType()] = $process;
92
            }
93
        }
94
    }
95
}
96