Completed
Push — master ( 7195f8...c1e355 )
by Bill
10s
created

ProcessManager::process()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 3
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Managers;
4
5
use Churn\Collections\FileCollection;
6
use Churn\Factories\ProcessFactory;
7
use Illuminate\Support\Collection;
8
9
class ProcessManager
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
     * Run the processes to gather information.
31
     * @param FileCollection $filesCollection      Collection of files.
32
     * @param ProcessFactory $processFactory       Process Factory.
33
     * @param integer        $numberOfParallelJobs Number of parallel jobs to run.
34
     * @return Collection
35
     */
36
    public function process(FileCollection $filesCollection, ProcessFactory $processFactory, int $numberOfParallelJobs): Collection
37
    {
38
        $this->filesCollection = $filesCollection;
39
        $this->processFactory = $processFactory;
0 ignored issues
show
Bug introduced by
The property processFactory does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
        $this->runningProcesses = new Collection;
41
        $this->completedProcessesArray = [];
42
        while ($filesCollection->hasFiles() || $this->runningProcesses->count()) {
43
            $this->getProcessResults($numberOfParallelJobs);
44
        }
45
        return new Collection($this->completedProcessesArray);
46
    }
47
48
    /**
49
     * Get the results of the processes.
50
     * @param integer $numberOfParallelJobs Number of parallel jobs to run.
51
     * @return void
52
     */
53
    private function getProcessResults(int $numberOfParallelJobs)
54
    {
55
        for ($index = $this->runningProcesses->count(); $this->filesCollection->hasFiles() > 0 && $index < $numberOfParallelJobs; $index++) {
56
            $file = $this->filesCollection->getNextFile();
57
58
            $process = $this->processFactory->createGitCommitProcess($file);
59
            $process->start();
60
            $this->runningProcesses->put($process->getKey(), $process);
61
62
            $process = $this->processFactory->createCyclomaticComplexityProcess($file);
63
            $process->start();
64
            $this->runningProcesses->put($process->getKey(), $process);
65
        }
66
67
        foreach ($this->runningProcesses as $file => $process) {
68
            if ($process->isSuccessful()) {
69
                $this->runningProcesses->forget($process->getKey());
70
                $this->completedProcessesArray[$process->getFileName()][$process->getType()] = $process;
71
            }
72
        }
73
    }
74
}
75