Completed
Pull Request — master (#12)
by Théo
06:24 queued 03:54
created

YamlSingleFileLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A loadFile() 0 20 4
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\FileLoader\InvalidArgumentException;
15
use Symfony\Component\Yaml\Exception\ParseException;
16
use Symfony\Component\Yaml\Parser as YamlParser;
17
18
/**
19
 * This loader is able to load YAML files. Parsed values are interpreted and added to the {@see ContainerBuilder} to be
20
 * loaded to the Application later on.
21
 *
22
 * @author Théo FIDRY <[email protected]>
23
 */
24
final class YamlSingleFileLoader
25
{
26
    /**
27
     * @var YamlParser
28
     */
29
    private $yamlParser;
30
31
    /**
32
     * @var YamlValidator
33
     */
34
    private $yamlValidator;
35
36
    public function __construct(YamlParser $yamlParser, YamlValidator $yamlValidator)
37
    {
38
        $this->yamlParser = $yamlParser;
39
        $this->yamlValidator = $yamlValidator;
40
    }
41
42
    /**
43
     * @param string $filePath
44
     *
45
     * @return array The file content
46
     * @throws InvalidArgumentException
47
     */
48
    public function loadFile($filePath)
49
    {
50
        if (false === stream_is_local($filePath)) {
51
            throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $filePath));
52
        }
53
54
        if (false === file_exists($filePath)) {
55
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $filePath));
56
        }
57
58
        try {
59
            $configuration = $this->yamlParser->parse(file_get_contents($filePath));
60
        } catch (ParseException $exception) {
61
            throw new InvalidArgumentException(
62
                sprintf('The file "%s" does not contain valid YAML.', $filePath), 0, $exception
63
            );
64
        }
65
66
        return $this->yamlValidator->validate($configuration, $filePath);
67
    }
68
}
69