Completed
Push — master ( ecd220...59c7a5 )
by GBProd
02:21
created

DataProvider::shouldFlushBulk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace GBProd\ElasticsearchDataProviderBundle\DataProvider;
4
5
use Elasticsearch\Client;
6
use GBProd\ElasticsearchDataProviderBundle\Event\HasIndexedDocument;
7
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
8
9
/**
10
 * Abstract class for data providing
11
 *
12
 * @author gbprod <[email protected]>
13
 */
14
abstract class DataProvider implements DataProviderInterface
15
{
16
    const BATCH_SIZE = 1000;
17
    
18
    /**
19
     * @var Client
20
     */
21
    private $client;
22
23
    /**
24
     * @var string
25
     */
26
    private $index;
27
28
    /**
29
     * @var string
30
     */
31
    private $type;
32
    
33
    /**
34
     * @var EventDispatcherInterface
35
     */
36
    private $dispatcher;
37
38
    /**
39
     * @var int
40
     */
41
    private $currentBulkSize;
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    public function run(
47
        Client $client, 
48
        $index, 
49
        $type, 
50
        EventDispatcherInterface $dispatcher
51
    ) {
52 3
        $this->client     = $client;
53 3
        $this->index      = $index;
54 3
        $this->type       = $type;
55 3
        $this->dispatcher = $dispatcher;
56
        
57 3
        $this->currentBulkSize = 0;
58 3
        $this->currentBulk     = ['body' => []];
0 ignored issues
show
Bug introduced by
The property currentBulk does not seem to exist. Did you mean currentBulkSize?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
59
        
60 3
        $this->populate();
61
    
62 3
        $this->flushBulk();
63 3
    }
64
65
    /**
66
     * Populate
67
     *
68
     * @return null
69
     */
70
    abstract protected function populate();
71
72
    /**
73
     * Index document
74
     *
75
     * @param string $id
76
     * @param array  $body
77
     */
78 2
    public function index($id, array $body)
79
    {
80 2
        $this->currentBulk['body'][] = [
0 ignored issues
show
Bug introduced by
The property currentBulk does not seem to exist. Did you mean currentBulkSize?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
            'index' => [
82 2
                '_index' => $this->index,
83 2
                '_type'  => $this->type,
84 2
                '_id'    => $id,
85
            ]
86 2
        ];
87
    
88 2
        $this->currentBulk['body'][] = $body;
0 ignored issues
show
Bug introduced by
The property currentBulk does not seem to exist. Did you mean currentBulkSize?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
89
        
90 2
        if ($this->shouldFlushBulk()) {
91 1
            $this->flushBulk();
92 1
        }
93
        
94 2
        $this->currentBulkSize++;
95
        
96 2
        $this->dispatcher->dispatch(
97 2
            'elasticsearch.has_indexed_document',
98 2
            new HasIndexedDocument($id)
99 2
        );
100 2
    }
101
    
102 3
    protected function flushBulk()
103
    {
104 3
        $this->client->bulk($this->currentBulk);
0 ignored issues
show
Bug introduced by
The property currentBulk does not seem to exist. Did you mean currentBulkSize?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
105
106 3
        $this->currentBulkSize = 0;
107 3
        $this->currentBulk     = ['body' => []];
0 ignored issues
show
Bug introduced by
The property currentBulk does not seem to exist. Did you mean currentBulkSize?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
108 3
    }
109
    
110 2
    private function shouldFlushBulk()
111
    {
112 2
        return $this->currentBulkSize >= self::BATCH_SIZE;
113
    }
114
    
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function count()
119
    {
120
        return null;
121
    }
122
}    
123