Completed
Pull Request — master (#196)
by Andreas
01:43
created

ProcessManager::completeRunningProcess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Managers;
4
5
use Churn\Collections\FileCollection;
6
use Churn\Factories\ProcessFactory;
7
use Churn\Processes\ChurnProcess;
8
use Illuminate\Support\Collection;
9
10
class ProcessManager
11
{
12
    /**
13
     * Collection of running processes.
14
     * @var Collection
15
     */
16
    private $runningProcesses;
17
18
    /**
19
     * Collection of files.
20
     * @var FileCollection
21
     */
22
    private $filesCollection;
23
24
    /**
25
     * Array of completed processes.
26
     * @var array
27
     */
28
    private $completedProcessesArray;
29
30
    /**
31
     * Process Factory.
32
     * @var ProcessFactory
33
     */
34
    private $processFactory;
35
36
    /**
37
     * Run the processes to gather information.
38
     * @param FileCollection $filesCollection      Collection of files.
39
     * @param ProcessFactory $processFactory       Process Factory.
40
     * @param integer        $numberOfParallelJobs Number of parallel jobs to run.
41
     * @return Collection
42
     */
43
    public function process(
44
        FileCollection $filesCollection,
45
        ProcessFactory $processFactory,
46
        int $numberOfParallelJobs
47
    ): Collection {
48
        $this->filesCollection = $filesCollection;
49
        $this->processFactory = $processFactory;
50
        $this->runningProcesses = new Collection;
51
        $this->completedProcessesArray = [];
52
        while ($filesCollection->hasFiles() || $this->runningProcesses->count()) {
53
            $this->getProcessResults($numberOfParallelJobs);
54
        }
55
        return new Collection($this->completedProcessesArray);
56
    }
57
58
    /**
59
     * Get the results of the processes.
60
     * @param integer $numberOfParallelJobs Number of parallel jobs to run.
61
     * @return void
62
     */
63
    private function getProcessResults(int $numberOfParallelJobs): void
64
    {
65
        for ($index = $this->runningProcesses->count();
66
             $this->filesCollection->hasFiles() > 0 && $index < $numberOfParallelJobs;
67
             $index++) {
68
            $file = $this->filesCollection->getNextFile();
69
70
            $process = $this->processFactory->createGitCommitProcess($file);
71
            $process->start();
72
            $this->runningProcesses->put($process->getKey(), $process);
73
            $process = $this->processFactory->createCyclomaticComplexityProcess($file);
74
            $process->start();
75
            $this->runningProcesses->put($process->getKey(), $process);
76
        }
77
78
        foreach ($this->runningProcesses as $file => $process) {
79
            $this->completeRunningProcess($process);
80
        }
81
    }
82
83
    /**
84
     * Completes a running churn process when it was successful.
85
     * @param ChurnProcess $process The churn process.
86
     * @return void
87
     */
88
    private function completeRunningProcess(ChurnProcess $process): void
89
    {
90
        if (!$process->isSuccessful()) {
91
            return;
92
        }
93
94
        $this->runningProcesses->forget($process->getKey());
95
        $this->completedProcessesArray[$process->getFileName()][$process->getType()] = $process;
96
    }
97
}
98