FileGetContentsWebLoader   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 3

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 41
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A load() 0 15 2
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