Completed
Push — master ( 5927eb...1e13db )
by
unknown
04:04
created

ChainableLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace League\JsonGuard\Loaders;
4
5
use League\JsonGuard\Exceptions\SchemaLoadingException;
6
use League\JsonGuard\Loader;
7
8
/**
9
 * This loader takes two other loaders as constructor parameters, and will
10
 * attempt to load from the first loader before deferring to the second loader.
11
 * This is useful when you would like to use multiple loaders for the same prefix.
12
 */
13
class ChainableLoader implements Loader
14
{
15
    /**
16
     * @var Loader
17
     */
18
    private $firstLoader;
19
20
    /**
21
     * @var Loader
22
     */
23
    private $secondLoader;
24
25
    /**
26
     * @param \League\JsonGuard\Loader $firstLoader
27
     * @param \League\JsonGuard\Loader $secondLoader
28
     */
29 62
    public function __construct(Loader $firstLoader, Loader $secondLoader)
30
    {
31 62
        $this->firstLoader  = $firstLoader;
32 62
        $this->secondLoader = $secondLoader;
33 62
    }
34
35
    /**
36
     * Load the json schema from the given path.
37
     *
38
     * @param string $path The path to load, without the protocol.
39
     *
40
     * @return object The object resulting from a json_decode of the loaded path.
41
     */
42 8
    public function load($path)
43
    {
44
        try {
45 8
            return $this->firstLoader->load($path);
46 4
        } catch (SchemaLoadingException $e) {
47 4
            return $this->secondLoader->load($path);
48
        }
49
    }
50
}
51