YamlFileLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 24
rs 10
c 2
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 11 3
A supports() 0 3 4
1
<?php
2
3
/*
4
 * This file is part of the awurth/config package.
5
 *
6
 * (c) Alexis Wurth <[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 AWurth\Config\Loader;
13
14
use LogicException;
15
use Symfony\Component\Config\Loader\Loader;
16
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
17
use Symfony\Component\Yaml\Yaml;
18
19
/**
20
 * YAML File Loader.
21
 *
22
 * @author Alexis Wurth <[email protected]>
23
 */
24
class YamlFileLoader extends Loader
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function load($file, $type = null)
30
    {
31
        if (!file_exists($file)) {
32
            throw new FileNotFoundException(sprintf('File "%s" not found.', $file));
33
        }
34
35
        if (!class_exists('Symfony\Component\Yaml\Yaml')) {
36
            throw new LogicException('Loading files from the YAML format requires the Symfony Yaml component.');
37
        }
38
39
        return Yaml::parse(file_get_contents($file), Yaml::PARSE_CONSTANT | Yaml::PARSE_DATETIME | Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function supports($resource, $type = null)
46
    {
47
        return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), ['yml', 'yaml'], true) && (!$type || 'yaml' === $type);
48
    }
49
}
50