Completed
Push — master ( 1dd7d1...0c5b7b )
by Tom
09:54 queued 05:02
created

AbstractConfigCommand::_convertScopeIdParam()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 26
rs 4.909
cc 9
eloc 14
nc 6
nop 2
1
<?php
2
3
namespace N98\Magento\Command\Config;
4
5
use Magento\Framework\ObjectManager\ObjectManager;
6
use N98\Magento\Command\AbstractMagentoCommand;
7
8
abstract class AbstractConfigCommand extends AbstractMagentoCommand
9
{
10
    /**
11
     * \Magento\Framework\App\Config\Storage\WriterInterface
12
     */
13
    private $configWriter;
14
15
    /**
16
     * @var \Magento\Framework\App\Config\ScopePoolInterface
17
     */
18
    protected $_scopePool;
19
20
    /**
21
     * @return \Magento\Framework\Encryption\EncryptorInterface
22
     */
23
    protected function getEncryptionModel()
24
    {
25
        return $this->getObjectManager()->get('\Magento\Framework\Encryption\EncryptorInterface');
26
    }
27
28
    /**
29
     * @return \Magento\Framework\App\Config
30
     */
31
    protected function _getConfigModel()
32
    {
33
        return $this->getObjectManager()->get('\Magento\Framework\App\Config');
34
    }
35
36
    /**
37
     * @return \Magento\Framework\App\Config\ScopePoolInterface
38
     */
39
    protected function getScopePool()
40
    {
41
        if (!$this->_scopePool) {
42
            $this->_scopePool = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopePool');
43
        }
44
45
        return $this->_scopePool;
46
    }
47
48
    /**
49
     * @param string $scope
50
     * @return \Magento\Framework\App\Config\Data
51
     */
52
    protected function getScope($scope)
53
    {
54
        return $this->getScopePool()->getScope($scope);
55
    }
56
57
    /**
58
     * @return \Magento\Framework\App\Config\Storage\WriterInterface
59
     */
60
    protected function getConfigWriter()
61
    {
62
        if (!$this->configWriter) {
63
            /** @var ObjectManager $objectManager */
64
            $objectManager = $this->getObjectManager();
65
            $this->configWriter = $objectManager->get('\Magento\Framework\App\Config\Storage\WriterInterface');
66
        }
67
68
        return $this->configWriter;
69
    }
70
71
    /**
72
     * @param string $value
73
     * @param string $encryptionType
74
     * @return string
75
     */
76
    protected function _formatValue($value, $encryptionType)
77
    {
78
        if ($encryptionType == 'encrypt') {
79
            $value = $this->getEncryptionModel()->encrypt($value);
80
        } elseif ($encryptionType == 'decrypt') {
81
            $value = $this->getEncryptionModel()->decrypt($value);
82
        }
83
84
        return $value;
85
    }
86
87
    /**
88
     * @param string $scope
89
     */
90
    protected function _validateScopeParam($scope)
91
    {
92
        if (!in_array($scope, $this->_scopes)) {
93
            throw new \InvalidArgumentException(
94
                'Invalid scope parameter. It must be one of ' . implode(',', $this->_scopes)
0 ignored issues
show
Bug introduced by
The property _scopes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
95
            );
96
        }
97
    }
98
99
    /**
100
     * @param string $scope
101
     * @param string $scopeId
102
     *
103
     * @return string
104
     */
105
    protected function _convertScopeIdParam($scope, $scopeId)
106
    {
107
        if (null === $scopeId && in_array($scope, array('websites', 'stores'), true)) {
108
            return $scopeId;
109
        }
110
111
        if ($scope == 'websites' && !is_numeric($scopeId)) {
112
            $website = \Mage::app()->getWebsite($scopeId);
113
            if (!$website) {
114
                throw new \InvalidArgumentException('Invalid scope parameter. Website does not exist.');
115
            }
116
117
            return $website->getId();
118
        }
119
120
        if ($scope == 'stores' && !is_numeric($scopeId)) {
121
            $store = \Mage::app()->getStore($scopeId);
122
            if (!$store) {
123
                throw new \InvalidArgumentException('Invalid scope parameter. Store does not exist.');
124
            }
125
126
            return $store->getId();
127
        }
128
129
        return $scopeId;
130
    }
131
}
132