Completed
Push — master ( b209b1...84fcc2 )
by Peter
05:42
created

YamlServiceAliasingChecker   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 13
c 1
b 0
f 0
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A check() 0 21 3
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