Completed
Pull Request — master (#25)
by
unknown
04:30
created

JSONLoader::loadUri()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 13
cp 0
rs 9.568
c 0
b 0
f 0
cc 2
nc 4
nop 1
crap 6
1
<?php
2
/**
3
 * Bright Nucleus Config Component.
4
 *
5
 * @package   BrightNucleus\Config
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   MIT
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2016-2017 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Config\Loader;
13
14
use BrightNucleus\Config\Exception\FailedToLoadConfigException;
15
use Exception;
16
17
/**
18
 * Class JSONLoader.
19
 *
20
 * @since   0.1.0
21
 *
22
 * @package BrightNucleus\Config\Loader
23
 * @author  Pascal Knecht <[email protected]>
24
 */
25
class JSONLoader extends AbstractLoader
26
{
27
28
    /**
29
     * Load the configuration from an URI.
30
     *
31
     * @since 0.4.0
32
     *
33
     * @param string $uri URI of the resource to load.
34
     *
35
     * @return array Data contained within the resource.
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
36
     */
37
    public static function canLoad($uri)
38
    {
39
        $path = pathinfo($uri);
40
41
        return 'json' === mb_strtolower($path['extension']);
42
    }
43
44
    /**
45
     * Load the contents of an resource identified by an URI.
46
     *
47
     * @since 0.4.0
48
     *
49
     * @param string $uri URI of the resource.
50
     *
51
     * @return array|null Raw data loaded from the resource. Null if no data found.
52
     * @throws FailedToLoadConfigException If the resource could not be loaded.
53
     */
54
    protected function loadUri($uri)
55
    {
56
        try {
57
            // Try to load the file through PHP's include().
58
            // Make sure we don't accidentally create output.
59
            ob_start();
60
            $data = json_decode(file_get_contents($uri));
61
            ob_end_clean();
62
63
            return (array)$data;
64
        } catch (Exception $exception) {
65
            throw new FailedToLoadConfigException(
66
                sprintf(
67
                    _('Could not include PHP config file "%1$s". Reason: "%2$s".'),
68
                    $uri,
69
                    $exception->getMessage()
70
                ),
71
                $exception->getCode(),
72
                $exception
73
            );
74
        }
75
    }
76
77
    /**
78
     * Validate and return the URI.
79
     *
80
     * @since 0.4.0
81
     *
82
     * @param string $uri URI of the resource to load.
83
     *
84
     * @return string Validated URI.
85
     * @throws FailedToLoadConfigException If the URI does not exist or is not readable.
86
     */
87 View Code Duplication
    protected function validateUri($uri)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
    {
89
        if (! is_readable($uri)) {
90
            throw new FailedToLoadConfigException(
91
                sprintf(
92
                    _('The requested PHP config file "%1$s" does not exist or is not readable.'),
93
                    $uri
94
                )
95
            );
96
        }
97
98
        return $uri;
99
    }
100
}
101