Passed
Branch main (078a91)
by Alain
15:46
created

PHPLoader::validateUri()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.2559

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 12
ccs 3
cts 5
cp 0.6
crap 2.2559
rs 10
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 PHPLoader.
19
 *
20
 * @since   0.4.0
21
 *
22
 * @package BrightNucleus\Config\Loader
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
class PHPLoader extends AbstractLoader
26
{
27
28
    /**
29
     * Check whether the loader is able to load a given URI.
30
     *
31
     * @since 0.4.0
32
     *
33
     * @param string $uri URI to check.
34
     *
35
     * @return bool Whether the loader can load the given URI.
36
     */
37 2
    public static function canLoad($uri)
38
    {
39 2
        $path = pathinfo($uri);
40
41 2
        return 'php' === 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 2
    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 2
            ob_start();
60 2
            $data = include($uri);
61 2
            ob_end_clean();
62
63 2
            return $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 2
    protected function validateUri($uri)
88
    {
89 2
        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 2
        return $uri;
99
    }
100
}
101