JsonLinesReader   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 73.08%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 11
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 89
ccs 19
cts 26
cp 0.7308
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 14 4
A createReader() 0 12 2
A doKey() 0 4 1
A doCurrent() 0 4 1
A doNext() 0 4 1
A doValid() 0 4 1
A doRewind() 0 4 1
1
<?php
2
3
namespace TreeHouse\Feeder\Reader;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Exception\EmptyResponseException;
7
use TreeHouse\Feeder\Exception\ReadException;
8
use TreeHouse\Feeder\Resource\ResourceInterface;
9
10
class JsonLinesReader extends AbstractReader
11
{
12
    /**
13
     * @var \Iterator
14
     */
15
    protected $fileObject;
16
17
    /**
18
     * Serializes a read item into a ParameterBag.
19
     *
20
     * @param string $data
21
     *
22
     * @return ParameterBag
23
     *
24
     * @throws ReadException
25
     */
26 6
    protected function serialize($data)
27
    {
28 6
        if ('' === trim($data)) {
29 4
            return null;
30
        }
31
32 4
        if (null === $result = json_decode($data, true)) {
33 2
            if (json_last_error() !== JSON_ERROR_NONE) {
34 2
                throw new ReadException(json_last_error_msg(), json_last_error());
35
            }
36
        }
37
38 2
        return new ParameterBag($result);
39
    }
40
41
    /**
42
     * Creates a reader for a resource.
43
     *
44
     * @param ResourceInterface $resource
45
     */
46 6
    protected function createReader(ResourceInterface $resource)
47
    {
48
        try {
49 6
            $jsonFile = $resource->getFile()->getPathname();
50
51 6
            $this->fileObject = new \SplFileObject($jsonFile);
52 6
        } catch (EmptyResponseException $e) {
53
            // getting an empty response is not a problem for jsonlines files, this
54
            // just means that there are no records.
55
            $this->fileObject = new \ArrayIterator();
56
        }
57 6
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    protected function doKey()
63
    {
64
        return $this->fileObject->key();
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 6
    protected function doCurrent()
71
    {
72 6
        return $this->fileObject->current();
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78 6
    protected function doNext()
79
    {
80 6
        $this->fileObject->next();
81 6
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 6
    protected function doValid()
87
    {
88 6
        return $this->fileObject->valid();
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    protected function doRewind()
95
    {
96
        $this->fileObject->rewind();
97
    }
98
}
99