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

AbstractLoader::validateUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 AbstractLoader.
19
 *
20
 * @since   0.4.0
21
 *
22
 * @package BrightNucleus\Config\Loader
23
 * @author  Alain Schlesser <[email protected]>
24
 */
25
abstract class AbstractLoader implements LoaderInterface
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|null Data contained within the resource. Null if no data could be loaded/parsed.
36
     * @throws FailedToLoadConfigException If the configuration could not be loaded.
37
     */
38 2
    public function load($uri)
39
    {
40
        try {
41 2
            $uri  = $this->validateUri($uri);
42 2
            $data = $this->loadUri($uri);
43
44 2
            return $this->parseData($data);
45
        } catch (Exception $exception) {
46
            throw new FailedToLoadConfigException(
47
                sprintf(
48
                    _('Could not load resource located at "%1$s". Reason: "%2$s".'),
49
                    $uri,
50
                    $exception->getMessage()
51
                ),
52
                $exception->getCode(),
53
                $exception
54
            );
55
        }
56
    }
57
58
    /**
59
     * Validate and return the URI.
60
     *
61
     * @since 0.4.0
62
     *
63
     * @param string $uri URI of the resource to load.
64
     *
65
     * @return string Validated URI.
66
     */
67
    protected function validateUri($uri)
68
    {
69
        return $uri;
70
    }
71
72
    /**
73
     * Parse the raw data and return it in parsed form.
74
     *
75
     * @since 0.4.0
76
     *
77
     * @param array|null $data Raw data to be parsed.
78
     *
79
     * @return array|null Data in parsed form. Null if no parsable data found.
80
     */
81 2
    protected function parseData($data)
82
    {
83 2
        return $data;
84
    }
85
86
    /**
87
     * Load the contents of an resource identified by an URI.
88
     *
89
     * @since 0.4.0
90
     *
91
     * @param string $uri URI of the resource.
92
     *
93
     * @return array|null Raw data loaded from the resource. Null if no data found.
94
     */
95
    abstract protected function loadUri($uri);
96
}
97