Completed
Push — master ( 378c5b...5052d2 )
by Markus
08:56
created

LocalFileStorage   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Test Coverage

Coverage 73.32%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 37
lcom 1
cbo 14
dl 0
loc 175
ccs 55
cts 75
cp 0.7332
rs 8.6
c 5
b 1
f 1

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getFile() 0 4 1
A isReadable() 0 4 2
A __construct() 0 5 1
A getFormat() 0 4 1
A isWritable() 0 4 1
A getAvailableFormats() 0 10 1
A reader() 0 12 3
D formatToReader() 0 28 10
A getFields() 0 8 2
A getHash() 0 8 2
A writer() 0 12 3
B formatToWriter() 0 16 5
B info() 0 14 5
1
<?php
2
3
namespace Mathielen\ImportEngine\Storage;
4
5
use Ddeboer\DataImport\Reader\ArrayReader;
6
use Ddeboer\DataImport\Reader\CsvReader;
7
use Mathielen\DataImport\Reader\ExcelReader;
8
use Ddeboer\DataImport\Reader\ReaderInterface;
9
use Mathielen\DataImport\Writer\CsvWriter;
10
use Mathielen\DataImport\Writer\ExcelWriter;
11
use Mathielen\DataImport\Writer\XmlWriter;
12
use Ddeboer\DataImport\Writer\WriterInterface;
13
use Mathielen\DataImport\Reader\XmlReader;
14
use Mathielen\ImportEngine\Storage\Format\CompressedFormat;
15
use Mathielen\ImportEngine\Storage\Format\Format;
16
use Mathielen\ImportEngine\Storage\Format\CsvFormat;
17
use Mathielen\ImportEngine\Storage\Format\ExcelFormat;
18
use Mathielen\ImportEngine\Storage\Format\JsonFormat;
19
use Mathielen\ImportEngine\Storage\Format\XmlFormat;
20
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
21
22
class LocalFileStorage implements StorageFormatInterface, RecognizableStorageInterface
23
{
24
    /**
25
     * @var \SplFileInfo
26
     */
27
    private $file;
28
29
    /**
30
     * @var Format
31
     */
32
    private $format;
33
34
    /**
35
     * @var ReaderInterface
36
     */
37
    private $reader;
38
39
    /**
40
     * @var WriterInterface
41
     */
42
    private $writer;
43
44
    /**
45
     * @var StorageInfo
46
     */
47
    private $info;
48
49 13
    public function __construct(\SplFileInfo $file, Format $format)
50
    {
51 13
        $this->file = $file;
52 13
        $this->format = $format;
53 13
    }
54
55
    /**
56
     * @return \SplFileInfo
57
     */
58
    public function getFile()
59
    {
60
        return $this->file;
61
    }
62
63
    /**
64
     * @return Format
65
     */
66 9
    public function getFormat()
67
    {
68 9
        return $this->format;
69
    }
70
71 1
    public function getAvailableFormats()
72
    {
73
        return array(
74 1
            'csv',
75
            'excel',
76
            'xml',
77
            'json',
78
            'zlib',
79
        );
80
    }
81
82 11
    public function reader()
83
    {
84 11
        if (!$this->reader) {
85 11
            if (!$this->isReadable()) {
86
                throw new InvalidConfigurationException('Cannot read from file '.$this->file);
87
            }
88
89 11
            $this->reader = $this->formatToReader($this->format, $this->file);
90
        }
91
92 11
        return $this->reader;
93
    }
94
95 11
    public function isReadable()
96
    {
97 11
        return $this->file->isReadable() && $this->file->getSize() > 0;
98
    }
99
100 6
    public function isWritable()
101
    {
102 6
        return is_writable(dirname($this->file));
103
    }
104
105 11
    private function formatToReader(Format $format, \SplFileInfo $file)
106
    {
107 11
        if ($format instanceof CsvFormat) {
108 6
            $reader = new CsvReader($file->openFile(), $format->getDelimiter(), $format->getEnclosure(), $format->getEscape());
109 6
            $reader->setStrict(false);
110 6
            if ($format->isHeaderInFirstRow()) {
111 6
                $reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_MERGE);
112 6
                $reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders())); //TODO some header-collaborator?
113
            }
