JsonLintProcessor::__construct()   A
last analyzed

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 1
1
<?php
2
3
namespace PhpGitHooks\Module\JsonLint\Infrastructure\Tool;
4
5
use PhpGitHooks\Infrastructure\Tool\ToolPathFinder;
6
use PhpGitHooks\Module\JsonLint\Model\JsonLintProcessorInterface;
7
use Symfony\Component\Process\Process;
8
9
class JsonLintProcessor implements JsonLintProcessorInterface
10
{
11
    /**
12
     * @var ToolPathFinder
13
     */
14
    private $toolPathFinder;
15
16
    /**
17
     * JsonLintProcessor constructor.
18
     *
19
     * @param ToolPathFinder $toolPathFinder
20
     */
21
    public function __construct(ToolPathFinder $toolPathFinder)
22
    {
23
        $this->toolPathFinder = $toolPathFinder;
24
    }
25
26
    /**
27
     * @param string $file
28
     *
29
     * @return string
30
     */
31
    public function process($file)
32
    {
33
        $process = $this->execute($file);
34
35
        return $this->setErrors($process);
36
    }
37
38
    /**
39
     * @param string $file
40
     *
41
     * @return Process
42
     */
43
    private function execute($file)
44
    {
45
        $process = new Process(
46
            [
47
                'php',
48
                $this->toolPathFinder->find('jsonlint'),
49
                $file,
50
            ]
51
        );
52
53
        $process->run();
54
55
        return $process;
56
    }
57
58
    /**
59
     * @param Process $process
60
     *
61
     * @return string
62
     */
63
    private function setErrors(Process $process)
64
    {
65
        if (false === $process->isSuccessful()) {
66
            return $process->getErrorOutput();
67
        }
68
    }
69
}
70