Completed
Pull Request — master (#74)
by Marcel
02:43
created

ProgressScanner::addJob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
crap 2
1
<?php
2
3
namespace PHPSemVerChecker\Scanner;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
class ProgressScanner
9
{
10
	/**
11
	 * @var string[][]
12
	 */
13
	private $fileLists = [];
14
	/**
15
	 * @var Scanner[]
16
	 */
17
	private $scanners = [];
18
	/**
19
	 * @var OutputInterface
20
	 */
21
	private $output;
22
	/**
23
	 * @var ProgressBar
24
	 */
25
	private $progressBar;
26
27
	public function __construct(OutputInterface $output)
28
	{
29
		$this->output = $output;
30
	}
31
32
	/**
33
	 * @param string $name
34
	 * @param string[] $fileList
35
	 * @param Scanner $scanner
36
	 */
37
	public function addJob($name, $fileList, $scanner)
38
	{
39
		$this->fileLists[$name] = $fileList;
40
		$this->scanners[$name] = $scanner;
41
	}
42
43
	public function runJobs()
44
	{
45
		foreach (array_keys($this->scanners) as $jobName) {
46
			$this->runJob($jobName);
47
		}
48
	}
49
50
	/**
51
	 * @param $jobName
52
	 */
53
	public function runJob($jobName)
54
	{
55
		$progress = $this->getProgessBar();
56
		$progress->setMessage('Scanning ' . $jobName);
57
		$scanner = $this->scanners[$jobName];
58
		foreach ($this->fileLists[$jobName] as $filePath) {
59
			$scanner->scan($filePath);
60
			$progress->advance();
61
		}
62
		if ($progress->getProgress() === $progress->getMaxSteps()) {
63
			$progress->clear();
64
		}
65
	}
66
67
	/**
68
	 * @return int
69
	 */
70
	private function getFileCount()
71
	{
72
		return array_sum(array_map('count', $this->fileLists));
73
	}
74
75
	/**
76
	 * @return ProgressBar
77
	 */
78
	private function getProgessBar()
79
	{
80
		if ($this->progressBar === null) {
81
			$this->progressBar = new ProgressBar($this->output, $this->getFileCount());
82
			$this->progressBar->setFormat("%message%\n%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%");
83
			$this->output->writeln('');
84
		}
85
		return $this->progressBar;
86
	}
87
}