Preloaded   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
wmc 10
eloc 30
dl 0
loc 62
ccs 24
cts 28
cp 0.8571
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A populateSchemas() 0 15 3
A setSchemaFile() 0 4 1
A setSchemaData() 0 4 1
A getSchemaData() 0 13 4
1
<?php
2
3
namespace Swaggest\JsonSchema\RemoteRef;
4
5
use Swaggest\JsonSchema\Context;
6
use Swaggest\JsonSchema\RefResolver;
7
use Swaggest\JsonSchema\RemoteRefProvider;
8
use Swaggest\JsonSchema\Schema;
9
10
class Preloaded implements RemoteRefProvider
11
{
12
    private $storage;
13
14
    private $schemaFiles;
15
16 23
    public function __construct()
17
    {
18 23
        $this->schemaFiles = array(
19
            'http://json-schema.org/draft-04/schema' => __DIR__ . '/../../spec/json-schema.json',
20
            'http://json-schema.org/draft-06/schema' => __DIR__ . '/../../spec/json-schema-draft6.json',
21
            'http://json-schema.org/draft-07/schema' => __DIR__ . '/../../spec/json-schema-draft7.json',
22
        );
23 23
    }
24
25 123
    public function getSchemaData($url)
26
    {
27 123
        if (isset($this->storage[$url])) {
28 120
            return $this->storage[$url];
29 4
        } elseif (isset($this->schemaFiles[$url])) {
30 4
            if ($data = file_get_contents($this->schemaFiles[$url])) {
31 4
                $this->storage[$url] = json_decode($data);
32
            } else {
33
                return false;
34
            }
35
            return $this->storage[$url];
36
        }
37
        return false;
38
    }
39
40
    /**
41 334
     * @param RefResolver $refResolver
42
     * @param Context|null $options
43 334
     * @throws \Swaggest\JsonSchema\Exception
44 1
     */
45 1
    public function populateSchemas(RefResolver $refResolver, Context $options = null)
46 1
    {
47
        if ($options === null) {
48
            $options = new Context();
49 334
            $options->refResolver = $refResolver;
50 334
            $options->version = Schema::VERSION_AUTO;
51 334
        }
52 334
53 334
        $prev = $refResolver->getResolutionScope();
54
        foreach ($this->storage as $url => $schemaData) {
55 334
            $refResolver->setupResolutionScope($url, $schemaData);
56 334
            $refResolver->setResolutionScope($url);
57
            $refResolver->preProcessReferences($schemaData, $options);
58 17
        }
59
        $refResolver->setResolutionScope($prev);
60 17
    }
61 17
62
    public function setSchemaData($url, $schemaData)
63
    {
64
        $this->storage[rtrim($url, '#')] = $schemaData;
65
        return $this;
66
    }
67
68
    public function setSchemaFile($url, $path)
69
    {
70
        $this->schemaFiles[rtrim($url, '#')] = $path;
71
        return $this;
72
    }
73
74
75
}