FileGetContentsWebLoader::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
ccs 10
cts 10
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 FileGetContentsWebLoader implements LoaderInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $prefix;
16
17
    /**
18
     * @var DecoderInterface
19
     */
20
    private $jsonDecoder;
21
22
    /**
23
     * @param string               $prefix
24
     * @param DecoderInterface $jsonDecoder
25
     */
26 6
    public function __construct($prefix, DecoderInterface $jsonDecoder = null)
27
    {
28 6
        $this->prefix      = $prefix;
29 6
        $this->jsonDecoder = $jsonDecoder ?: new JsonDecoder();
30 6
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 6
    public function load($path)
36
    {
37 6
        $uri = $this->prefix . $path;
38 6
        set_error_handler(function () use ($uri) {
39 2
            throw SchemaLoadingException::create($uri);
40 6
        });
41 3
        $response = file_get_contents($uri);
42 2
        restore_error_handler();
43
44 2
        if (!$response) {
45 1
            throw SchemaLoadingException::create($uri);
46
        }
47
48 1
        return $this->jsonDecoder->decode($response);
49
    }
50
}
51