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

InitAttributesFileIterator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getAttributeTypesMapper() 0 4 1
A rewind() 0 5 1
A configureOptions() 0 12 1
B initializeAttributeTypes() 0 24 6
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Iterator;
4
5
use Pim\Bundle\ExcelConnectorBundle\Mapper\AttributeTypeMapperInterface;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * XSLX file iterator
10
 *
11
 * @author    JM Leroux <[email protected]>
12
 * @author    Antoine Guigan <[email protected]>
13
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
14
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
15
 */
16
class InitAttributesFileIterator extends InitFileIterator
17
{
18
    /** @var AttributeTypeMapperInterface */
19
    protected $attributeTypesMapper;
20
21
    public function __construct($type, $filePath, array $options)
22
    {
23
        parent::__construct($type, $filePath, $options);
24
        $this->attributeTypesMapper = $options['attribute_types_mapper'];
25
    }
26
27
    /**
28
     * @return AttributeTypeMapperInterface
29
     */
30
    public function getAttributeTypesMapper()
31
    {
32
        return $this->attributeTypesMapper;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function rewind()
39
    {
40
        $this->initializeAttributeTypes();
41
        parent::rewind();
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configureOptions(OptionsResolver $resolver)
48
    {
49
        parent::configureOptions($resolver);
50
        $resolver->setDefaults(
51
            [
52
                'skip_empty' => true,
53
            ]
54
        );
55
        $resolver->setRequired([
56
            'attribute_types_mapper',
57
        ]);
58
    }
59
60
    /**
61
     * Returns the Excel Helper service
62
     *
63
     * @throws \RuntimeException
64
     */
65
    protected function initializeAttributeTypes()
66
    {
67
        $spout = $this->reader;
68
        $spout->getSheetIterator()->rewind();
69
70
        $attributeTypesWorksheet = null;
71
72
        foreach ($spout->getSheetIterator() as $worksheet) {
73
            if ('attribute_types' === $worksheet->getName()) {
74
                $attributeTypesWorksheet = $worksheet;
75
                break;
76
            }
77
        }
78
79
        if (null === $attributeTypesWorksheet) {
80
            throw new \RuntimeException('No attribute_types worksheet in the excel file');
81
        }
82
83
        foreach ($attributeTypesWorksheet->getRowIterator() as $key => $row) {
84
            if ($key >= 2) {
85
                $this->attributeTypesMapper->addAttributeTypeMapping($row[1], $row[0]);
86
            }
87
        }
88
    }
89
}
90