Completed
Push — master ( 180991...2e46a8 )
by Mikael
08:09
created

Configure::configure()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.5726

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 13
cp 0.5385
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
crap 5.5726
1
<?php
2
3
namespace Anax\Common;
4
5
/**
6
 * Trait implementing reading from config-file and storing options in
7
 * $this->config.
8
 *
9
 */
10
trait Configure
11
{
12
13
    /**
14
     * Properties
15
     *
16
     */
17
    private $config = [];  // Store all config as an array
18
19
20
21
    /**
22
     * Read configuration from file or array, if a file, first check in
23
     * ANAX_APP_PATH/config and then in ANAX_INSTALL_PATH/config.
24
     *
25
     * @param array/string $what is an array with key/value config options
0 ignored issues
show
Documentation introduced by
The doc-type array/string could not be parsed: Unknown type name "array/string" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
26
     *                           or a file to be included which returns such
27
     *                           an array.
28
     *
29
     * @throws Exception when argument if not a filer nor an array.
30
     *
31
     * @return $this for chaining.
32
     */
33 1
    public function configure($what)
34
    {
35 1
        $anaxInstallPath = ANAX_INSTALL_PATH . "/config/$what";
36 1
        $anaxAppPath = ANAX_APP_PATH . "/config/$what";
37
        
38 1
        if (is_array($what)) {
39
            $options = $what;
40 1
        } elseif (is_readable($anaxAppPath)) {
41
            $options = require $anaxAppPath;
42 1
        } elseif (is_readable($anaxInstallPath)) {
43
            $options = require $anaxInstallPath;
44
        } else {
45 1
            throw new ConfigurationException("Configure item '$what' is not an array nor a readable file.");
46
        }
47
48
        $this->config = array_merge($this->config, $options);
49
        return $this->config;
50
    }
51
}
52