Passed
Pull Request — master (#69)
by Viacheslav
02:25
created

Preloaded   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 89.29%

Importance

Changes 0
Metric Value
wmc 9
eloc 27
dl 0
loc 58
ccs 25
cts 28
cp 0.8929
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setSchemaFile() 0 4 1
A setSchemaData() 0 4 1
A __construct() 0 6 1
A populateSchemas() 0 15 3
A getSchemaData() 0 9 3
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 5
        } elseif (isset($this->schemaFiles[$url])) {
30 4
            $this->storage[$url] = json_decode(file_get_contents($this->schemaFiles[$url]));
31 4
            return $this->storage[$url];
32
        }
33 1
        return false;
34
    }
35
36
    /**
37
     * @param RefResolver $refResolver
38
     * @param Context|null $options
39
     * @throws \Swaggest\JsonSchema\Exception
40
     */
41 334
    public function populateSchemas(RefResolver $refResolver, Context $options = null)
42
    {
43 334
        if ($options === null) {
44 1
            $options = new Context();
45 1
            $options->refResolver = $refResolver;
46 1
            $options->version = Schema::VERSION_AUTO;
47
        }
48
49 334
        $prev = $refResolver->getResolutionScope();
50 334
        foreach ($this->storage as $url => $schemaData) {
51 334
            $refResolver->setupResolutionScope($url, $schemaData);
52 334
            $refResolver->setResolutionScope($url);
53 334
            $refResolver->preProcessReferences($schemaData, $options);
54
        }
55 334
        $refResolver->setResolutionScope($prev);
56 334
    }
57
58 17
    public function setSchemaData($url, $schemaData)
59
    {
60 17
        $this->storage[rtrim($url, '#')] = $schemaData;
61 17
        return $this;
62
    }
63
64
    public function setSchemaFile($url, $path)
65
    {
66
        $this->schemaFiles[rtrim($url, '#')] = $path;
67
        return $this;
68
    }
69
70
71
}