ChainedDecoderFileLoader   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 38
rs 10
c 1
b 0
f 0
wmc 6
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 3
A load() 0 16 3
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