JsonParser   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%
Metric Value
dl 0
loc 53
wmc 6
lcom 1
cbo 1
ccs 20
cts 20
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 13 3
A getFieldsFromContent() 0 10 2
1
<?php
2
3
namespace Fmaj\LaposteDatanovaBundle\Parser;
4
5
use Fmaj\LaposteDatanovaBundle\Service\Finder;
6
7
class JsonParser implements ParserInterface
8
{
9
    /** File format */
10
    const FORMAT = 'json';
11
12
    /**
13
     * @var Finder
14
     */
15
    private $finder;
16
17
    /**
18
     * @param Finder $finder
19
     */
20 1
    public function __construct(Finder $finder)
21
    {
22 1
        $this->finder = $finder;
23 1
    }
24
25
    /**
26
     * @param string $dataset
27
     *
28
     * @return false|array
29
     */
30 1
    public function parse($dataset)
31
    {
32 1
        $data = false;
33 1
        $path = $this->finder->findDataset($dataset, self::FORMAT);
34 1
        if (false !== $path) {
35 1
            $content = $this->finder->getContent($path);
36 1
            if (null !== $content) {
37 1
                $data = $this->getFieldsFromContent($content);
38 1
            }
39 1
        }
40
41 1
        return $data;
42
    }
43
44
    /**
45
     * @param string $content json content
46
     *
47
     * @return array
48
     */
49 1
    private function getFieldsFromContent($content)
50
    {
51 1
        $data = array();
52 1
        $content = json_decode($content, true);
53 1
        foreach ($content as $record) {
54 1
            $data[] = $record['fields'];
55 1
        }
56
57 1
        return $data;
58
    }
59
}
60