Completed
Push — master ( c76e35...2b89c4 )
by Max
03:09
created

Parser   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 6
Bugs 1 Features 2
Metric Value
wmc 11
c 6
b 1
f 2
lcom 1
cbo 2
dl 0
loc 106
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 21 2
A stop() 0 4 1
A checkCallback() 0 6 2
A openFile() 0 13 3
A setOption() 0 4 1
A getOption() 0 8 2
1
<?php
2
namespace JsonCollectionParser;
3
4
class Parser
5
{
6
    /**
7
     * @var array
8
     */
9
    protected $options = array(
10
        'line_ending' => "\n",
11
        'emit_whitespace' => false
12
    );
13
14
    /**
15
     * @var \JsonStreamingParser\Parser
16
     */
17
    protected $parser;
18
19
    /**
20
     * @param $filePath
21
     * @param $itemCallback
22
     *
23
     * @throws \Exception
24
     */
25
    public function parse($filePath, $itemCallback)
26
    {
27
        $this->checkCallback($itemCallback);
28
29
        $stream = $this->openFile($filePath);
30
31
        try {
32
            $listener = new Listener($itemCallback);
33
            $this->parser = new \JsonStreamingParser\Parser(
34
                $stream,
35
                $listener,
36
                $this->getOption('line_ending'),
37
                $this->getOption('emit_whitespace')
38
            );
39
            $this->parser->parse();
40
        } catch (\Exception $e) {
41
            fclose($stream);
42
            throw $e;
43
        }
44
        fclose($stream);
45
    }
46
47
    /**
48
     *
49
     */
50
    public function stop()
51
    {
52
        $this->parser->stop();
53
    }
54
55
    /**
56
     * @param $callback
57
     *
58
     * @throws \Exception
59
     */
60
    protected function checkCallback($callback)
61
    {
62
        if (!is_callable($callback)) {
63
            throw new \Exception("Callback should be callable");
64
        }
65
    }
66
67
    /**
68
     * @param $filePath
69
     *
70
     * @return resource
71
     * @throws \Exception
72
     */
73
    protected function openFile($filePath)
74
    {
75
        if (!is_file($filePath)) {
76
            throw new \Exception('File does not exist: ' . $filePath);
77
        }
78
79
        $stream = @fopen($filePath, 'r');
80
        if (false === $stream) {
81
            throw new \Exception('Unable to open file for read: ' . $filePath);
82
        }
83
84
        return $stream;
85
    }
86
87
    /**
88
     * @param string $name
89
     * @param $value
90
     */
91
    public function setOption($name, $value)
92
    {
93
        $this->options[$name] = $value;
94
    }
95
96
    /**
97
     * @param string $name
98
     *
99
     * @return mixed
100
     */
101
    public function getOption($name)
102
    {
103
        if (isset($this->options[$name])) {
104
            return $this->options[$name];
105
        } else {
106
            return null;
107
        }
108
    }
109
}
110