Test Failed
Push — master ( a58c94...1bcec0 )
by Brent
03:26
created

JsonParser::parse()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 28
Code Lines 17

Duplication

Lines 6
Ratio 21.43 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 8
nop 1
dl 6
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace Brendt\Stitcher\Parser;
4
5
use Brendt\Stitcher\Config;
6
use Brendt\Stitcher\Exception\ParserException;
7
use Symfony\Component\Finder\Finder;
8
9
/**
10
 * The JsonParser take a path to one or more JSON files, and parses the content into an array.
11
 */
12
class JsonParser extends AbstractArrayParser {
13
14
    /**
15
     * @param string $path
16
     *
17
     * @return array
18
     * @throws ParserException
19
     */
20
    public function parse($path = '*.json') {
21
        if (!strpos($path, '.json')) {
22
            $path .= '.json';
23
        }
24
25
        $data = [];
26
        $root = Config::get('directories.src');
27
        $files = Finder::create()->files()->in("{$root}")->path($path);
28
29
        foreach ($files as $file) {
30
            $parsed = json_decode($file->getContents(), true);
31
32
            if (json_last_error() > 0 && $error = json_last_error_msg()) {
33
                throw new ParserException("{$file->getRelativePathname()}: {$error}");
34
            }
35
36 View Code Duplication
            if (isset($parsed['entries'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
37
                $data += $parsed['entries'];
38
            } else {
39
                $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
40
                $data[$id] = $parsed;
41
            }
42
        }
43
44
        $data = $this->parseArrayData($data);
45
46
        return $data;
47
    }
48
49
}
50