Completed
Push — master ( 005d42...c6a537 )
by ARCANEDEV
9s
created

AbstractImporter::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php namespace Arcanedev\LaravelExcel\Importers;
2
3
use Arcanedev\LaravelExcel\Contracts\Importer as ImporterContract;
4
use Arcanedev\LaravelExcel\Contracts\Parser as ParserContract;
5
use Box\Spout\Reader\ReaderFactory;
6
use Illuminate\Support\Collection;
7
8
/**
9
 * Class     AbstractExporter
10
 *
11
 * @package  Arcanedev\LaravelExcel\Importer
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class AbstractImporter implements ImporterContract
15
{
16
    /* ------------------------------------------------------------------------------------------------
17
     |  Properties
18
     | ------------------------------------------------------------------------------------------------
19
     */
20
    /** @var  string */
21
    protected $type;
22
23
    /** @var  string */
24
    protected $path = '';
25
26
    /** @var  int */
27
    protected $sheet = 0;
28
29
    /** @var  \Arcanedev\LaravelExcel\Contracts\Parser */
30
    protected $parser;
31
32
    /** @var \Box\Spout\Reader\ReaderInterface */
33
    protected $reader;
34
35
    /** @var array */
36
    protected $options = [];
37
38
    /* ------------------------------------------------------------------------------------------------
39
     |  Constructor
40
     | ------------------------------------------------------------------------------------------------
41
     */
42
    /**
43
     * AbstractImporter constructor.
44
     *
45
     * @param  array  $options
46
     */
47 42
    public function __construct(array $options = [])
48
    {
49 42
        $this->setParser(new DefaultParser);
50 42
        $this->setOptions($options);
51 42
    }
52
53
    /* ------------------------------------------------------------------------------------------------
54
     |  Getters & Setters
55
     | ------------------------------------------------------------------------------------------------
56
     */
57
    /**
58
     * Set the path.
59
     *
60
     * @param  string  $path
61
     *
62
     * @return self
63
     */
64 12
    public function setPath($path)
65
    {
66 12
        $this->path = $path;
67
68 12
        return $this;
69
    }
70
71
    /**
72
     * Set the sheet number.
73
     *
74
     * @param  int  $sheet
75
     *
76
     * @return self
77
     */
78 12
    public function setSheet($sheet)
79
    {
80 12
        $this->sheet = $sheet;
81
82 12
        return $this;
83
    }
84
85
    /**
86
     * Set the parser.
87
     *
88
     * @param  \Arcanedev\LaravelExcel\Contracts\Parser  $parser
89
     *
90
     * @return self
91
     */
92 42
    public function setParser(ParserContract $parser)
93
    {
94 42
        $this->parser = $parser;
95
96 42
        return $this;
97
    }
98
99
    /**
100
     * Get the file type.
101
     *
102
     * @return string
103
     */
104 30
    public function getType()
105
    {
106 30
        return $this->type;
107
    }
108
109
    /**
110
     * Set the reader options.
111
     *
112
     * @param  array  $options
113
     *
114
     * @return self
115
     */
116 42
    public function setOptions(array $options)
117
    {
118 42
        $this->options = $options;
119
120 42
        return $this;
121
    }
122
123
    /* ------------------------------------------------------------------------------------------------
124
     |  Main Functions
125
     | ------------------------------------------------------------------------------------------------
126
     */
127
    /**
128
     * Load the file.
129
     *
130
     * @param  string  $path
131
     *
132
     * @return self
133
     */
134 12
    public function load($path)
135
    {
136 12
        return $this->setPath($path);
137
    }
138
139
    /**
140
     * Get the parsed data.
141
     *
142
     * @return \Illuminate\Support\Collection
143
     */
144 12
    public function get()
145
    {
146 12
        $this->create();
147 12
        $this->reader->open($this->path);
148 12
        $collection = $this->parseRows();
149 12
        $this->reader->close();
150
151 12
        return $collection;
152
    }
153
154
    /* ------------------------------------------------------------------------------------------------
155
     |  Other Functions
156
     | ------------------------------------------------------------------------------------------------
157
     */
158
    /**
159
     * Create the reader instance.
160
     */
161 12
    protected function create()
162
    {
163 12
        $this->reader = ReaderFactory::create($this->type);
164 12
        $this->loadOptions();
165 12
    }
166
167
    /**
168
     * Load the reader options.
169
     */
170
    abstract protected function loadOptions();
171
172
    /**
173
     * Parse the rows.
174
     *
175
     * @return \Illuminate\Support\Collection
176
     */
177 12
    protected function parseRows()
178
    {
179 12
        $collection = Collection::make();
180
181 12
        foreach ($this->reader->getSheetIterator() as $index => $sheet) {
182 12
            if ($index !== $this->sheet) continue;
183
184 12
            foreach ($sheet->getRowIterator() as $row) {
185 12
                $collection->push(
186 12
                    $this->parser->transform($row)
187 6
                );
188 6
            }
189 6
        }
190
191 12
        return $collection;
192
    }
193
}
194