Completed
Pull Request — master (#210)
by
unknown
02:24 queued 36s
created

ProcessManager::setProgressBarMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Churn\Managers;
4
5
use Churn\Collections\FileCollection;
6
use Churn\Factories\ProcessFactory;
7
use Symfony\Component\Console\Helper\ProgressBar;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Tightenco\Collect\Support\Collection;
10
11
class ProcessManager
12
{
13
    /**
14
     * Collection of running processes.
15
     * @var Collection
16
     */
17
    private $runningProcesses;
18
19
    /**
20
     * Collection of files.
21
     * @var FileCollection
22
     */
23
    private $filesCollection;
24
25
    /**
26
     * Array of completed processes.
27
     * @var array
28
     */
29
    private $completedProcessesArray;
30
31
    /**
32
     * Process Factory.
33
     * @var ProcessFactory
34
     */
35
    private $processFactory;
36
37
    /**
38
     * Total count of files needed to be processed.
39
     * @var integer
40
     */
41
    private $totalFilesToProcessCount = 0;
42
43
    /**
44
     * To show or not progress bar while processing files.
45
     * @var boolean
46
     */
47
    private $showProgressBar = false;
48
49
    /**
50
     * Symfony progress bar
51
     * @var ProgressBar
52
     */
53
    private $progressBar;
54
55
    /**
56
     * Symfony output.
57
     *
58
     * @var OutputInterface
59
     */
60
    private $output;
61
62
    /**
63
     * Run the processes to gather information.
64
     * @param FileCollection $filesCollection      Collection of files.
65
     * @param ProcessFactory $processFactory       Process Factory.
66
     * @param integer        $numberOfParallelJobs Number of parallel jobs to run.
67
     * @return Collection
68
     */
69
    public function process(
70
        FileCollection $filesCollection,
71
        ProcessFactory $processFactory,
72
        int $numberOfParallelJobs
73
    ): Collection {
74
        $this->filesCollection = $filesCollection;
75
        $this->processFactory = $processFactory;
76
        $this->runningProcesses = new Collection;
77
        $this->completedProcessesArray = [];
78
        $this->totalFilesToProcessCount = $this->filesCollection->count();
79
80
        if ($this->showProgressBar && $this->output) {
81
            $this->progressBar = new ProgressBar($this->output, 100);
82
            ProgressBar::setFormatDefinition('custom', '[%bar%] %percent:3s%% -- %message%');
83
            $this->progressBar->setFormat('custom');
84
            $this->output->write("Progress:".PHP_EOL);
85
            $this->setProgressBarMessage();
86
            $this->progressBar->start();
87
        }
88
89
        while ($filesCollection->hasFiles() || $this->runningProcesses->count()) {
90
            $this->getProcessResults($numberOfParallelJobs);
91
        }
92
93
        if ($this->showProgressBar) {
94
            $this->progressBar->finish();
95
        }
96
97
        return new Collection($this->completedProcessesArray);
98
    }
99
100
    /**
101
     * Returns count of currently processed files.
102
     *
103
     * @return integer
104
     */
105
    private function getCompletedProcessesCount(): int
106
    {
107
        return count($this->completedProcessesArray);
108
    }
109
110
    /**
111
     * Get the results of the processes.
112
     * @param integer $numberOfParallelJobs Number of parallel jobs to run.
113
     * @return void
114
     */
115
    private function getProcessResults(int $numberOfParallelJobs): void
116
    {
117
        for ($index = $this->runningProcesses->count();
118
             $this->filesCollection->hasFiles() > 0 && $index < $numberOfParallelJobs;
119
             $index++) {
120
            $file = $this->filesCollection->getNextFile();
121
122
            $process = $this->processFactory->createGitCommitProcess($file);
123
            $process->start();
124
            $this->runningProcesses->put($process->getKey(), $process);
125
            $process = $this->processFactory->createCyclomaticComplexityProcess($file);
126
            $process->start();
127
            $this->runningProcesses->put($process->getKey(), $process);
128
        }
129
130
        foreach ($this->runningProcesses as $file => $process) {
131
            if (!$process->isSuccessful()) {
132
                continue;
133
            }
134
135
            $this->runningProcesses->forget($process->getKey());
136
            $this->completedProcessesArray[$process->getFileName()][$process->getType()] = $process;
137
138
            $this->updateProgressBar();
139
        }
140
    }
141
142
    /**
143
     * Sets progress message for progress bar
144
     *
145
     * @return void
146
     */
147
    private function setProgressBarMessage(): void
148
    {
149
        $this->progressBar->setMessage(sprintf("Files: %d of %d", $this->getCompletedProcessesCount(), $this->totalFilesToProcessCount));
150
    }
151
152
    /**
153
     * @param bool $flag Enable or disable progress bar output.
154
     *
155
     * @return void
156
     */
157
    public function setProgressBarEnabled(bool $flag): void
158
    {
159
        $this->showProgressBar = $flag;
160
    }
161
162
    /**
163
     * @param OutputInterface $output Symfony output.
164
     *
165
     * @return void
166
     */
167
    public function setOutputStream(OutputInterface $output): void
168
    {
169
        $this->output = $output;
170
    }
171
172
    /**
173
     * Updated and prints progress bar if needed.
174
     *
175
     * @return void
176
     */
177
    private function updateProgressBar(): void
178
    {
179
        if (!$this->showProgressBar) {
180
            return;
181
        }
182
183
        $currentStep = intval(floor(($this->getCompletedProcessesCount() / $this->totalFilesToProcessCount) * 100));
184
        $this->setProgressBarMessage();
185
        $this->progressBar->setProgress($currentStep);
186
    }
187
}
188