AbstractConfigCommand::getScopePool()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace N98\Magento\Command\Config;
4
5
use N98\Magento\Command\AbstractMagentoCommand;
6
7
abstract class AbstractConfigCommand extends AbstractMagentoCommand
8
{
9
    /**
10
     * @var \Magento\Framework\App\Config\ScopePoolInterface
11
     */
12
    protected $_scopePool;
13
14
    /**
15
     * @return \Magento\Framework\Encryption\EncryptorInterface
16
     */
17
    protected function getEncryptionModel()
18
    {
19
        return $this->getObjectManager()->get('\Magento\Framework\Encryption\EncryptorInterface');
20
    }
21
22
    /**
23
     * @return \Magento\Framework\App\Config
0 ignored issues
show
Documentation introduced by
Should the return type not be \Magento\Framework\ObjectManager\ObjectManager?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
24
     */
25
    protected function _getConfigModel()
26
    {
27
        return $this->getObjectManager('\Magento\Framework\App\Config');
0 ignored issues
show
Unused Code introduced by
The call to AbstractConfigCommand::getObjectManager() has too many arguments starting with '\\Magento\\Framework\\App\\Config'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28
    }
29
30
    /**
31
     * @return \Magento\Framework\App\Config\ScopePoolInterface
32
     */
33
    protected function getScopePool()
34
    {
35
        if (!$this->_scopePool) {
36
            $this->_scopePool = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopePool');
37
        }
38
39
        return $this->_scopePool;
40
    }
41
42
    /**
43
     * @param string $scope
44
     * @return \Magento\Framework\App\Config\Data
45
     */
46
    protected function getScope($scope)
47
    {
48
        return $this->getScopePool()->getScope($scope);
49
    }
50
51
    /**
52
     * @return \Magento\Framework\App\Config\Storage\WriterInterface
53
     */
54
    protected function getConfigWriter()
55
    {
56
        return $this->getObjectManager()->get('\Magento\Framework\App\Config\Storage\WriterInterface');
57
    }
58
59
    /**
60
     * @param string $value
61
     * @param string $encryptionType
62
     * @return string
63
     */
64
    protected function _formatValue($value, $encryptionType)
65
    {
66
        if ($encryptionType == 'encrypt') {
67
            $value = $this->getEncryptionModel()->encrypt($value);
68
        } elseif ($encryptionType == 'decrypt') {
69
            $value = $this->getEncryptionModel()->decrypt($value);
70
        }
71
72
        return $value;
73
    }
74
75
    /**
76
     * @param string $scope
77
     */
78
    protected function _validateScopeParam($scope)
79
    {
80
        if (!in_array($scope, $this->_scopes)) {
81
            throw new \InvalidArgumentException(
82
                '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...
83
            );
84
        }
85
    }
86
87
    /**
88
     * @param string $scope
89
     * @param string $scopeId
90
     *
91
     * @return string
92
     */
93
    protected function _convertScopeIdParam($scope, $scopeId)
94
    {
95
        if ($scope == 'websites' && !is_numeric($scopeId)) {
96
            $website = \Mage::app()->getWebsite($scopeId);
97
            if (!$website) {
98
                throw new \InvalidArgumentException('Invalid scope parameter. Website does not exist.');
99
            }
100
101
            return $website->getId();
102
        }
103
104
        if ($scope == 'stores' && !is_numeric($scopeId)) {
105
            $store = \Mage::app()->getStore($scopeId);
106
            if (!$store) {
107
                throw new \InvalidArgumentException('Invalid scope parameter. Store does not exist.');
108
            }
109
110
            return $store->getId();
111
        }
112
113
        return $scopeId;
114
    }
115
}
116