Completed
Pull Request — master (#11)
by bloveless
02:20
created

GetSettingDefaultValue::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 10
nc 4
nop 2
1
<?php namespace Anomaly\SettingsModule\Setting\Command;
2
3
use Illuminate\Config\Repository;
4
use Illuminate\Container\Container;
5
use Illuminate\Contracts\Bus\SelfHandling;
6
7
/**
8
 * Class GetSettingDefaultValue
9
 *
10
 * @link          http://fritzandandre.com
11
 * @author        Brennon Loveless <[email protected]>
12
 * @package       Anomaly\SettingsModule\Setting\Command
13
 */
14
class GetSettingDefaultValue implements SelfHandling
15
{
16
    /**
17
     * The qualified key of the setting.
18
     * Namespace . :: . key
19
     *
20
     * @var
21
     */
22
    protected $settingKey;
23
24
    /**
25
     * GetSettingDefaultValue constructor.
26
     *
27
     * @param $settingKey
28
     */
29
    public function __construct($settingKey)
30
    {
31
        $this->settingKey = $settingKey;
32
    }
33
34
    /**
35
     * Look for a default value from the config definition file. If it has one then return it, otherwise return null.
36
     *
37
     * @param Repository $config
38
     * @param Container  $container
39
     * @return mixed
40
     */
41
    public function handle(Repository $config, Container $container)
42
    {
43
        $parts     = explode('::', $this->settingKey);
44
        $namespace = $parts[0];
45
        $key       = $parts[1];
46
47
        if (!$fields = $config->get($namespace . '::settings/settings')) {
48
            $fields = $config->get($namespace . '::settings');
49
        }
50
51
        $defaultValue = array_get($fields, $key . '.config.default_value', null);
52
53
        /**
54
         * If it is a closure, run it through the IoC container
55
         */
56
        if ($fields && ($defaultValue instanceof \Closure)) {
57
58
            return $container->call($defaultValue);
59
        }
60
61
        return $defaultValue;
62
    }
63
}