JsonLintToolExecutor   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 0
loc 52
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 21 3
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