Passed
Push — master ( 26d549...0e9514 )
by Alain
02:38
created

PHPLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 61.11%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 76
ccs 11
cts 18
cp 0.6111
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A canLoad() 0 6 1
A loadUri() 0 22 2
A validateUri() 0 13 2
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 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
     * 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 1
    public static function canLoad($uri)
38
    {
39 1
        $path = pathinfo($uri);
40
41 1
        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 1
    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 1
            ob_start();
60 1
            $data = include($uri);
61 1
            ob_end_clean();
62
63 1
            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 1
    protected function validateUri($uri)
88
    {
89 1
        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 1
        return $uri;
99
    }
100
}
101