Passed
Push — master ( d475b6...1e5be3 )
by Fabien
02:11
created

ParallelProcessHandler::getProcessResults()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 6
nop 2
dl 0
loc 18
rs 9.4888
c 0
b 0
f 0
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Process\Handler;
4
5
use Churn\Collections\FileCollection;
6
use Churn\Process\Observer\OnSuccess;
7
use Churn\Process\ProcessFactory;
8
use Churn\Values\File;
9
use function count;
10
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...
11
12
class ParallelProcessHandler implements ProcessHandler
13
{
14
    /**
15
     * Collection of running processes.
16
     * @var Collection
17
     */
18
    private $runningProcesses;
19
20
    /**
21
     * Collection of files.
22
     * @var FileCollection
23
     */
24
    private $filesCollection;
25
26
    /**
27
     * Array of completed processes.
28
     * @var array
29
     */
30
    private $completedProcessesArray;
31
32
    /**
33
     * Process Factory.
34
     * @var ProcessFactory
35
     */
36
    private $processFactory;
37
38
    /**
39
     * Number of parallel jobs to run.
40
     * @var integer
41
     */
42
    private $numberOfParallelJobs;
43
44
    /**
45
     * ProcessManager constructor.
46
     * @param int $numberOfParallelJobs Number of parallel jobs to run.
47
     */
48
    public function __construct(int $numberOfParallelJobs)
49
    {
50
        $this->numberOfParallelJobs = $numberOfParallelJobs;
51
    }
52
53
    /**
54
     * Run the processes to gather information.
55
     * @param FileCollection $filesCollection Collection of files.
56
     * @param ProcessFactory $processFactory  Process Factory.
57
     * @param OnSuccess      $onSuccess       The OnSuccess event observer.
58
     * @return Collection
59
     */
60
    public function process(
61
        FileCollection $filesCollection,
62
        ProcessFactory $processFactory,
63
        OnSuccess $onSuccess
64
    ): Collection {
65
        $this->filesCollection = $filesCollection;
66
        $this->processFactory = $processFactory;
67
        $this->runningProcesses = new Collection;
68
        $this->completedProcessesArray = [];
69
        while ($filesCollection->hasFiles() || $this->runningProcesses->count()) {
70
            $this->getProcessResults($this->numberOfParallelJobs, $onSuccess);
71
        }
72
        return new Collection($this->completedProcessesArray);
73
    }
74
75
    /**
76
     * Get the results of the processes.
77
     * @param integer   $numberOfParallelJobs Number of parallel jobs to run.
78
     * @param OnSuccess $onSuccess            The OnSuccess event observer.
79
     * @return void
80
     */
81
    private function getProcessResults(int $numberOfParallelJobs, OnSuccess $onSuccess): void
82
    {
83
        $index = $this->runningProcesses->count();
84
        for (; $index < $numberOfParallelJobs && $this->filesCollection->hasFiles() > 0; $index++) {
85
            $file = $this->filesCollection->getNextFile();
86
            $process = $this->processFactory->createGitCommitProcess($file);
87
            $process->start();
88
            $this->runningProcesses->put($process->getKey(), $process);
89
            $process = $this->processFactory->createCyclomaticComplexityProcess($file);
90
            $process->start();
91
            $this->runningProcesses->put($process->getKey(), $process);
92
        }
93
94
        foreach ($this->runningProcesses as $file => $process) {
95
            if ($process->isSuccessful()) {
96
                $this->runningProcesses->forget($process->getKey());
97
                $this->completedProcessesArray[$process->getFileName()][$process->getType()] = $process;
98
                $this->sendEventIfComplete($process->getFile(), $onSuccess);
99
            }
100
        }
101
    }
102
103
    /**
104
     * @param File      $file      The file processed.
105
     * @param OnSuccess $onSuccess The OnSuccess event observer.
106
     * @return void
107
     */
108
    private function sendEventIfComplete(File $file, OnSuccess $onSuccess): void
109
    {
110
        if (count($this->completedProcessesArray[$file->getDisplayPath()]) === 2) {
111
            $onSuccess($file);
112
        }
113
    }
114
}
115