ValidatorService::validate()   C
last analyzed

Complexity

Conditions 8
Paths 28

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 8.004

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 41
ccs 24
cts 25
cp 0.96
rs 5.3846
cc 8
eloc 26
nc 28
nop 1
crap 8.004
1
<?php
2
3
namespace Decline\TransformatBundle\Services;
4
5
use DOMDocument;
6
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
7
8
/**
9
 * Class ValidatorService
10
 * @package Decline\TransformatBundle\Services
11
 */
12
class ValidatorService
13
{
14
15
    const MODE_STRICT = 'strict';
16
    const MODE_TRANSITIONAL = 'transitional';
17
    const MODE_DISABLED = 'false';
18
19
    /**
20
     * @var array
21
     */
22
    private $config;
23
24
    /**
25
     * @var string
26
     */
27
    private $schemaContent;
28
29
    /**
30
     * ValidatorService constructor.
31
     * @param array $config
32
     */
33 4
    public function __construct($config)
34
    {
35 4
        $this->config = $config;
36 4
    }
37
38
    /**
39
     * Validates the given xliff content with the configured validation mode
40
     *
41
     * @param string $xliffContent
42
     * @return \LibXMLError[]
43
     * @throws \Symfony\Component\Filesystem\Exception\FileNotFoundException
44
     */
45 5
    public function validate($xliffContent)
46
    {
47 5
        $errors = [];
48
49 5
        switch ($this->getValidationMode()) {
50 5
            case static::MODE_STRICT:
51 1
                $schema = 'xliff-core-1.2-strict.xsd';
52 1
                break;
53 5
            case static::MODE_TRANSITIONAL:
54 1
                $schema = 'xliff-core-1.2-transitional.xsd';
55 1
                break;
56 5
            case static::MODE_DISABLED:
57
            default:
58 5
                $schema = null;
59 5
                break;
60
        }
61
62 5
        if ($schema === null) {
63 5
            return $errors;
64
        }
65
66
        // retrieve schema (from file or previous validation calls)
67 1
        $this->schemaContent = $this->schemaContent ?: file_get_contents(__DIR__ . '/../Resources/schema/' . $schema);
68 1
        if ($this->schemaContent === null) {
69
            throw new FileNotFoundException('Schema could not be loaded!');
70
        }
71
72
        // validate xliff against schema
73 1
        libxml_use_internal_errors(true);
74 1
        $xmlDOM = new DOMDocument();
75 1
        $xmlDOM->loadXML($xliffContent);
76 1
        if (!$xmlDOM->schemaValidateSource($this->schemaContent)) {
77 1
            $errors = libxml_get_errors();
78 1
            libxml_clear_errors();
79
        }
80
81
        // free up some memory
82 1
        unset($xmlDOM);
83
84 1
        return $errors;
85
    }
86
    /**
87
     * Return the configured validation mode
88
     *
89
     * @return mixed
90
     */
91 5
    private function getValidationMode()
92
    {
93 5
        return $this->config['xliff']['validation'];
94
    }
95
}