Completed
Pull Request — master (#14)
by
unknown
06:16
created

FileGetContentsWebLoader::__construct()   A

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 Activerules\JsonReference\Loader;
4
5
use Activerules\JsonReference\JsonDecoder\JsonDecoder;
6
use Activerules\JsonReference\JsonDecoderInterface;
7
use Activerules\JsonReference\LoaderInterface;
8
use Activerules\JsonReference\SchemaLoadingException;
9
10
final class FileGetContentsWebLoader implements LoaderInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $prefix;
16
17
    /**
18
     * @var JsonDecoderInterface
19
     */
20
    private $jsonDecoder;
21
22
    /**
23
     * @param string               $prefix
24
     * @param JsonDecoderInterface $jsonDecoder
25
     */
26 6
    public function __construct($prefix, JsonDecoderInterface $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