Passed
Pull Request — master (#15)
by Niels
01:40
created

SchemaLoader::getFileResource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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
use Symfony\Component\Config\Resource\FileResource;
18
use Symfony\Component\Config\Resource\ResourceInterface;
19
20
/**
21
 * Loads a dereferenced JSON schema.
22
 *
23
 * @author Niels Nijens <[email protected]>
24
 */
25
class SchemaLoader implements SchemaLoaderInterface
26
{
27
    /**
28
     * @var FileLocatorInterface
29
     */
30
    private $fileLocator;
31
32
    /**
33
     * @var DereferencerInterface
34
     */
35
    private $dereferencer;
36
37
    /**
38
     * The loaded JSON schemas.
39
     *
40
     * @var array
41
     */
42
    private $schemas = array();
43
44
    /**
45
     * Constructs a new SchemaLoader instance.
46
     *
47
     * @param FileLocatorInterface  $fileLocator
48
     * @param DereferencerInterface $dereferencer
49
     */
50
    public function __construct(FileLocatorInterface $fileLocator, DereferencerInterface $dereferencer)
51
    {
52
        $this->fileLocator = $fileLocator;
53
        $this->dereferencer = $dereferencer;
54
    }
55
56
    /**
57
     * {@inheritdoc}
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
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getFileResource(string $file): ?ResourceInterface
77
    {
78
        $locatedFile = $this->fileLocator->locate($file);
79
        if (isset($this->schemas[$locatedFile])) {
80
            return new FileResource($locatedFile);
0 ignored issues
show
Bug introduced by
It seems like $locatedFile can also be of type array; however, parameter $resource of Symfony\Component\Config...Resource::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

80
            return new FileResource(/** @scrutinizer ignore-type */ $locatedFile);
Loading history...
81
        }
82
83
        return null;
84
    }
85
}
86