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

FileGetContentsWebLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.004
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 15
ccs 9
cts 10
cp 0.9
crap 2.004
rs 9.4285
1
<?php
2
3
namespace League\JsonGuard\Loaders;
4
5
use League\JsonGuard;
6
use League\JsonGuard\Exceptions\SchemaLoadingException;
7
use League\JsonGuard\Loader;
8
9
class FileGetContentsWebLoader implements Loader
10
{
11
    /**
12
     * @var string
13
     */
14
    private $prefix;
15
16
    /**
17
     * @param string $prefix
18
     */
19 4
    public function __construct($prefix)
20
    {
21 4
        $this->prefix = $prefix;
22 4
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 4
    public function load($path)
28
    {
29 4
        $uri = $this->prefix . $path;
30 4
        set_error_handler(function () use ($uri) {
31 2
            throw new SchemaLoadingException($uri);
32 4
        });
33 4
        $response = file_get_contents($uri);
34 2
        restore_error_handler();
35
36 2
        if (!$response) {
37
            throw SchemaLoadingException::create($uri);
38
        }
39
40 2
        return JsonGuard\json_decode($response);
41
    }
42
}
43