Passed
Push — master ( c82b6f...43d2f1 )
by Brent
02:59
created

JsonProvider::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\provider;
4
5
use brendt\stitcher\exception\ProviderException;
6
use Symfony\Component\Finder\Finder;
7
8
class JsonProvider extends AbstractArrayProvider {
9
10
    public function parse($path = '*.json') {
11
        $finder = new Finder();
12
        $data = [];
13
        if (!strpos($path, '.json')) {
14
            $path .= '.json';
15
        }
16
17
        $files = $finder->files()->in("{$this->root}")->path($path);
18
19
        foreach ($files as $file) {
20
            $parsed = json_decode($file->getContents(), true);
21
22
            if (json_last_error() > 0 && $error = json_last_error_msg()) {
23
                throw new ProviderException("{$file->getRelativePathname()}: {$error}");
24
            }
25
26 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...
27
                $data += $parsed['entries'];
28
            } else {
29
                $id = str_replace(".{$file->getExtension()}", '', $file->getFilename());
30
                $data[$id] = $parsed;
31
            }
32
        }
33
34
        $data = $this->parseArrayData($data);
35
36
        return $data;
37
    }
38
39
}
40