YamlValidator::validate()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5
Metric Value
dl 0
loc 24
ccs 15
cts 15
cp 1
rs 8.5125
cc 5
eloc 12
nc 5
nop 2
crap 5
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\LaravelYaml\FileLoader\Yaml;
13
14
use Fidry\LaravelYaml\Exception\Configuration\InvalidArgumentException;
15
16
/**
17
 * @author Théo FIDRY <[email protected]>
18
 */
19
final class YamlValidator
20
{
21
    /**
22
     * Checks that the content returned by the YAML parser has an understandable definition.
23
     *
24
     * @param $content
25
     * @param $file
26
     *
27
     * @return mixed
28
     * @throws InvalidArgumentException
29
     */
30 18
    public function validate($content, $file)
31
    {
32 18
        if (null === $content) {
33 2
            return $content;
34
        }
35
36 16
        if (false === is_array($content)) {
37 2
            throw new InvalidArgumentException(
38 2
                sprintf('The service file "%s" is not valid. It should contain an array. Check your YAML syntax.', $file)
39 1
            );
40
        }
41
42 14
        foreach ($content as $namespace => $data) {
43 14
            if (in_array($namespace, ['imports', 'parameters', 'services'])) {
44 12
                continue;
45
            }
46
47 8
            throw new InvalidArgumentException(
48 8
                sprintf('Invalid namespace name "%s" in "%s".', $namespace, $file)
49 4
            );
50 3
        }
51
52 6
        return $content;
53
    }
54
}
55