YamlValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 1
dl 0
loc 36
ccs 15
cts 15
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B validate() 0 24 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