Completed
Push — master ( ad0611...abd676 )
by Tom
09:08 queued 04:30
created

AbstractMagentoStoreConfigCommand::execute()   D

Complexity

Conditions 12
Paths 240

Size

Total Lines 65
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 65
rs 4.627
cc 12
eloc 43
nc 240
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace N98\Magento\Command;
4
5
use Symfony\Component\Console\Input\ArrayInput;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Input\StringInput;
11
use Symfony\Component\Console\Output\NullOutput;
12
13
abstract class AbstractMagentoStoreConfigCommand extends AbstractMagentoCommand
14
{
15
    /**
16
     * @var string
17
     */
18
    const SCOPE_STORE_VIEW = 'store';
19
20
    /**
21
     * @var string
22
     */
23
    const SCOPE_WEBSITE = 'website';
24
25
    /**
26
     * @var string
27
     */
28
    const SCOPE_GLOBAL = 'global';
29
30
    /**
31
     * Store view or global by additional option
32
     */
33
    const SCOPE_STORE_VIEW_GLOBAL = 'store_view_global';
34
35
    /**
36
     * @var string
37
     */
38
    protected $commandName = '';
39
40
    /**
41
     * @var string
42
     */
43
    protected $commandDescription = '';
44
45
    /**
46
     * @var string
47
     */
48
    protected $configPath = '';
49
50
    /**
51
     * @var string
52
     */
53
    protected $toggleComment = '';
54
55
    /**
56
     * @var string
57
     */
58
    protected $falseName = 'disabled';
59
60
    /**
61
     * @var string
62
     */
63
    protected $trueName = 'enabled';
64
65
    /**
66
     * Add admin store to interactive prompt
67
     *
68
     * @var bool
69
     */
70
    protected $withAdminStore = false;
71
72
    /**
73
     * @var string
74
     */
75
    protected $scope = self::SCOPE_STORE_VIEW;
76
77
    protected function configure()
78
    {
79
        $this
80
            ->setName($this->commandName)
81
            ->addOption('on', null, InputOption::VALUE_NONE, 'Switch on')
82
            ->addOption('off', null, InputOption::VALUE_NONE, 'Switch off')
83
            ->setDescription($this->commandDescription)
84
        ;
85
86
        if ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL) {
87
            $this->addOption('global', null, InputOption::VALUE_NONE, 'Set value on default scope');
88
        }
89
90
        if ($this->scope == self::SCOPE_STORE_VIEW || $this->scope == self::SCOPE_STORE_VIEW_GLOBAL) {
91
            $this->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID');
92
        }
93
    }
94
95
    /**
96
     * @param \Symfony\Component\Console\Input\InputInterface $input
97
     * @param \Symfony\Component\Console\Output\OutputInterface $output
98
     * @return int|void
99
     */
100
    protected function execute(InputInterface $input, OutputInterface $output)
101
    {
102
        $this->detectMagento($output);
103
        if ($this->initMagento()) {
104
            $runOnStoreView = false;
105
            if ($this->scope == self::SCOPE_STORE_VIEW
106
                || ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL && !$input->getOption('global'))
107
            ) {
108
                $runOnStoreView = true;
109
            }
110
111
            if ($runOnStoreView) {
112
                $store = $this->_initStore($input, $output);
113
            } else {
114
                $storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManagerInterface');
115
                /* @var $storeManager \Magento\Store\Model\StoreManagerInterface */
116
                $store = $storeManager->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
117
            }
118
        }
119
120
        if ($input->getOption('on')) {
121
            $isFalse = true;
122
        } elseif ($input->getOption('off')) {
123
            $isFalse = false;
124
        } else {
125
            $scopeConfig = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopeConfigInterface');
126
            /* @var $scopeConfig \Magento\Framework\App\Config\ScopeConfigInterface */
127
            $isFalse = !$scopeConfig->isSetFlag(
128
                $this->configPath,
129
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
130
                $store->getCode()
0 ignored issues
show
Bug introduced by
The variable $store does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
131
            );
132
        }
133
134
        $this->_beforeSave($store, $isFalse);
135
136
137
        if ($store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) {
138
            $scope = 'default'; // @TODO Constant was removed in Magento2 ?
139
        } else {
140
            $scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;
141
        }
142
143
        $configSetCommands = [
144
            'command'    => 'config:set',
145
            'path'       => $this->configPath,
146
            'value'      => $isFalse ? 1 : 0,
147
            '--scope'    => $scope,
148
            '--scope-id' => $store->getId(),
149
        ];
150
151
        $input = new ArrayInput($configSetCommands);
152
        $this->getApplication()->setAutoExit(false);
153
        $this->getApplication()->run($input, new NullOutput());
154
155
        $comment = '<comment>' . $this->toggleComment . '</comment> '
156
                 . '<info>' . (!$isFalse ? $this->falseName : $this->trueName) . '</info>'
157
                 . ($runOnStoreView ? ' <comment>for store</comment> <info>' . $store->getCode() . '</info>' : '');
0 ignored issues
show
Bug introduced by
The variable $runOnStoreView does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
158
        $output->writeln($comment);
159
160
        $this->_afterSave($store, $isFalse);
161
162
        $input = new StringInput('cache:flush');
163
        $this->getApplication()->run($input, new NullOutput());
164
    }
165
166
    /**
167
     * @param \Symfony\Component\Console\Input\InputInterface $input
168
     * @param \Symfony\Component\Console\Output\OutputInterface $output
169
     * @return mixed
170
     * @throws \Exception
171
     */
172
    protected function _initStore($input, $output)
173
    {
174
        return $this->getHelperSet()->get('parameter')->askStore($input, $output, 'store', $this->withAdminStore);
175
    }
176
177
    /**
178
     * @param \Magento\Store\Model\Store $store
179
     * @param bool $disabled
180
     */
181
    protected function _beforeSave(\Magento\Store\Model\Store $store, $disabled)
0 ignored issues
show
Unused Code introduced by
The parameter $store is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $disabled is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
182
    {
183
    }
184
185
    /**
186
     * @param \Magento\Store\Model\Store $store
187
     * @param bool $disabled
188
     */
189
    protected function _afterSave(\Magento\Store\Model\Store $store, $disabled)
0 ignored issues
show
Unused Code introduced by
The parameter $store is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $disabled is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
    {
191
    }
192
}
193