Test Setup Failed
Push — master ( f687c0...10c0b6 )
by Gabriel
12:32
created

PackageHasConfigTrait::setConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Config\Utils;
4
5
use Nip\Config\Config;
6
7
/**
8
 * Trait PackageHasConfigTrait
9
 * @package Nip\Config\Utils
10
 */
11
trait PackageHasConfigTrait
12
{
13
    protected static $config = null;
14
15
    /**
16
     * @param null $name
17
     * @param null $default
18
     * @return mixed|Config|string|null
19
     */
20
    protected static function getPackageConfig($name = null, $default = null)
21
    {
22
        $config = static::getConfig();
23
        $name = static::getPackageConfigName().($name ? '.'.$name : '');
24
25
        return $config->get($name, $default);
26
    }
27
28
    /**
29
     * @return string
30
     */
31
    abstract protected static function getPackageConfigName();
32
33
    /**
34
     * @return Config
35
     */
36
    protected static function getConfig()
37
    {
38
        if (static::$config === null) {
39
            static::setConfig(static::generateConfig());
40
        }
41
42
        return static::$config;
43
    }
44
45
    /**
46
     * @return Config
47
     */
48
    protected static function generateConfig()
49
    {
50
        if (function_exists('config')) {
51
            try {
52
                return \config();
53
            } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
54
            }
55
        }
56
57
        return new Config();
58
    }
59
60
    /**
61
     * @param Config|array $config
62
     */
63
    public static function setConfig($config)
64
    {
65
        if (is_array($config)) {
66
            $config = new Config($config);
67
        }
68
        self::$config = $config;
69
    }
70
71
    public static function resetConfig()
72
    {
73
        static::$config = null;
74
    }
75
}
76