1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace NamespaceProtector\OutputDevice; |
4
|
|
|
|
5
|
|
|
use NamespaceProtector\Result\ErrorResult; |
6
|
|
|
use NamespaceProtector\Result\ResultInterface; |
7
|
|
|
use NamespaceProtector\Result\ResultProcessedFile; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use NamespaceProtector\Result\ResultProcessorInterface; |
10
|
|
|
|
11
|
|
|
final class ConsoleDevice implements OutputDeviceInterface |
12
|
|
|
{ |
13
|
|
|
/** @var OutputInterface */ |
14
|
|
|
private $outputInterface; |
15
|
|
|
|
16
|
|
|
/** @var int */ |
17
|
|
|
private $totalErrors; |
18
|
|
|
|
19
|
|
|
public function __construct(OutputInterface $outputInterface) |
20
|
|
|
{ |
21
|
|
|
$this->outputInterface = $outputInterface; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function output(ResultProcessorInterface $value): void |
25
|
|
|
{ |
26
|
|
|
$output = ''; |
27
|
|
|
$this->totalErrors = 0; |
28
|
|
|
foreach ($value->getProcessedResult() as $processedFileResult) { |
29
|
|
|
$output .= $this->plot($processedFileResult); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($output === '') { |
33
|
|
|
$this->outputInterface->writeln('<fg=blue>No output</>'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
echo $output; |
37
|
|
|
$this->outputInterface->writeln('<fg=red>Total files: ' . $value->getProcessedResult()->count() . '</>'); |
38
|
|
|
$this->outputInterface->writeln('<fg=red>Total errors: ' . $this->totalErrors . '</>'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function plot(ResultProcessedFile $processedFileResult): string |
42
|
|
|
{ |
43
|
|
|
$resultbuffer = ''; |
44
|
|
|
$resultTitle = "\nProcessed file: " . $processedFileResult->get() . "\n"; |
45
|
|
|
|
46
|
|
|
foreach ($processedFileResult->getConflicts() as $conflict) { |
47
|
|
|
$resultbuffer .= $this->plotResult($conflict); |
48
|
|
|
$this->totalErrors++; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($resultbuffer === '') { |
52
|
|
|
return $resultbuffer; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $resultTitle . $resultbuffer; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function plotResult(ResultInterface $result): string |
59
|
|
|
{ |
60
|
|
|
if ($result instanceof ErrorResult) { |
61
|
|
|
return \Safe\sprintf("\t > ERROR Line: %d of use %s ", $result->getLine(), $result->getUse()) . "\n"; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $result->get(); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|