1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpGitHooks\Module\JsonLint\Service; |
4
|
|
|
|
5
|
|
|
use PhpGitHooks\Module\Git\Contract\Response\BadJobLogoResponse; |
6
|
|
|
use PhpGitHooks\Module\Git\Service\PreCommitOutputWriter; |
7
|
|
|
use PhpGitHooks\Module\JsonLint\Contract\Exception\JsonLintViolationsException; |
8
|
|
|
use PhpGitHooks\Module\JsonLint\Model\JsonLintProcessorInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class JsonLintToolExecutor |
12
|
|
|
{ |
13
|
|
|
const CHECKING_MESSAGE = 'Checking json files'; |
14
|
|
|
/** |
15
|
|
|
* @var JsonLintProcessorInterface |
16
|
|
|
*/ |
17
|
|
|
private $jsonLintProcessor; |
18
|
|
|
/** |
19
|
|
|
* @var OutputInterface |
20
|
|
|
*/ |
21
|
|
|
private $output; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* JsonLintToolExecutor constructor. |
25
|
|
|
* |
26
|
|
|
* @param JsonLintProcessorInterface $jsonLintProcessor |
27
|
|
|
* @param OutputInterface $output |
28
|
|
|
*/ |
29
|
3 |
|
public function __construct(JsonLintProcessorInterface $jsonLintProcessor, OutputInterface $output) |
30
|
|
|
{ |
31
|
3 |
|
$this->jsonLintProcessor = $jsonLintProcessor; |
32
|
3 |
|
$this->output = $output; |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $files |
37
|
|
|
* @param string $errorMessage |
38
|
|
|
* |
39
|
|
|
* @throws JsonLintViolationsException |
40
|
|
|
*/ |
41
|
2 |
|
public function execute(array $files, $errorMessage) |
42
|
|
|
{ |
43
|
2 |
|
$outputMessage = new PreCommitOutputWriter(self::CHECKING_MESSAGE); |
44
|
2 |
|
$this->output->write($outputMessage->getMessage()); |
45
|
|
|
|
46
|
2 |
|
$errors = []; |
47
|
2 |
|
foreach ($files as $file) { |
48
|
2 |
|
$errors[] = $this->jsonLintProcessor->process($file); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
$errors = array_filter($errors); |
52
|
|
|
|
53
|
2 |
|
if (!empty($errors)) { |
54
|
1 |
|
$this->output->writeln($outputMessage->getFailMessage()); |
55
|
1 |
|
$this->output->writeln($outputMessage->setError(implode('', $errors))); |
56
|
1 |
|
$this->output->writeln(BadJobLogoResponse::paint($errorMessage)); |
57
|
1 |
|
throw new JsonLintViolationsException(implode('', $errors)); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->output->writeln($outputMessage->getSuccessfulMessage()); |
61
|
1 |
|
} |
62
|
|
|
} |
63
|
|
|
|