ArrayLoader::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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