Completed
Push — master ( 5cd237...36e8c5 )
by Tom
04:40
created

AbstractConfigCommand::invalidScopeId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
1
<?php
2
3
namespace N98\Magento\Command\Config;
4
5
use InvalidArgumentException;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
8
abstract class AbstractConfigCommand extends AbstractMagentoCommand
9
{
10
    /**
11
     * @var array strings of configuration scopes
12
     */
13
    protected $_scopes = array(
14
        'default',
15
        'websites',
16
        'stores',
17
    );
18
19
    /**
20
     * @return \Mage_Core_Model_Encryption
21
     */
22
    protected function getEncryptionModel()
23
    {
24
        if ($this->_magentoMajorVersion == self::MAGENTO_MAJOR_VERSION_2) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
25
            // @TODO Magento 2 support
26
        } else {
27
            return \Mage::helper('core')->getEncryptor();
28
        }
29
    }
30
31
    /**
32
     * @return \Mage_Core_Model_Abstract
33
     */
34
    protected function _getConfigDataModel()
35
    {
36
        return $this->_getModel('core/config_data', 'Mage_Core_Model_Config_Data');
37
    }
38
39
    /**
40
     * @param string $value
41
     * @param string $encryptionType
42
     * @return string
43
     */
44
    protected function _formatValue($value, $encryptionType)
45
    {
46
        if ($encryptionType == 'encrypt') {
47
            $value = $this->getEncryptionModel()->encrypt($value);
48
        } elseif ($encryptionType == 'decrypt') {
49
            $value = $this->getEncryptionModel()->decrypt($value);
50
        }
51
52
        return $value;
53
    }
54
55
    /**
56
     * @param string $scope
57
     *
58
     * @return string
59
     */
60
    protected function _validateScopeParam($scope)
61
    {
62
        if (!in_array($scope, $this->_scopes)) {
63
            throw new InvalidArgumentException(
64
                sprintf('Invalid scope parameter, must be one of: %s.', implode(', ', $this->_scopes))
65
            );
66
        }
67
68
        return $scope;
69
    }
70
71
    /**
72
     * @param string $scope
73
     * @param string $scopeId
74
     * @param boolean $allowZeroScope
75
     *
76
     * @return string non-negative integer number
77
     */
78
    protected function _convertScopeIdParam($scope, $scopeId, $allowZeroScope = false)
79
    {
80
        if ($scope === 'default') {
81
            if ("$scopeId" !== "0") {
82
                throw new InvalidArgumentException(
83
                    sprintf("Invalid scope ID %d in scope '%s', must be 0", $scopeId, $scope)
84
                );
85
            }
86
87
            return $scopeId;
88
        }
89
90
        if ($scope == 'websites' && !is_numeric($scopeId)) {
91
            $website = \Mage::app()->getWebsite($scopeId);
92
            if (!$website) {
93
                throw new InvalidArgumentException(
94
                    sprintf("Invalid scope parameter, website '%s' does not exist.", $scopeId)
95
                );
96
            }
97
98
            return $website->getId();
99
        }
100
101
        if ($scope == 'stores' && !is_numeric($scopeId)) {
102
            $store = \Mage::app()->getStore($scopeId);
103
            if (!$store) {
104
                throw new InvalidArgumentException(
105
                    sprintf("Invalid scope parameter. store '%s' does not exist.", $scopeId)
106
                );
107
            }
108
109
            return $store->getId();
110
        }
111
112
        $this->invalidScopeId(
113
            $scopeId !== (string)(int)$scopeId,
114
            "Invalid scope parameter, %s is not an integer value",
115
            $scopeId
116
        );
117
118
        $this->invalidScopeId(
119
            0 - (bool)$allowZeroScope >= (int)$scopeId,
120
            "Invalid scope parameter, %s is not a positive integer value",
121
            $scopeId
122
        );
123
124
        return $scopeId;
125
    }
126
127
    private function invalidScopeId($condition, $mask, $scopeId)
128
    {
129
        if (!$condition) {
130
            return;
131
        }
132
133
        throw new InvalidArgumentException(
134
            sprintf($mask, var_export($scopeId, true))
135
        );
136
    }
137
138
    /**
139
     * @return \Mage_Core_Model_Config
140
     */
141
    protected function _getConfigModel()
142
    {
143
        return $this->_getModel('core/config', 'Mage_Core_Model_Config');
144
    }
145
}
146