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

Json   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 36
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2
ccs 0
cts 22
cp 0
rs 10

3 Methods

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