Completed
Push — master ( 34d5f0...715aef )
by Chauncey
07:28
created

AdminTrait::apiConfig()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 2
dl 0
loc 12
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Charcoal\Admin\Support;
4
5
// From 'charcoal-app'
6
use Charcoal\App\AppConfig;
7
8
// From 'charcoal-admin'
9
use Charcoal\Admin\Config as AdminConfig;
10
11
/**
12
 * Admin Support Trait
13
 */
14
trait AdminTrait
15
{
16
    /**
17
     * Store a reference to the admin configuration.
18
     *
19
     * @var AdminConfig
20
     */
21
    protected $adminConfig;
22
23
    /**
24
     * Store a reference to the application configuration.
25
     *
26
     * @var AppConfig
27
     */
28
    protected $appConfig;
29
30
    /**
31
     * Whether the debug mode is enabled.
32
     *
33
     * @var boolean
34
     */
35
    private $debug = false;
36
37
    /**
38
     * Set application debug mode.
39
     *
40
     * @param  boolean $debug The debug flag.
41
     * @return void
42
     */
43
    protected function setDebug($debug)
44
    {
45
        $this->debug = !!$debug;
46
    }
47
48
    /**
49
     * Retrieve the application debug mode.
50
     *
51
     * @return boolean
52
     */
53
    public function debug()
54
    {
55
        return $this->debug;
56
    }
57
58
    /**
59
     * Set the admin's configset.
60
     *
61
     * @param  AdminConfig $config A configset.
62
     * @return void
63
     */
64
    protected function setAdminConfig(AdminConfig $config)
65
    {
66
        $this->adminConfig = $config;
67
    }
68
69
    /**
70
     * Retrieve the admin's configset.
71
     *
72
     * @param  string|null $key     Optional data key to retrieve from the configset.
73
     * @param  mixed|null  $default The default value to return if data key does not exist.
74
     * @return mixed|AdminConfig
75
     */
76
    protected function adminConfig($key = null, $default = null)
77
    {
78
        if ($key) {
79
            if (isset($this->adminConfig[$key])) {
80
                return $this->adminConfig[$key];
81
            } else {
82
                if (!is_string($default) && is_callable($default)) {
83
                    return $default();
84
                } else {
85
                    return $default;
86
                }
87
            }
88
        }
89
90
        return $this->adminConfig;
91
    }
92
93
    /**
94
     * Set the application's configset.
95
     *
96
     * @param  AppConfig $config A configset.
97
     * @return void
98
     */
99
    protected function setAppConfig(AppConfig $config)
100
    {
101
        $this->appConfig = $config;
102
    }
103
104
    /**
105
     * Retrieve the application's configset.
106
     *
107
     * @param  string|null $key     Optional data key to retrieve from the configset.
108
     * @param  mixed|null  $default The default value to return if data key does not exist.
109
     * @return mixed|AppConfig
110
     */
111
    protected function appConfig($key = null, $default = null)
112
    {
113
        if ($key) {
114
            if (isset($this->appConfig[$key])) {
115
                return $this->appConfig[$key];
116
            } else {
117
                if (!is_string($default) && is_callable($default)) {
118
                    return $default();
119
                } else {
120
                    return $default;
121
                }
122
            }
123
        }
124
125
        return $this->appConfig;
126
    }
127
128
    /**
129
     * Retrieve a value from the API configset.
130
     *
131
     * Looks up the admin module first, the application second.
132
     *
133
     * @param  string|null $key     Optional data key to retrieve from the configset.
134
     * @param  mixed|null  $default The default value to return if data key does not exist.
135
     * @return mixed
136
     */
137
    protected function apiConfig($key, $default = null)
138
    {
139
        $key = 'apis.'.$key;
140
141
        if (isset($this->adminConfig[$key])) {
142
            return $this->adminConfig[$key];
143
        } elseif (isset($this->appConfig[$key])) {
144
            return $this->appConfig[$key];
145
        } elseif (!is_string($default) && is_callable($default)) {
146
            return $default();
147
        } else {
148
            return $default;
149
        }
150
    }
151
}
152