BulkQuery   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
eloc 17
c 1
b 0
f 1
dl 0
loc 87
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A isReady() 0 3 1
A reset() 0 3 1
A hasItems() 0 3 1
A addAction() 0 5 1
A toArray() 0 14 2
1
<?php namespace Nord\Lumen\Elasticsearch\Documents\Bulk;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
5
class BulkQuery implements Arrayable
6
{
7
8
    /**
9
     * The number of actions to handle in one query
10
     */
11
    public const BULK_SIZE_DEFAULT = 500;
12
13
    /**
14
     * @var BulkAction[]
15
     */
16
    private $actions = [];
17
18
    /**
19
     * @var int
20
     */
21
    private $bulkSize;
22
23
24
    /**
25
     * BulkQuery constructor.
26
     *
27
     * @param int $bulkSize
28
     */
29
    public function __construct($bulkSize)
30
    {
31
        $this->bulkSize = $bulkSize;
32
    }
33
34
35
    /**
36
     * @param BulkAction $action
37
     *
38
     * @return BulkQuery
39
     */
40
    public function addAction(BulkAction $action)
41
    {
42
        $this->actions[] = $action;
43
44
        return $this;
45
    }
46
47
48
    /**
49
     * @return bool
50
     */
51
    public function hasItems()
52
    {
53
        return !empty($this->actions);
54
    }
55
56
57
    /**
58
     * @return bool whether the query is ready for dispatch
59
     */
60
    public function isReady()
61
    {
62
        return count($this->actions) === $this->bulkSize;
63
    }
64
65
66
    /**
67
     * Removes all actions
68
     */
69
    public function reset()
70
    {
71
        $this->actions = [];
72
    }
73
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function toArray()
79
    {
80
        $result = [
81
            'body' => [],
82
        ];
83
84
        foreach ($this->actions as $i => $action) {
85
            $result['body'][] = [
86
                $action->getAction() => $action->getMetadata()
87
            ];
88
            $result['body'][] = $action->getBody();
89
        }
90
91
        return $result;
92
    }
93
}
94