JsonLinesReader::doKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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