ChainedDecoderFileLoader::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
nc 4
nop 2
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
1
<?php namespace HSkrasek\OpenAPI\Loaders;
2
3
use League\JsonReference\Decoder\JsonDecoder;
4
use League\JsonReference\Decoder\YamlDecoder;
5
use League\JsonReference\DecoderInterface;
6
use League\JsonReference\DecodingException;
7
use League\JsonReference\LoaderInterface;
8
use League\JsonReference\SchemaLoadingException;
9
10
class ChainedDecoderFileLoader implements LoaderInterface
11
{
12
    /**
13
     * @var DecoderInterface
14
     */
15
    private $jsonDecoder;
16
17
    /**
18
     * @var DecoderInterface
19
     */
20
    private $yamlDecoder;
21
22
    public function __construct(DecoderInterface $jsonDecoder = null, DecoderInterface $yamlDecoder = null)
23
    {
24
        $this->jsonDecoder = $jsonDecoder ?: new JsonDecoder;
25
        $this->yamlDecoder = $yamlDecoder ?: new YamlDecoder;
26
    }
27
28
    /**
29
     * @inheritDoc
30
     */
31
    public function load($path)
32
    {
33
        $uri = 'file://' . $path;
34
35
        if (!file_exists($uri)) {
36
            throw SchemaLoadingException::notFound($uri);
37
        }
38
39
        $schema = file_get_contents($uri);
40
41
        try {
42
            return $this->jsonDecoder->decode($schema);
43
        } catch (DecodingException $e) {
44
            return $this->yamlDecoder->decode($schema);
45
        }
46
    }
47
}
48