Completed
Push — master ( 6c1cc1...b9be17 )
by Jeroen
9s
created

JsonLinesReader::serialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace TreeHouse\Feeder\Reader;
4
5
use Symfony\Component\HttpFoundation\ParameterBag;
6
use TreeHouse\Feeder\Exception\ReadException;
7
use TreeHouse\Feeder\Resource\ResourceInterface;
8
9
class JsonLinesReader extends AbstractReader
10
{
11
    /**
12
     * @var \SplFileObject
13
     */
14
    protected $fileObject;
15
16
    /**
17
     * Serializes a read item into a ParameterBag.
18
     *
19
     * @param string $data
20
     *
21
     * @return ParameterBag
22
     * 
23
     * @throws ReadException
24
     */
25
    protected function serialize($data)
26
    {
27
        if ('' === trim($data)) {
28
            return null;
29
        }
30
31
        if (null === $result = json_decode($data, true)) {
32
            if (json_last_error() !== JSON_ERROR_NONE) {
33
                throw new ReadException(json_last_error_msg(), json_last_error());
34
            }
35
        }
36
37
        return new ParameterBag($result);
38
    }
39
40
    /**
41
     * Creates a reader for a resource.
42
     *
43
     * @param ResourceInterface $resource
44
     */
45
    protected function createReader(ResourceInterface $resource)
46
    {
47
        $jsonFile = $resource->getFile()->getPathname();
48
49
        $this->fileObject = new \SplFileObject($jsonFile);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    protected function doKey()
56
    {
57
        return $this->fileObject->key();
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    protected function doCurrent()
64
    {
65
        return $this->fileObject->current();
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    protected function doNext()
72
    {
73
        $this->fileObject->next();
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    protected function doValid()
80
    {
81
        return $this->fileObject->valid();
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    protected function doRewind()
88
    {
89
        $this->fileObject->rewind();
90
    }
91
}
92