Completed
Push — 1.0 ( 916c87...0a54d3 )
by Simonas
02:30
created

JsonWriter::initialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
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