Completed
Push — master ( 8f75b3...70c410 )
by Théo
10s
created

YamlSingleFileLoader::loadFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
ccs 10
cts 11
cp 0.9091
rs 9.2
cc 4
eloc 11
nc 4
nop 1
crap 4.0119
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
 * @author Théo FIDRY <[email protected]>
20
 */
21
final class YamlSingleFileLoader
22
{
23
    /**
24
     * @var YamlParser
25
     */
26
    private $yamlParser;
27
28
    /**
29
     * @var YamlValidator
30
     */
31
    private $yamlValidator;
32
33 10
    public function __construct(YamlParser $yamlParser, YamlValidator $yamlValidator)
34
    {
35 10
        $this->yamlParser = $yamlParser;
36 10
        $this->yamlValidator = $yamlValidator;
37 10
    }
38
39
    /**
40
     * @param string $filePath
41
     *
42
     * @return array The file content
43
     * @throws InvalidArgumentException
44
     */
45 8
    public function loadFile($filePath)
46
    {
47 8
        if (false === stream_is_local($filePath)) {
48
            throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $filePath));
49
        }
50
51 8
        if (false === file_exists($filePath)) {
52 2
            throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $filePath));
53
        }
54
55
        try {
56 6
            $configuration = $this->yamlParser->parse(file_get_contents($filePath));
57 4
        } catch (ParseException $exception) {
58 2
            throw new InvalidArgumentException(
59 2
                sprintf('The file "%s" does not contain valid YAML.', $filePath), 0, $exception
60 1
            );
61
        }
62
63 4
        return $this->yamlValidator->validate($configuration, $filePath);
64
    }
65
}
66