SchemaLoader::load()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the OpenapiBundle package.
5
 *
6
 * (c) Niels Nijens <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Nijens\OpenapiBundle\Json;
13
14
use Nijens\OpenapiBundle\Json\Loader\LoaderInterface;
15
use stdClass;
16
use Symfony\Component\Config\Resource\FileResource;
17
use Symfony\Component\Config\Resource\ResourceInterface;
18
19
/**
20
 * Loads a dereferenced JSON schema.
21
 *
22
 * @author Niels Nijens <[email protected]>
23
 */
24
class SchemaLoader implements SchemaLoaderInterface
25
{
26
    /**
27
     * @var LoaderInterface
28
     */
29
    private $loader;
30
31
    /**
32
     * @var DereferencerInterface
33
     */
34
    private $dereferencer;
35
36
    /**
37
     * The loaded JSON schemas.
38
     *
39
     * @var array
40
     */
41
    private $schemas = [];
42
43
    /**
44
     * Constructs a new {@see SchemaLoader} instance.
45
     */
46
    public function __construct(LoaderInterface $loader, DereferencerInterface $dereferencer)
47
    {
48
        $this->loader = $loader;
49
        $this->dereferencer = $dereferencer;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function load(string $file): stdClass
56
    {
57
        if (isset($this->schemas[$file]) === false) {
58
            $schema = $this->loader->load($file);
59
            $dereferencedSchema = $this->dereferencer->dereference($schema);
60
61
            $this->schemas[$file] = $dereferencedSchema;
62
        }
63
64
        return $this->schemas[$file];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function getFileResource(string $file): ?ResourceInterface
71
    {
72
        if (isset($this->schemas[$file])) {
73
            return new FileResource($file);
74
        }
75
76
        return null;
77
    }
78
}
79