Completed
Push — develop ( f30ad5...49592b )
by Tom
03:52
created

AbstractMagentoStoreConfigCommand::execute()   C

Complexity

Conditions 12
Paths 193

Size

Total Lines 66
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 5.4065
c 0
b 0
f 0
cc 12
eloc 44
nc 193
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 Exception;
6
use N98\Util\Console\Helper\ParameterHelper;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Input\StringInput;
12
use Symfony\Component\Console\Output\NullOutput;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
abstract class AbstractMagentoStoreConfigCommand extends AbstractMagentoCommand
16
{
17
    /**
18
     * @var string
19
     */
20
    const SCOPE_STORE_VIEW = 'store';
21
22
    /**
23
     * @var string
24
     */
25
    const SCOPE_WEBSITE = 'website';
26
27
    /**
28
     * @var string
29
     */
30
    const SCOPE_GLOBAL = 'global';
31
32
    /**
33
     * Store view or global by additional option
34
     */
35
    const SCOPE_STORE_VIEW_GLOBAL = 'store_view_global';
36
37
    /**
38
     * @var string
39
     */
40
    protected $commandName = '';
41
42
    /**
43
     * @var string
44
     */
45
    protected $commandDescription = '';
46
47
    /**
48
     * @var string
49
     */
50
    protected $configPath = '';
51
52
    /**
53
     * @var string
54
     */
55
    protected $toggleComment = '';
56
57
    /**
58
     * @var string
59
     */
60
    protected $falseName = 'disabled';
61
62
    /**
63
     * @var string
64
     */
65
    protected $trueName = 'enabled';
66
67
    /**
68
     * Add admin store to interactive prompt
69
     *
70
     * @var bool
71
     */
72
    protected $withAdminStore = false;
73
74
    /**
75
     * @var string
76
     */
77
    protected $scope = self::SCOPE_STORE_VIEW;
78
79
    protected function configure()
80
    {
81
        $this
82
            ->setName($this->commandName)
83
            ->addOption('on', null, InputOption::VALUE_NONE, 'Switch on')
84
            ->addOption('off', null, InputOption::VALUE_NONE, 'Switch off')
85
            ->setDescription($this->commandDescription)
86
        ;
87
88
        if ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL) {
89
            $this->addOption('global', null, InputOption::VALUE_NONE, 'Set value on default scope');
90
        }
91
92
        if ($this->scope == self::SCOPE_STORE_VIEW || $this->scope == self::SCOPE_STORE_VIEW_GLOBAL) {
93
            $this->addArgument('store', InputArgument::OPTIONAL, 'Store code or ID');
94
        }
95
    }
96
97
    /**
98
     * @param InputInterface $input
99
     * @param OutputInterface $output
100
     * @return int|void
101
     */
102
    protected function execute(InputInterface $input, OutputInterface $output)
103
    {
104
        $this->detectMagento($output);
105
        if (!$this->initMagento()) {
106
            return;
107
        }
108
109
        $runOnStoreView = false;
110
        if ($this->scope == self::SCOPE_STORE_VIEW
111
            || ($this->scope == self::SCOPE_STORE_VIEW_GLOBAL && !$input->getOption('global'))
112
        ) {
113
            $runOnStoreView = true;
114
        }
115
116
        if ($runOnStoreView) {
117
            $store = $this->initStore($input, $output);
118
        } else {
119
            $storeManager = $this->getObjectManager()->get('Magento\Store\Model\StoreManagerInterface');
120
            /* @var $storeManager \Magento\Store\Model\StoreManagerInterface */
121
            $store = $storeManager->getStore(\Magento\Store\Model\Store::DEFAULT_STORE_ID);
122
        }
123
124
        if ($input->getOption('on')) {
125
            $isFalse = true;
126
        } elseif ($input->getOption('off')) {
127
            $isFalse = false;
128
        } else {
129
            $scopeConfig = $this->getObjectManager()->get('\Magento\Framework\App\Config\ScopeConfigInterface');
130
            /* @var $scopeConfig \Magento\Framework\App\Config\ScopeConfigInterface */
131
            $isFalse = !$scopeConfig->isSetFlag(
132
                $this->configPath,
133
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
134
                $store->getCode()
135
            );
136
        }
137
138
        $this->beforeSave($store, $isFalse);
139
140
        if ($store->getId() == \Magento\Store\Model\Store::DEFAULT_STORE_ID) {
141
            $scope = 'default'; // @TODO Constant was removed in Magento2 ?
142
        } else {
143
            $scope = \Magento\Store\Model\ScopeInterface::SCOPE_STORES;
144
        }
145
146
        $configSetCommands = [
147
            'command'    => 'config:set',
148
            'path'       => $this->configPath,
149
            'value'      => $isFalse ? 1 : 0,
150
            '--scope'    => $scope,
151
            '--scope-id' => $store->getId(),
152
        ];
153
154
        $input = new ArrayInput($configSetCommands);
155
        $this->getApplication()->setAutoExit(false);
156
        $this->getApplication()->run($input, new NullOutput());
157
158
        $comment = '<comment>' . $this->toggleComment . '</comment> '
159
                    . '<info>' . (!$isFalse ? $this->falseName : $this->trueName) . '</info>'
160
                    . ($runOnStoreView ? ' <comment>for store</comment> <info>' . $store->getCode() . '</info>' : '');
161
        $output->writeln($comment);
162
163
        $this->afterSave($store, $isFalse);
164
165
        $input = new StringInput('cache:flush');
166
        $this->getApplication()->run($input, new NullOutput());
167
    }
168
169
    /**
170
     * @param InputInterface $input
171
     * @param OutputInterface $output
172
     * @return mixed
173
     * @throws Exception
174
     */
175
    protected function initStore(InputInterface $input, OutputInterface $output)
176
    {
177
        /** @var $parameter ParameterHelper */
178
        $parameter = $this->getHelper('parameter');
179
180
        return $parameter->askStore($input, $output, 'store', $this->withAdminStore);
181
    }
182
183
    /**
184
     * @param \Magento\Store\Model\Store $store
185
     * @param bool $disabled
186
     */
187
    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...
188
    {
189
    }
190
191
    /**
192
     * @param \Magento\Store\Model\Store $store
193
     * @param bool $disabled
194
     */
195
    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...
196
    {
197
    }
198
}
199