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

ConfigFactory::createFromFile()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6.6829

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 32
ccs 11
cts 15
cp 0.7332
rs 8.439
cc 6
eloc 16
nc 16
nop 1
crap 6.6829
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;
13
14
use Exception;
15
16
/**
17
 * Create new object instances that implement ConfigInterface.
18
 *
19
 * @since   0.3.0
20
 *
21
 * @package BrightNucleus\Config
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
class ConfigFactory
25
{
26
27
    /**
28
     * Create a new ConfigInterface object from a file.
29
     *
30
     * If a comma-separated list of files is provided, they are checked in sequence until the first one could be loaded
31
     * successfully.
32
     *
33
     * @since 0.3.0
34
     *
35
     * @param string|array $_ List of files.
36
     *
37
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

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...
38
     */
39 1
    public static function createFromFile($_)
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
40
    {
41 1
        $files = array_reverse(func_get_args());
42
43 1
        if (is_array($files[0])) {
44
            $files = $files[0];
45
        }
46
47 1
        while (count($files) > 0) {
48
            try {
49 1
                $file = array_pop($files);
50
51 1
                if (! is_readable($file)) {
52 1
                    continue;
53
                }
54
55 1
                $data = (array)Loader::load($file);
56
57 1
                $config = static::createFromArray($data);
58
59 1
                if (null === $config) {
60
                    continue;
61
                }
62
63 1
                return $config;
64
            } catch (Exception $exception) {
65
                // Fail silently and try next file.
66
            }
67
        }
68
69
        return static::createFromArray([]);
70
    }
71
72
    /**
73
     * Create a new ConfigInterface object from an array.
74
     *
75
     * @since 0.3.0
76
     *
77
     * @param array $array Array with configuration values.
78
     *
79
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

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...
80
     */
81 1
    public static function createFromArray(array $array)
82
    {
83
        try {
84 1
            return new Config($array);
85
        } catch (Exception $exception) {
86
            // Fail silently and try next file.
87
        }
88
89
        return null;
90
    }
91
92
    /**
93
     * Create a new ConfigInterface object.
94
     *
95
     * Tries to deduce the correct creation method by inspecting the provided arguments.
96
     *
97
     * @since 0.3.0
98
     *
99
     * @param mixed $_ Array with configuration values.
100
     *
101
     * @return ConfigInterface Instance of a ConfigInterface implementation.
0 ignored issues
show
Documentation introduced by
Should the return type not be Config|null?

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...
102
     */
103 3
    public static function create($_)
0 ignored issues
show
Unused Code introduced by
The parameter $_ is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
104
    {
105 3
        if (func_num_args() < 1) {
106
            return static::createFromArray([]);
107
        }
108
109 3
        $arguments = func_get_args();
110
111 3
        if (is_array($arguments[0]) && func_num_args() === 1) {
112 1
            return static::createFromArray($arguments[0]);
113
        }
114
115 2
        return static::createFromFile($arguments);
116
    }
117
}
118