convertNumericIdentifierToString()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Pim\Bundle\ExcelConnectorBundle\Reader;
4
5
use Symfony\Component\HttpFoundation\File\File;
6
use Symfony\Component\Validator\Constraints as Assert;
7
use Akeneo\Bundle\BatchBundle\Item\UploadedFileAwareInterface;
8
use Pim\Bundle\ExcelConnectorBundle\Iterator\FileIteratorFactory;
9
use Pim\Bundle\CatalogBundle\Validator\Constraints\File as AssertFile;
10
11
/**
12
 * File iterator reader
13
 *
14
 * @author    Antoine Guigan <[email protected]>
15
 * @copyright 2013 Akeneo SAS (http://www.akeneo.com)
16
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
17
 */
18
class FileIteratorReader extends AbstractIteratorReader implements UploadedFileAwareInterface
19
{
20
    /** @var FileIteratorFactory */
21
    protected $iteratorFactory;
22
23
    /** @var string */
24
    protected $iteratorClass;
25
26
    /** @var array */
27
    protected $iteratorOptions;
28
29
    /**
30
     * @var string
31
     *
32
     * @Assert\NotBlank(groups={"Execution"})
33
     * @AssertFile(groups={"Execution"})
34
     */
35
    protected $filePath;
36
37
    /**
38
     * @var boolean
39
     *
40
     * @Assert\Type(type="bool")
41
     * @Assert\IsTrue(groups={"UploadExecution"})
42
     */
43
    protected $uploadAllowed = false;
44
45
    /**
46
     * @param FileIteratorFactory $iteratorFactory
47
     * @param string              $iteratorClass
48
     * @param array               $iteratorOptions
49
     * @param bool                $batchMode
50
     */
51
    public function __construct(
52
        FileIteratorFactory $iteratorFactory,
53
        $iteratorClass,
54
        array $iteratorOptions = array(),
55
        $batchMode = false
56
    ) {
57
        parent::__construct($batchMode);
58
59
        $this->iteratorFactory = $iteratorFactory;
60
        $this->iteratorClass = $iteratorClass;
61
        $this->iteratorOptions = $iteratorOptions;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getConfigurationFields()
68
    {
69
        return array(
70
            'filePath' => array(
71
                'options' => array(
72
                    'label' => 'pim_base_connector.import.filePath.label',
73
                    'help'  => 'pim_base_connector.import.filePath.help'
74
                )
75
            ),
76
            'uploadAllowed' => array(
77
                'type'    => 'switch',
78
                'options' => array(
79
                    'label' => 'pim_base_connector.import.uploadAllowed.label',
80
                    'help'  => 'pim_base_connector.import.uploadAllowed.help'
81
                )
82
            ),
83
        );
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function getUploadedFileConstraints()
90
    {
91
        return array(
92
            new Assert\NotBlank(),
93
            new AssertFile()
94
        );
95
    }
96
97
    /**
98
     * @param File $uploadedFile
99
     *
100
     * @return FileIteratorReader
101
     */
102
    public function setUploadedFile(File $uploadedFile)
103
    {
104
        $this->filePath = $uploadedFile->getRealPath();
105
        $this->initialize();
106
107
        return $this;
108
    }
109
110
    /**
111
     * @param string $filePath
112
     *
113
     * @return FileIteratorReader
114
     */
115
    public function setFilePath($filePath)
116
    {
117
        $this->filePath = $filePath;
118
        $this->initialize();
119
120
        return $this;
121
    }
122
123
    /**
124
     * @param boolean $uploadAllowed
125
     *
126
     * @return FileIteratorReader
127
     */
128
    public function setUploadAllowed($uploadAllowed)
129
    {
130
        $this->uploadAllowed = $uploadAllowed;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @return boolean $uploadAllowed
137
     */
138
    public function isUploadAllowed()
139
    {
140
        return $this->uploadAllowed;
141
    }
142
143
    /**
144
     * @return string $filePath
145
     */
146
    public function getFilePath()
147
    {
148
        return $this->filePath;
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    protected function createIterator()
155
    {
156
        return $this->iteratorFactory->create($this->iteratorClass, $this->filePath, $this->getIteratorOptions());
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    protected function getIteratorOptions()
163
    {
164
        return $this->iteratorOptions;
165
    }
166
167
    /**
168
     * {@inheritdoc}
169
     */
170
    protected function convertNumericIdentifierToString(array $item)
171
    {
172
        if (isset($item['code']) && is_int($item['code'])) {
173
            $item['code'] = (string) $item['code'];
174
        }
175
176
        return $item;
177
    }
178
}
179