XmlValidator::decode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 10
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Validator;
17
18
use Hogosha\Monitor\Exception\ValidatorException;
19
use Hogosha\Monitor\Utils\Dom;
20
21
/**
22
 * Class XmlValidator.
23
 */
24
class XmlValidator implements ValidatorInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function check($value, $match)
30
    {
31
        try {
32
            $xml = $this->decode($value);
33
        } catch (\DomException $e) {
34
            throw new ValidatorException($e->getMessage());
35
        }
36
37
        if (0 === $xml->xpath($match)->length) {
38
            throw new ValidatorException(sprintf('this node "%s" does not exist', $match));
39
        }
40
    }
41
42
    /**
43
     * decode.
44
     *
45
     * @param string $content
46
     *
47
     * @throws ValidatorException
48
     *
49
     * @return \DomDocument
50
     */
51
    private function decode($content)
52
    {
53
        try {
54
            $result = new Dom($content);
55
        } catch (\DomException $e) {
56
            throw new ValidatorException('This xml is not valid');
57
        }
58
59
        return $result;
60
    }
61
}
62