Completed
Push — 1.0 ( 731e1a...f45751 )
by Simonas
9s
created

JsonWriter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 99
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __destruct() 0 4 1
A initialize() 0 10 2
A finalize() 0 9 2
B push() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchBundle\Service\Json;
13
14
/**
15
 * Serializes records one by one. Outputs given metadata before first record.
16
 *
17
 * Sample output:
18
 * <p>
19
 * [
20
 * {"count":2},
21
 * {"_id":"doc1","title":"Document 1"},
22
 * {"_id":"doc2","title":"Document 2"}
23
 * ]
24
 * </p>
25
 */
26
class JsonWriter
27
{
28
    /**
29
     * @var string
30
     */
31
    private $filename;
32
33
    /**
34
     * @var resource A file system pointer resource.
35
     */
36
    private $handle;
37
38
    /**
39
     * @var array
40
     */
41
    private $metadata;
42
43
    /**
44
     * @var int Current record number.
45
     */
46
    private $currentPosition = 0;
47
48
    /**
49
     * Constructor.
50
     *
51
     * Metadata can contain any fields but only the field "count"
52
     * is recognized and used while writing to file. If written lines count
53
     * reaches "count", writer will automatically finalize file.
54
     *
55
     * @param string $filename A file in which data will be saved.
56
     * @param array  $metadata Additional metadata to be stored.
57
     */
58
    public function __construct($filename, $metadata = [])
59
    {
60
        $this->filename = $filename;
61
        $this->metadata = $metadata;
62
    }
63
64
    /**
65
     * Destructor. Closes file handler if open.
66
     */
67
    public function __destruct()
68
    {
69
        $this->finalize();
70
    }
71
72
    /**
73
     * Performs initialization.
74
     */
75
    protected function initialize()
76
    {
77
        if ($this->handle !== null) {
78
            return;
79
        }
80
81
        $this->handle = fopen($this->filename, 'w');
82
        fwrite($this->handle, "[\n");
83
        fwrite($this->handle, json_encode($this->metadata));
84
    }
85
86
    /**
87
     * Performs finalization.
88
     */
89
    public function finalize()
90
    {
91
        $this->initialize();
92
93
        if (is_resource($this->handle)) {
94
            fwrite($this->handle, "\n]");
95
            fclose($this->handle);
96
        }
97
    }
98
99
    /**
100
     * Writes single document to stream.
101
     *
102
     * @param mixed $document Object to insert into stream.
103
     *
104
     * @throws \OverflowException
105
     */
106
    public function push($document)
107
    {
108
        $this->initialize();
109
        $this->currentPosition++;
110
111
        if (isset($this->metadata['count']) && $this->currentPosition > $this->metadata['count']) {
112
            throw new \OverflowException(
113
                sprintf('This writer was set up to write %d documents, got more.', $this->metadata['count'])
114
            );
115
        }
116
117
        fwrite($this->handle, ",\n");
118
        fwrite($this->handle, json_encode($document));
119
120
        if (isset($this->metadata['count']) && $this->currentPosition == $this->metadata['count']) {
121
            $this->finalize();
122
        }
123
    }
124
}
125