Completed
Pull Request — master (#2028)
by
unknown
10:39
created

AbstractImport::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*************************************************************************************/
3
/*      This file is part of the Thelia package.                                     */
4
/*                                                                                   */
5
/*      Copyright (c) OpenStudio                                                     */
6
/*      email : [email protected]                                                       */
7
/*      web : http://www.thelia.net                                                  */
8
/*                                                                                   */
9
/*      For the full copyright and license information, please view the LICENSE.txt  */
10
/*      file that was distributed with this source code.                             */
11
/*************************************************************************************/
12
13
namespace Thelia\ImportExport\Import;
14
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\HttpFoundation\File\File;
17
use Thelia\Core\Translation\Translator;
18
use Thelia\Model\Lang;
19
20
/**
21
 * Class AbstractImport
22
 * @author Jérôme Billiras <[email protected]>
23
 */
24
abstract class AbstractImport implements \Iterator
25
{
26
    /**
27
     * @var array
28
     */
29
    private $data;
30
31
    /**
32
     * @var \Symfony\Component\HttpFoundation\File\File
33
     */
34
    protected $file;
35
36
    /**
37
     * @var \Thelia\Model\Lang A language model
38
     */
39
    protected $language;
40
41
    /**
42
     * @var array Mandatory columns
43
     */
44
    protected $mandatoryColumns = [];
45
46
    /**
47
     * @var ContainerInterface
48
     */
49
    protected $container;
50
51
    /**
52
     * @var integer Imported row count
53
     */
54
    protected $importedRows = 0;
55
56
    public function current()
57
    {
58
        return current($this->data);
59
    }
60
61
    public function key()
62
    {
63
        return key($this->data);
64
    }
65
66
    public function next()
67
    {
68
        next($this->data);
69
    }
70
71
    public function rewind()
72
    {
73
        reset($this->data);
74
    }
75
76
    public function valid()
77
    {
78
        return key($this->data) !== null;
79
    }
80
81
    /**
82
     * Get data
83
     *
84
     * @return array Parsed data
85
     */
86
    public function getData()
87
    {
88
        return $this->data;
89
    }
90
91
    /**
92
     * Set data
93
     *
94
     * @param array $data Parsed data
95
     *
96
     * @return $this Return $this, allow chaining
97
     */
98
    public function setData(array $data)
99
    {
100
        $this->data = $data;
101
102
        return $this;
103
    }
104
105
    /**
106
     * Get file
107
     *
108
     * @return \Symfony\Component\HttpFoundation\File\File
109
     */
110
    public function getFile()
111
    {
112
        return $this->file;
113
    }
114
115
    /**
116
     * Set file
117
     *
118
     * @param \Symfony\Component\HttpFoundation\File\File $file
119
     *
120
     * @return $this Return $this, allow chaining
121
     */
122
    public function setFile(File $file)
123
    {
124
        $this->file = $file;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Get language
131
     *
132
     * @return \Thelia\Model\Lang A language model
133
     */
134
    public function getLang()
135
    {
136
        return $this->language;
137
    }
138
139
    /**
140
     * Set language
141
     *
142
     * @param null|\Thelia\Model\Lang $language A language model
143
     *
144
     * @return $this Return $this, allow chaining
145
     */
146
    public function setLang(Lang $language = null)
147
    {
148
        $this->language = $language;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Check mandatory columns
155
     *
156
     * @param array $data Data
157
     *
158
     * @return boolean Data contains mandatory columns or not
159
     */
160
    public function checkMandatoryColumns(array $data)
161
    {
162
        $diff = array_diff($this->mandatoryColumns, array_keys($data));
163
164
        if (count($diff) > 0) {
165
            throw new \UnexpectedValueException(
166
                Translator::getInstance()->trans(
167
                    'The following columns are missing: %columns',
168
                    [
169
                        '%columns' => implode(', ', $diff)
170
                    ]
171
                )
172
            );
173
        }
174
    }
175
176
    /**
177
     * Get imported rows
178
     *
179
     * @return int Imported rows count
180
     */
181
    public function getImportedRows()
182
    {
183
        return $this->importedRows;
184
    }
185
186
    /**
187
     * Set imported rows
188
     *
189
     * @param int $importedRows Imported rows count
190
     *
191
     * @return $this Return $this, allow chaining
192
     */
193
    public function setImportedRows($importedRows)
194
    {
195
        $this->importedRows = $importedRows;
196
197
        return $this;
198
    }
199
200
    /**
201
     * @param ContainerInterface $container
202
     */
203
    public function setContainer(ContainerInterface $container)
204
    {
205
        $this->container = $container;
206
    }
207
208
    /**
209
     * @return ContainerInterface
210
     */
211
    protected function getContainer()
212
    {
213
        return $this->container;
214
    }
215
216
    /**
217
     * Import data
218
     *
219
     * @param array $data Data to import
220
     *
221
     * @return null|String String with error, null otherwise
222
     */
223
    abstract public function importData(array $data);
224
}
225