_getConfig()   C
last analyzed

Complexity

Conditions 8
Paths 11

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 27
rs 5.3846
cc 8
eloc 21
nc 11
nop 2
1
<?php
2
/**
3
 * Slack.com Integration
4
 *
5
 * PHP Version 5
6
 *
7
 * @category  Steverobbins
8
 * @package   Steverobbins_Slack
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright Copyright 2015 Steve Robbins (http://steverobbins.com)
11
 * @license   http://creativecommons.org/licenses/by/3.0/deed.en_US Creative Commons Attribution 3.0 Unported License
12
 */
13
14
/**
15
 * Abstract config getter
16
 */
17
class Steverobbins_Slack_Model_Config_Abstract extends Varien_Object
18
{
19
    const XML_PATH_CONFIG_SECTION = 'slack';
20
21
    /**
22
     * Gets sdm_lyris config
23
     *
24
     * @param string $field
25
     *
26
     * @return string|array
27
     */
28
    public function getConfig($field)
29
    {
30
        return $this->_getConfig($field);
31
    }
32
33
    /**
34
     * Update a config setting
35
     *
36
     * @param string  $field
37
     * @param mixed   $data
38
     * @param string  $scope
39
     * @param integer $scopeId
40
     *
41
     * @return Steverobbins_Slack_Model_Config_Abstract
42
     */
43
    public function setConfig($field, $data, $scope = 'default', $scopeId = 0)
44
    {
45
        Mage::getModel('core/config')->saveConfig(
46
            $this->getConfigPath($field),
47
            is_array($data) ? serialize($data) : $data,
48
            $scope,
49
            $scopeId
50
        );
51
        return $this;
52
    }
53
54
    /**
55
     * Gets sdm_lyris config flag
56
     *
57
     * @param string $field
58
     *
59
     * @return boolean
60
     */
61
    public function getConfigFlag($field)
62
    {
63
        return $this->_getConfig($field, true);
64
    }
65
66
    /**
67
     * Get the path for this config
68
     *
69
     * @param string $field
70
     *
71
     * @return string
72
     */
73
    public function getConfigPath($field)
74
    {
75
        return sprintf(
76
            '%s/%s/%s',
77
            self::XML_PATH_CONFIG_SECTION,
78
            $this->getGroup(),
79
            $field
80
        );
81
    }
82
83
    /**
84
     * Gets config value
85
     *
86
     * @param string  $field
87
     * @param boolean $flag
88
     *
89
     * @return string|array|boolean|null
90
     */
91
    protected function _getConfig($field, $flag = false)
92
    {
93
        $configPath = $this->getConfigPath($field);
94
        $scope = $this->getScope();
95
        if (isset($scope['website'])) {
96
            $value = Mage::app()->getWebsite($scope['website'])
97
                ->getConfig($configPath);
98
            $node = Mage::getConfig()->getNode('default/' . $configPath);
99
            if (!empty($node['backend_model']) && !empty($value)) {
100
                $backend = Mage::getModel((string) $node['backend_model']);
101
                $backend->setPath($configPath)->setValue($value)->afterLoad();
102
                $value = $backend->getValue();
103
            }
104
        } elseif (isset($scope['store'])) {
105
            $value = Mage::app()->getStore($scope['store'])
106
                ->getConfig($configPath);
107
        } else {
108
            if ($flag) {
109
                return Mage::getStoreConfigFlag($configPath);
110
            }
111
            return Mage::getStoreConfig($configPath);
112
        }
113
        if ($flag) {
114
            return !empty($value) && 'false' !== $value;
115
        }
116
        return $value;
117
    }
118
}
119