Passed
Push — master ( 762e12...f9ab9d )
by MoshiMoshi
02:41
created

YamlFileLoader::loadFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the ConfigCacheBundle package.
5
 *
6
 * Copyright (c) 2015-2016 Yahoo Japan Corporation
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 YahooJapan\ConfigCacheBundle\ConfigCache\Loader;
13
14
use Symfony\Component\Yaml\Yaml;
15
16
/**
17
 * YamlFileLoader loads YAML files user configurations.
18
 */
19
class YamlFileLoader extends Loader
20
{
21
    /**
22
     * Returns true if this class supports the given resource.
23
     *
24
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
25
     * @param mixed  $resource A resource
26
     * @param string $type     The resource type
27
     *
28
     * @return bool true if this class supports the given resource, false otherwise
29
     */
30 9
    public function supports($resource, $type = null)
31
    {
32 9
        return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION);
33
    }
34
35
    /**
36
     * Loads internal.
37
     *
38
     * @param string $file
39
     *
40
     * @return array
41
     */
42 37
    protected function loadFile($file)
43
    {
44 37
        $this->validateArgument($file);
45
46
        try {
47 34
            $array = Yaml::parse(file_get_contents($file));
48 34
        } catch (\Exception $e) {
49 1
            throw new \Exception(sprintf("Yaml parse failed[%s]", $file));
50
        }
51
52 33
        foreach ($this->loaders as $loader) {
53 5
            $array = $loader->load($array);
54 33
        }
55
56 33
        return $array;
57
    }
58
59
    /**
60
     * Validates a file argument.
61
     *
62
     * @param string $file
63
     *
64
     * @throws \InvalidArgumentException
65
     */
66 37
    protected function validateArgument($file)
67
    {
68 37
        if (!stream_is_local($file)) {
69 1
            throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file));
70
        }
71
72 36
        if (!file_exists($file)) {
73 2
            throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file));
74
        }
75 34
    }
76
}
77