Completed
Pull Request — master (#57)
by jean-marie
26:09
created

InitFamilyFileIterator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 101
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createValuesIterator() 0 7 1
C getChannelData() 0 62 13
A configureOptions() 0 18 1
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Iterator;
4
5
use Symfony\Component\OptionsResolver\OptionsResolver;
6
7
/**
8
 * Family init file iterator
9
 *
10
 * @author    JM leroux <[email protected]>
11
 * @author    Antoine Guigan <[email protected]>
12
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
13
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
14
 */
15
class InitFamilyFileIterator extends InitFileIterator
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20
    protected function createValuesIterator()
21
    {
22
        $familyData = $this->getChannelData();
23
        $this->headers = array_keys($familyData);
24
        $this->rows = new \ArrayIterator(['dummy_line', $familyData]);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \ArrayIterator(array...my_line', $familyData)) of type object<ArrayIterator> is incompatible with the declared type object<Box\Spout\Reader\IteratorInterface> of property $rows.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
25
        $this->rows->rewind();
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function getChannelData()
32
    {
33
        $this->rows = $this->reader->getSheetIterator()->current()->getRowIterator();
34
        $this->rows->rewind();
35
36
        $data = ['attributes' => []];
37
        $channelLabels = [];
38
        $labelLocales = [];
39
        $codeColumn = null;
40
        $useAsLabelColumn = null;
41
        $channelColumn = null;
42
43
        $arrayHelper = new ArrayHelper();
44
45
        $rowIterator = $this->rows;
46
47
        foreach ($rowIterator as $index => $row) {
48
            $row = $this->trimRight($row);
49
            if ($index == $this->options['channel_label_row']) {
50
                $channelLabels = $row;
51
            }
52
            if ($index == $this->options['family_labels_locales_row']) {
53
                $labelLocales = array_slice($row, $this->options['family_labels_first_column']);
54
            }
55
            if ($index == $this->options['family_data_row']) {
56
                $data['code'] = $row[$this->options['family_code_column']];
57
                $data['labels'] = $arrayHelper->combineArrays(
58
                    $labelLocales,
59
                    array_slice($row, $this->options['family_labels_first_column'])
60
                );
61
            }
62
            if ($index == $this->options['attribute_label_row']) {
63
                $attributeLabels = $row;
64
                $channelColumn = count($attributeLabels);
65
                array_splice($channelLabels, 0, $channelColumn);
66
                $data['requirements'] = array_fill_keys($channelLabels, []);
67
                $codeColumn = array_search('code', $attributeLabels);
68
                $useAsLabelColumn = array_search('use_as_label', $attributeLabels);
69
            }
70
71
            if ($index >= (int) $this->options['attribute_data_row']) {
72
                $code = $row[$codeColumn];
73
                if ($code === '') {
74
                    continue;
75
                }
76
                $data['attributes'][] = $code;
77
                if (isset($row[$useAsLabelColumn]) && ('1' === trim($row[$useAsLabelColumn]))) {
78
                    $data['attribute_as_label'] = $code;
79
                }
80
                $channelValues = array_slice($row, $channelColumn);
81
                foreach ($channelLabels as $channelIndex => $channel) {
82
                    if (isset($channelValues[$channelIndex]) && '1' === trim($channelValues[$channelIndex])) {
83
                        $data['requirements'][$channel][] = $code;
84
                    }
85
                }
86
            }
87
        }
88
89
        $data['attributes'] = implode(',', $data['attributes']);
90
91
        return $data;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function configureOptions(OptionsResolver $resolver)
98
    {
99
        parent::configureOptions($resolver);
100
101
        $resolver->remove([
102
            'header_key',
103
        ]);
104
105
        $resolver->setRequired([
106
            'family_data_row',
107
            'family_code_column',
108
            'family_labels_locales_row',
109
            'family_labels_first_column',
110
            'channel_label_row',
111
            'attribute_label_row',
112
            'attribute_data_row',
113
        ]);
114
    }
115
}
116