JsonLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 4
Ratio 100 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 4
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * JSON file loader.
4
 *
5
 * @package SugiPHP.Config
6
 * @author  Plamen Popov <[email protected]>
7
 * @license http://opensource.org/licenses/mit-license.php (MIT License)
8
 */
9
10
namespace SugiPHP\Config;
11
12 View Code Duplication
class JsonLoader implements LoaderInterface
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    protected $locator;
15
16
    public function __construct(LocatorInterface $locator = null)
17
    {
18
        $this->locator = $locator;
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function load($resource)
25
    {
26
        // check the extension. If it's not provided we'll add .php
27
        if (pathinfo($resource, PATHINFO_EXTENSION) === "") {
28
            $resource .= ".json";
29
        }
30
31
        $file = false;
32
33
        if ($this->locator) {
34
            // pass it to the locator (if set) and than include the file
35
            $file = $this->locator->locate($resource);
36
        } elseif (is_file($resource) && is_readable($resource)) {
37
            // check if the $resource is a real file and include it
38
            $file = $resource;
39
        }
40
41
        if ($file) {
42
            $json = file_get_contents($file);
43
            return json_decode($json, true);
44
        }
45
46
        return null;
47
    }
48
}
49