Completed
Pull Request — master (#6)
by
unknown
02:04
created

Json::getFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Dyrynda\Artisan\BulkImport\Handlers;
4
5
use Dyrynda\Artisan\Exceptions\ImportFileException;
6
7
class Json extends Base
8
{
9 8
    public function getData()
10
    {
11 8
        return json_decode(file_get_contents($this->filePath), true, 512, JSON_BIGINT_AS_STRING);
12
    }
13
14 9
    protected function validateSyntax()
15
    {
16 9
        $data = json_decode(file_get_contents($this->filePath), true);
17
18 9
        if (json_last_error()) {
19 1
            throw ImportFileException::invalidSyntax($this->file->getFilename());
20
        }
21
22 8
        $fields = array_keys($data[0]);
23
24 8
        foreach ($data as $row) {
25 8
            if (count($fields) != count($row) || count(array_intersect($fields, array_keys($row))) != count($fields)) {
26 8
                throw ImportFileException::invalidSyntax($this->file->getFilename(), 'Fields not consistent');
27
            }
28
        }
29 8
    }
30
31
    /**
32
     * Get list of columns from the file.
33
     *
34
     * @return array
35
     */
36
    protected function getFields()
37
    {
38
        $data = json_decode(file_get_contents($this->filePath), true);
39
40
        return isset($data[0]) ? array_map('strtolower', array_map('trim', array_keys($data[0]))) : [];
41
    }
42
}
43