ArrayLoader   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 3

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 3
dl 0
loc 43
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A load() 0 16 4
1
<?php
2
3
namespace League\JsonReference\Loader;
4
5
use League\JsonReference\Decoder\JsonDecoder;
6
use League\JsonReference\DecoderInterface;
7
use League\JsonReference\LoaderInterface;
8
use League\JsonReference\SchemaLoadingException;
9
10
final class ArrayLoader implements LoaderInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $schemas;
16
17
    /**
18
     * @var DecoderInterface
19
     */
20
    private $jsonDecoder;
21
22
    /**
23
     * @param array                $schemas A map of schemas where path => schema.The schema should be a string or the
24
     *                                      object resulting from a json_decode call.
25
     * @param DecoderInterface $jsonDecoder
26
     */
27 18
    public function __construct(array $schemas, DecoderInterface $jsonDecoder = null)
28
    {
29 18
        $this->schemas     = $schemas;
30 18
        $this->jsonDecoder = $jsonDecoder ?: new JsonDecoder();
31 18
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 12
    public function load($path)
37
    {
38 12
        if (!array_key_exists($path, $this->schemas)) {
39 4
            throw SchemaLoadingException::notFound($path);
40
        }
41
42 10
        $schema = $this->schemas[$path];
43
44 10
        if (is_string($schema)) {
45 4
            return $this->jsonDecoder->decode($schema);
46 8
        } elseif (is_object($schema)) {
47 6
            return $schema;
48
        } else {
49 2
            throw SchemaLoadingException::create($path);
50
        }
51
    }
52
}
53