BulkWriter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 39
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A prepare() 0 4 1
A writeItem() 0 4 1
A finish() 0 6 1
A flush() 0 7 1
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
}