JsonLoader   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 9
c 1
b 0
f 0
dl 0
loc 30
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A supports() 0 3 1
A load() 0 12 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the OpenapiBundle package.
7
 *
8
 * (c) Niels Nijens <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Nijens\OpenapiBundle\Json\Loader;
15
16
use Nijens\OpenapiBundle\Json\Exception\LoaderLoadException;
17
use stdClass;
18
19
/**
20
 * Loads JSON schema files in JSON format.
21
 *
22
 * @author Niels Nijens <[email protected]>
23
 */
24
final class JsonLoader implements LoaderInterface
25
{
26
    /**
27
     * The options used for decoding the JSON.
28
     */
29
    private const DECODE_OPTIONS = JSON_BIGINT_AS_STRING;
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function supports(string $file): bool
35
    {
36
        return pathinfo($file, PATHINFO_EXTENSION) === 'json';
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function load(string $file): stdClass
43
    {
44
        if (file_exists($file) === false) {
45
            throw new LoaderLoadException(sprintf('The JSON schema "%s" could not be found.', $file));
46
        }
47
48
        $json = json_decode(file_get_contents($file), false, 512, self::DECODE_OPTIONS);
49
        if (json_last_error() !== JSON_ERROR_NONE) {
50
            throw new LoaderLoadException(sprintf('The JSON schema "%s" contains invalid JSON: %s', $file, json_last_error_msg()));
51
        }
52
53
        return $json;
54
    }
55
}
56