Completed
Push — develop ( 9cf581...7427e0 )
by Tom
10s
created

DummyCommand::manageArguments()   C

Complexity

Conditions 11
Paths 32

Size

Total Lines 80
Code Lines 50

Duplication

Lines 26
Ratio 32.5 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 26
loc 80
rs 5.2653
cc 11
eloc 50
nc 32
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\Category\Create;
4
5
use N98\Magento\Application;
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\Question\ChoiceQuestion;
11
use Symfony\Component\Console\Question\Question;
12
13
class DummyCommand extends \N98\Magento\Command\AbstractMagentoCommand
14
{
15
    const DEFAULT_CATEGORY_NAME = "My Awesome Category";
16
    const DEFAULT_CATEGORY_STATUS = 1; // enabled
17
    const DEFAULT_CATEGORY_ANCHOR = 1; // enabled
18
    const DEFAULT_STORE_ID = 1; // Default Store ID
19
20
21
    protected function configure()
22
    {
23
        $this
24
            ->setName('category:create:dummy')
25
            ->addArgument('store-id', InputArgument::OPTIONAL, 'Id of Store to create categories (default: 1)')
26
            ->addArgument('category-number', InputArgument::OPTIONAL, 'Number of categories to create (default: 1)')
27
            ->addArgument('children-categories-number', InputArgument::OPTIONAL, "Number of children for each category created (default: 0 - use '-1' for random from 0 to 5)")
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 175 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
28
            ->addArgument('category-name-prefix', InputArgument::OPTIONAL, "Category Name Prefix (default: 'My Awesome Category')")
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
29
            ->setDescription('Create a dummy category');
30
    }
31
32
    /**
33
     * @param \Symfony\Component\Console\Input\InputInterface   $input
34
     * @param \Symfony\Component\Console\Output\OutputInterface $output
35
     *
36
     * @return int|void
37
     */
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $this->detectMagento($output, true);
41
        $this->initMagento();
42
43
        $output->writeln("<warning>This only create sample categories, do not use on production environment</warning>\r\n");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
44
45
        // MANAGE ARGUMENTS
46
        $_argument = $this->manageArguments($input, $output);
47
48
        /**
49
         * LOOP to create categories
50
         */
51
        for ($i = 0; $i < $_argument['category-number']; $i++) {
52
            if(!is_null($_argument['category-name-prefix'])) {
53
                $name = $_argument['category-name-prefix'] . " " . $i;
54
            }
55
            else {
56
                $name = self::DEFAULT_CATEGORY_NAME . " " . $i;
57
            }
58
59
            // Check if product exists
60
            $collection = \Mage::getModel('catalog/category')->getCollection()
61
                ->addAttributeToSelect('name')
62
                ->addAttributeToFilter('name', array('eq' => $name));
63
            $_size = $collection->getSize();
64
            if($_size > 0) {
65
                $output->writeln("<comment>CATEGORY: WITH NAME: '" . $name . "' EXISTS! Skip</comment>\r");
66
                $_argument['category-number']++;
67
                continue;
68
            }
69
            unset($collection);
70
71
            $_category_root_id = \Mage::app()->getStore($_argument['store-id'])->getRootCategoryId();
72
73
            $category = \Mage::getModel('catalog/category');
74
            $category->setName($name);
75
            $category->setIsActive(self::DEFAULT_CATEGORY_STATUS);
76
            $category->setDisplayMode('PRODUCTS');
77
            $category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR);
78
79 View Code Duplication
            if(\Mage::getVersion() === "1.5.1.0")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
80
                $category->setStoreId(array(0,$_argument['store-id']));
81
            else
82
                $category->setStoreId($_argument['store-id']);
83
            $parentCategory = \Mage::getModel('catalog/category')->load($_category_root_id);
84
            $category->setPath($parentCategory->getPath());
85
86
            $category->save();
87
            $_parent_id = $category->getId();
88
            $output->writeln("<comment>CATEGORY: '" . $category->getName() . "' WITH ID: '" . $category->getId() . "' CREATED!</comment>\r");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 141 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
89
            unset($category);
90
91
            // CREATE CHILDREN CATEGORIES
92
            for ($j = 0; $j < $_argument['children-categories-number']; $j++) {
93
                $name_child = $name . " child " . $j;
94
95
                $category = \Mage::getModel('catalog/category');
96
                $category->setName($name_child);
97
                $category->setIsActive(self::DEFAULT_CATEGORY_STATUS);
98
                $category->setDisplayMode('PRODUCTS');
99
                $category->setIsAnchor(self::DEFAULT_CATEGORY_ANCHOR);
100
101 View Code Duplication
                if(\Mage::getVersion() === "1.5.1.0")
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
                    $category->setStoreId(array(0,$_argument['store-id']));
103
                else
104
                    $category->setStoreId($_argument['store-id']);
105
                $parentCategory = \Mage::getModel('catalog/category')->load($_parent_id);
106
                $category->setPath($parentCategory->getPath());
107
108
                $category->save();
109
                $output->writeln("<comment>CATEGORY CHILD: '" . $category->getName() . "' WITH ID: '" . $category->getId() . "' CREATED!</comment>\r");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 151 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
110
                unset($category);
111
            }
112
        }