114
        } elseif ($format instanceof ExcelFormat) {
115 4
            $headerRowNumber = $format->isHeaderInFirstRow() ? 0 : null;
116 4
            $reader = new ExcelReader($file->openFile(), $headerRowNumber, $format->getActivesheet());
117 4
            if ($format->isHeaderInFirstRow()) {
118 4
                $reader->setColumnHeaders(array_map('trim', $reader->getColumnHeaders())); //TODO some header-collaborator?
119
            }
120
        } elseif ($format instanceof JsonFormat) {
121
            $array = json_decode(file_get_contents($file), true);
122
            $reader = new ArrayReader($array);
123
        } elseif ($format instanceof XmlFormat) {
124 1
            $reader = new XmlReader($file->openFile(), $format->getXpath());
125 1
        } elseif ($format instanceof CompressedFormat && $format->getSubFormat()) {
126 1
            $reader = $this->formatToReader($format->getSubFormat(), $format->getInsideStream($file));
127
        } else {
128
            throw new InvalidConfigurationException('Cannot build reader. Unknown format: '.$format);
129
        }
130
131 11
        return $reader;
132
    }
133
134
    public function getFields()
135
    {
136
        if (!$this->isReadable()) {
137
            throw new InvalidConfigurationException('Cannot read from file '.$this->file);
138
        }
139
140
        return $this->reader()->getFields();
141
    }
142
143 4
    public function getHash()
144
    {
145 4
        if (!$this->isReadable()) {
146
            throw new InvalidConfigurationException('Cannot read from file '.$this->file);
147
        }
148
149 4
        return md5_file($this->file->getRealPath());
150
    }
151
152 6
    public function writer()
153
    {
154 6
        if (!$this->writer) {
155 6
            if (!$this->isWritable()) {
156
                throw new InvalidConfigurationException('Cannot write to file '.$this->file);
157
            }
158
159 6
            $this->writer = $this->formatToWriter($this->format, $this->file);
160
        }
161
162 6
        return $this->writer;
163
    }
164
165 6
    private function formatToWriter(Format $format, \SplFileInfo $file)
166
    {
167 6
        if ($format instanceof CsvFormat) {
168 3
            $writer = new CsvWriter($format->getDelimiter(), $format->getEnclosure(), fopen($file, 'a'), false, $format->isHeaderInFirstRow());
169
        } elseif ($format instanceof ExcelFormat) {
170 2
            $writer = new ExcelWriter($file->openFile('a'), $format->getActivesheet(), $format->getExceltype(), $format->isHeaderInFirstRow());
171
        } elseif ($format instanceof XmlFormat) {
172 1
            $writer = new XMLWriter($file->openFile('a'));
173
        } elseif ($format instanceof CompressedFormat) {
174
            throw new \LogicException('Not implemented!');
175
        } else {
176
            throw new InvalidConfigurationException('Cannot build writer. Unknown format: '.$format);
177
        }
178
179 6
        return $writer;
180
    }
181
182 9
    public function info()
183
    {
184 9
        if (!isset($this->info)) {
185 9
            $this->info = new StorageInfo(array(
186 9
                'name' => $this->file->getFilename(),
187 9
                'hash' => $this->isReadable() ? $this->getHash() : null,
188 9
                'format' => $this->getFormat(),
189 9
                'size' => $this->isReadable() ? $this->file->getSize() : 0,
190 9
                'count' => $this->isReadable() ? count($this->reader()) : 0,
191
            ));
192
        }
193
194 9
        return $this->info;
195
    }
196
}
197