Passed
Pull Request — master (#921)
by
unknown
08:01
created

Config::validInputData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 13
ccs 10
cts 10
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * Validation of config
5
 *
6
 * @category  NFePHP
7
 * @package   NFePHP\NFe\Common\Config
8
 * @copyright NFePHP Copyright (c) 2008-2019
9
 * @license   http://www.gnu.org/licenses/lgpl.txt LGPLv3+
10
 * @license   https://opensource.org/licenses/MIT MIT
11
 * @license   http://www.gnu.org/licenses/gpl.txt GPLv3+
12
 * @author    Roberto L. Machado <linux.rlm at gmail dot com>
13
 * @link      http://github.com/nfephp-org/sped-nfe for the canonical source repository
14
 */
15
16
namespace NFePHP\NFe\Common;
17
18
use JsonSchema\Validator as JsonValid;
19
use NFePHP\NFe\Exception\DocumentsException;
20
21
class Config
22
{
23
    /**
24
     * Validate method
25
     * @param string $content config.json
26
     * @return \stdClass
27
     */
28 76
    public static function validate($content)
29
    {
30 76
        if (!is_string($content)) {
0 ignored issues
show
introduced by
The condition is_string($content) is always true.
Loading history...
31 1
            throw DocumentsException::wrongDocument(8, "Não foi passado um json.");
32
        }
33 75
        $std = json_decode($content);
34 75
        if (!is_object($std)) {
35 1
            throw DocumentsException::wrongDocument(8, "Não foi passado um json valido.");
36
        }
37 74
        self::validInputData($std);
38 68
        return $std;
39
    }
40
41
    /**
42
     * Validation with JsonValid::class
43
     * @param object $data
44
     * @return boolean
45
     * @throws DocumentsException
46
     */
47 74
    protected static function validInputData($data)
48
    {
49 74
        $filejsonschema = __DIR__ . "/../../storage/config.schema";
50 74
        $validator = new JsonValid();
51 74
        $validator->check($data, (object)['$ref' => 'file://' . $filejsonschema]);
52 74
        if (!$validator->isValid()) {
53 6
            $msg = "";
54 6
            foreach ($validator->getErrors() as $error) {
55 6
                $msg .= sprintf("[%s] %s\n", $error['property'], $error['message']);
56
            }
57 6
            throw DocumentsException::wrongDocument(8, $msg);
58
        }
59 68
        return true;
60
    }
61
}
62