113
    }
114
115
    /**
116
     * Manage console arguments
117
     *
118
     * @param \Symfony\Component\Console\Input\InputInterface   $input
119
     * @param \Symfony\Component\Console\Output\OutputInterface $output
120
     *
121
     * @return array
122
     */
123
    protected function manageArguments($input, $output)
124
    {
125
        /**
126
         * ARGUMENTS
127
         */
128
        $helper = $this->getHelper('question');
129
        $_argument = array();
130
131
        // STORE ID
132
        if(is_null($input->getArgument('store-id'))) {
133
            $store_id = \Mage::getModel('core/store')->getCollection()
134
                ->addFieldToSelect('*')
135
                ->addFieldToFilter('store_id', array('gt' => 0))
136
                ->setOrder('store_id', 'ASC');;
137
            $_store_ids = array();
138
139
            foreach ($store_id as $item) {
140
                $_store_ids[$item['store_id']] = $item['store_id'] . "|" . $item['code'];
141
            }
142
143
            $question = new ChoiceQuestion(
144
                'Please select Store ID (default: 1)',
145
                $_store_ids,
146
                self::DEFAULT_STORE_ID
147
            );
148
            $question->setErrorMessage('Store ID "%s" is invalid.');
149
            $response = explode("|", $helper->ask($input, $output, $question));
150
            $input->setArgument('store-id', $response[0]);
151
        }
152
        $output->writeln('<info>Store ID selected: ' . $input->getArgument('store-id') . "</info>\r\n");
153
        $_argument['store-id'] = $input->getArgument('store-id');
154
155
        // NUMBER OF CATEGORIES
156 View Code Duplication
        if(is_null($input->getArgument('category-number'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
            $question = new Question("Please enter the number of categories to create (default 1): ", 1);
158
            $question->setValidator(function ($answer) {
159
                $answer = (int)($answer);
160
                if(!is_int($answer) || $answer <= 0) {
161
                    throw new \RuntimeException(
162
                        'Please enter an integer value or > 0'
163
                    );
164
                }
165
                return $answer;
166
            });
167
            $input->setArgument('category-number', $helper->ask($input, $output, $question));
168
        }
169
        $output->writeln('<info>Number of categories to create: ' . $input->getArgument('category-number') . "</info>\r\n");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 124 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
170
        $_argument['category-number'] = $input->getArgument('category-number');
171
172
        // NUMBER OF CHILDREN CATEGORIES
173 View Code Duplication
        if(is_null($input->getArgument('children-categories-number'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
174
            $question = new Question("Number of children for each category created (default: 0 - use '-1' for random from 0 to 5): ", 0);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 137 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
175
            $question->setValidator(function ($answer) {
176
                $answer = (int)($answer);
177
                if(!is_int($answer) || $answer < -1) {
178
                    throw new \RuntimeException(
179
                        "Please enter an integer value or >= -1"
180
                    );
181
                }
182
                return $answer;
183
            });
184
            $input->setArgument('children-categories-number', $helper->ask($input, $output, $question));
185
        }
186
        if($input->getArgument('children-categories-number') == -1) {
187
            $input->setArgument('children-categories-number', rand(0, 5));
188
        }
189
190
        $output->writeln('<info>Number of categories children to create: ' . $input->getArgument('children-categories-number') . "</info>\r\n");
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
191
        $_argument['children-categories-number'] = $input->getArgument('children-categories-number');
192
193
        // CATEGORY NAME PREFIX
194
        if(is_null($input->getArgument('category-name-prefix'))) {
195
            $question = new Question("Please enter the category name prefix (default '" . self::DEFAULT_CATEGORY_NAME . "'): ", self::DEFAULT_CATEGORY_NAME);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 157 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
196
            $input->setArgument('category-name-prefix', $helper->ask($input, $output, $question));
197
        }
198
        $output->writeln('<info>CATEGORY NAME PREFIX: ' . $input->getArgument('category-name-prefix') . "</info>\r\n");
199
        $_argument['category-name-prefix'] = $input->getArgument('category-name-prefix');
200
201
        return $_argument;
202
    }
203
}
204