Completed
Push — master ( 56a710...3d2ac0 )
by Niels
12s
created

SchemaLoader::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
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 League\JsonReference\DereferencerInterface;
15
use stdClass;
16
use Symfony\Component\Config\FileLocatorInterface;
17
18
/**
19
 * Loads a dereferenced JSON schema.
20
 *
21
 * @author Niels Nijens <[email protected]>
22
 */
23
class SchemaLoader implements SchemaLoaderInterface
24
{
25
    /**
26
     * @var FileLocatorInterface
27
     */
28
    private $fileLocator;
29
30
    /**
31
     * @var DereferencerInterface
32
     */
33
    private $dereferencer;
34
35
    /**
36
     * The loaded JSON schemas.
37
     *
38
     * @var array
39
     */
40
    private $schemas = array();
41
42
    /**
43
     * Constructs a new SchemaLoader instance.
44
     *
45
     * @param FileLocatorInterface  $fileLocator
46
     * @param DereferencerInterface $dereferencer
47
     */
48
    public function __construct(FileLocatorInterface $fileLocator, DereferencerInterface $dereferencer)
49
    {
50
        $this->fileLocator = $fileLocator;
51
        $this->dereferencer = $dereferencer;
52
    }
53
54
    /**
55
     * @param string $file
56
     *
57
     * @return stdClass
58
     */
59
    public function load(string $file): stdClass
60
    {
61
        $locatedFile = $this->fileLocator->locate($file);
62
63
        if (isset($this->schemas[$locatedFile]) === false) {
64
            $schema = $this->dereferencer->dereference('file://'.$locatedFile);
0 ignored issues
show
Bug introduced by
Are you sure $locatedFile of type array|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

64
            $schema = $this->dereferencer->dereference('file://'./** @scrutinizer ignore-type */ $locatedFile);
Loading history...
65
            $dereferencedSchema = json_decode(json_encode($schema));
66
67
            $this->schemas[$locatedFile] = $dereferencedSchema;
68
        }
69
70
        return $this->schemas[$locatedFile];
71
    }
72
}
73