BulkWriter::finish()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace Port\ElasticSearch;
4
5
use Port\Writer\FlushableWriter;
6
use Elasticsearch\Client;
7
8
class BulkWriter implements FlushableWriter
9
{
10
    private $client;
11
12
    private $request;
13
14
    private $items;    
15
16
    public function __construct(Client $client, array $request = [])
17
    {
18
        $this->client = $client;
19
        $this->request = $request;
20
    }
21
22
    public function prepare()
23
    {
24
        $this->items = [];
25
    }
26
27
    public function writeItem(array $item)
28
    {        
29
        $this->items[] = $item;
30
    }
31
32
    public function finish()
33
    {
34
        $this->flush();
35
36
        $this->items = [];
37
    }
38
39
    public function flush()
40
    {        
41
        $req = $this->request;
42
        $req['body'] = $this->items;
43
44
        $this->client->bulk($req);
45
    }
46
}