ImportConvertValueCommand::executeSimpleCommand()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 54
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 54
ccs 0
cts 22
cp 0
crap 30
rs 9.2568

How to fix   Long Method   

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
/**
4
 * TechDivision\Import\Cli\Command\ImportConvertValueCommand
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2019 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-cli-simple
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Cli\Command;
16
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use TechDivision\Import\Utils\CommandNames;
21
use TechDivision\Import\Utils\InputArgumentKeysInterface;
22
use TechDivision\Import\Serializer\Csv\ValueCsvSerializer;
23
use TechDivision\Import\Configuration\ConfigurationInterface;
24
25
/**
26
 * The command to simulate converting a file.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 TechDivision GmbH <[email protected]>
30
 * @license   https://opensource.org/licenses/MIT
31
 * @link      https://github.com/techdivision/import-cli-simple
32
 * @link      http://www.techdivision.com
33
 */
34
class ImportConvertValueCommand extends AbstractSimpleImportCommand
35
{
36
37
    /**
38
     * Configures the current command.
39
     *
40
     * @return void
41
     * @see \Symfony\Component\Console\Command\Command::configure()
42
     */
43
    protected function configure()
44
    {
45
46
        // initialize the command with the required/optional options
47
        $this->setName(CommandNames::IMPORT_CONVERT_VALUE)
48
             ->setDescription('Converts the value to the format expected by the given column')
49
             ->addArgument(InputArgumentKeysInterface::ENTITY_TYPE_CODE, InputArgument::REQUIRED, 'The entity type code to use, e. g. catalog_product')
50
             ->addArgument(InputArgumentKeysInterface::COLUMN, InputArgument::REQUIRED, 'The column name to convert the value for')
51
             ->addArgument(InputArgumentKeysInterface::VALUES, InputArgument::REQUIRED|InputArgument::IS_ARRAY, 'The value to convert');
52
53
        // invoke the parent method
54
        parent::configure();
55
    }
56
57
    /**
58
     * Finally executes the simple command.
59
     *
60
     * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The configuration instance
61
     * @param \Symfony\Component\Console\Input\InputInterface           $input         An InputInterface instance
62
     * @param \Symfony\Component\Console\Output\OutputInterface         $output        An OutputInterface instance
63
     *
64
     * @return void
65
     */
66
    protected function executeSimpleCommand(
67
        ConfigurationInterface $configuration,
68
        InputInterface $input,
69
        OutputInterface $output
70
    ) {
71
72
        // initialize the default CSV serializer
73
        $serializer = new ValueCsvSerializer();
74
        $serializer->init($configuration);
75
76
        // initialize the array for the values that has to be serialized
77
        $serialize = array();
78
79
        // load the values that has to be serialized
80
        $values = $input->getArgument(InputArgumentKeysInterface::VALUES);
81
82
        // simulate custom column handling
83
        switch ($input->getArgument(InputArgumentKeysInterface::COLUMN)) {
84
            case 'categories':
85
                // serialize the categories and use the default delimiter
86
                $serialize[] = $serializer->serialize($values);
87
                break;
88
89
            case 'path':
90
                // categories use a slash (/) as delimiter for the first level of serialization
91
                $delimiter = '/';
92
                // load the enclosure from the configuration
93
                $enclosure = $configuration->getEnclosure();
94
                // iterate over the values and serialize them
95
                for ($i = 0; $i < sizeof($values); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
96
                    // serialize the value and use a slash (/) as delimiter
97
                    $val = $serializer->serialize(array($values[$i]), $delimiter);
98
99
                    // clean-up (means to remove surrounding + double quotes)
100
                    // because we've no delimiter within the value
101
                    if (strstr($val, $delimiter) === false) {
102
                        $val = preg_replace("/^(\'(.*)\'|".$enclosure."(.*)".$enclosure.")$/", '$2$3', $val);
103
                        $val = str_replace('""', '"', $val);
104
                    }
105
106
                    // append the cleaned value
107
                    $values[$i] = $val;
108
                }
109
110
                // implode the category's to get the complete path
111
                $serialize[] = implode($delimiter, $values);
112
                break;
113
114
            default:
115
                break;
116
        }
117
118
        // second serialization that simulates the framework parsing the CSV file
119
        $output->write($serializer->serialize($serialize));
120
    }
121
}
122