FamilyXlsxFileIterator::getChannelData()   D
last analyzed

Complexity

Conditions 14
Paths 257

Size

Total Lines 56
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 4.8629
c 0
b 0
f 0
cc 14
eloc 40
nc 257
nop 0

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 Pim\Bundle\ExcelConnectorBundle\Iterator;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
/**
8
 * Family Xls File Iterator
9
 *
10
 * @author    Antoine Guigan <[email protected]>
11
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
12
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
13
 */
14
class FamilyXlsxFileIterator extends AbstractXlsxFileIterator
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function createValuesIterator()
20
    {
21
        return new \ArrayIterator(array($this->getChannelData()));
22
    }
23
24
    /**
25
     * {@inheritdoc}
26
     */
27
    protected function getChannelData()
28
    {
29
        $xls = $this->getExcelObject();
30
        $data = [ 'attributes' => [] ];
31
        $attributeLabels = [];
32
        $channelLabels = [];
33
        $labelLocales = [];
34
        $codeColumn = null;
35
        $useAsLabelColumn = null;
36
        $channelColumn = null;
37
        $rowIterator = $xls->createRowIterator($this->worksheetIterator->key(), $this->options['parser_options']);
38
39
        foreach ($rowIterator as $index => $row) {
40
            if ($index == $this->options['code_row']) {
41
                $data['code'] = $row[$this->options['code_column']];
42
            }
43
            if ($index == $this->options['attribute_label_row']) {
44
                $attributeLabels = $row;
45
                $channelColumn = count($attributeLabels);
46
                array_splice($channelLabels, 0, $channelColumn);
47
                $data['requirements'] = array_fill_keys($channelLabels, []);
48
                $codeColumn = array_search('code', $attributeLabels);
49
                $useAsLabelColumn = array_search('use_as_label', $attributeLabels);
50
            }
51
            if ($index == $this->options['channel_label_row']) {
52
                $channelLabels = $row;
53
            }
54
            if ($index == $this->options['labels_label_row']) {
55
                $labelLocales = array_slice($row, $this->options['labels_column']);
56
            }
57
            if ($index == $this->options['labels_data_row']) {
58
                $data['labels'] = $this->getArrayHelper()->combineArrays(
59
                    $labelLocales,
60
                    array_slice($row, $this->options['labels_column'])
61
                );
62
            }
63
            if ($index >= (int) $this->options['attribute_data_row']) {
64
                $code = $row[$codeColumn];
65
                if ($code === '') {
66
                    continue;
67
                }
68
                $data['attributes'][] = $code;
69
                if (isset($row[$useAsLabelColumn]) && ('1' === trim($row[$useAsLabelColumn]))) {
70
                    $data['attribute_as_label'] = $code;
71
                }
72
                $channelValues = array_slice($row, $channelColumn);
73
                foreach ($channelLabels as $index => $channel) {
74
                    if (isset($channelValues[$index]) && '1' === trim($channelValues[$index])) {
75
                        $data['requirements'][$channel][] = $code;
76
                    }
77
                }
78
            }
79
        }
80
81
        return $data;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    protected function setDefaultOptions(OptionsResolver $resolver)
88
    {
89
        parent::setDefaultOptions($resolver);
90
91
        $resolver->setRequired(
92
            array(
93
                'channel_label_row',
94
                'attribute_label_row',
95
                'attribute_data_row',
96
                'code_row',
97
                'code_column',
98
                'labels_column',
99
                'labels_label_row',
100
                'labels_data_row',
101
                'labels_column'
102
            )
103
        );
104
    }
105
}
106