AttributeXlsxFileIterator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Iterator;
4
5
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
8
/**
9
 * Attribute XLSX file iterator
10
 *
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 AttributeXlsxFileIterator extends \FilterIterator implements ContainerAwareInterface
16
{
17
    /** @var array */
18
    protected $attributeTypes;
19
20
    /** @var array */
21
    protected $options;
22
23
    /** @var XlsxFileIterator */
24
    private $innerIterator;
25
26
    /**
27
     * @param string $filePath
28
     * @param array  $options
29
     */
30
    public function __construct($filePath, array $options = array())
31
    {
32
        $options['skip_empty'] = true;
33
        $this->innerIterator = new XlsxFileIterator($filePath, $options);
34
        $this->options = $options;
35
36
        parent::__construct($this->innerIterator);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function accept()
43
    {
44
        $data = $this->current();
45
        unset($data['code'], $data['use_as_label']);
46
47
        foreach ($data as $value) {
48
            if (trim($value)) {
49
                return true;
50
            }
51
        }
52
53
        return false;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function current()
60
    {
61
        $item = parent::current();
62
        unset($item['use_as_label']);
63
64
        if (isset($item['type'])) {
65
            if (isset($this->attributeTypes[$item['type']])) {
66
                $item['type'] = $this->attributeTypes[$item['type']];
67
            }
68
        }
69
70
        return $item;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function rewind()
77
    {
78
        $this->initializeAttributeTypes();
79
        parent::rewind();
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function setContainer(ContainerInterface $container = null)
86
    {
87
        $this->container = $container;
88
        $this->innerIterator->setContainer($container);
89
    }
90
91
    /**
92
     * Returns the Excel Helper service
93
     *
94
     * @throws \RuntimeException
95
     */
96
    protected function initializeAttributeTypes()
97
    {
98
        $xls = $this->getInnerIterator()->getExcelObject();
99
        $parserOptions = isset($this->options['parser_options']) ? $this->options['parser_options'] : [] ;
100
        $this->attributeTypes = array();
101
        $attributeWorkseet = $xls->getWorksheetIndex('attribute_types');
102
        if (false === $attributeWorkseet) {
103
            throw new \RuntimeException('No attribute_types worksheet in the excel file');
104
        }
105
        $iterator = $xls->createRowIterator($attributeWorkseet, $parserOptions);
106
107
        foreach ($iterator as $key => $row) {
108
            if ($key >= 2) {
109
                $this->attributeTypes[$row[1]] = $row[0];
110
            }
111
        }
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function __clone()
118
    {
119
        return clone $this;
120
    }
121
}
122