1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace YamlStandards\Model\YamlServiceAliasing; |
6
|
|
|
|
7
|
|
|
use SebastianBergmann\Diff\Differ; |
8
|
|
|
use YamlStandards\Command\ProcessOutput; |
9
|
|
|
use YamlStandards\Model\CheckerInterface; |
10
|
|
|
use YamlStandards\Model\Component\YamlService; |
11
|
|
|
use YamlStandards\Model\Config\StandardParametersData; |
12
|
|
|
use YamlStandards\Result\Result; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Check service file observe uniform aliasing |
16
|
|
|
*/ |
17
|
|
|
class YamlServiceAliasingChecker implements CheckerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @inheritDoc |
21
|
|
|
*/ |
22
|
5 |
|
public function check(string $pathToYamlFile, StandardParametersData $standardParametersData): Result |
23
|
|
|
{ |
24
|
5 |
|
$yamlContent = file_get_contents($pathToYamlFile); |
25
|
5 |
|
$yamlContent = str_replace("\r", '', $yamlContent); // remove carriage returns |
26
|
5 |
|
$yamlLines = explode("\n", $yamlContent); |
27
|
|
|
|
28
|
5 |
|
if (YamlServiceAliasingDataFactory::existsServicesInHighestParent($yamlLines) === false) { |
29
|
1 |
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK); |
30
|
|
|
} |
31
|
|
|
|
32
|
4 |
|
$correctYamlLines = YamlServiceAliasingDataFactory::getCorrectYamlLines($yamlLines, YamlService::getYamlData($pathToYamlFile), $standardParametersData); |
33
|
4 |
|
$correctYamlContent = implode("\n", $correctYamlLines); |
34
|
|
|
|
35
|
4 |
|
if ($yamlContent === $correctYamlContent) { |
36
|
3 |
|
return new Result($pathToYamlFile, Result::RESULT_CODE_OK, ProcessOutput::STATUS_CODE_OK); |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
$differ = new Differ(); |
40
|
1 |
|
$diffBetweenStrings = $differ->diff($yamlContent, $correctYamlContent); |
41
|
|
|
|
42
|
1 |
|
return new Result($pathToYamlFile, Result::RESULT_CODE_INVALID_FILE_SYNTAX, ProcessOutput::STATUS_CODE_INVALID_FILE_SYNTAX, $diffBetweenStrings, true); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|