Completed
Pull Request — master (#22)
by Sergey
13:25
created

QueryBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 64.29%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 42
rs 10
c 0
b 0
f 0
ccs 9
cts 14
cp 0.6429

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A getMatchAllQuery() 0 4 1
A build() 0 4 1
A setChunkCount() 0 5 1
A getChunkCount() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace Isswp101\Persimmon\QueryBuilder;
4
5
use Isswp101\Persimmon\Contracts\Stringable;
6
use ONGR\ElasticsearchDSL\Search;
7
8
class QueryBuilder implements IQueryBuilder, Stringable
9
{
10
    protected $query = [];
11
    protected $chunk = 0;
12
13
    /**
14
     * @param Search|array|null $query
15
     */
16
    public function __construct($query = null)
17
    {
18
        if ($query instanceof Search) {
19
            $query = $query->toArray();
20
        }
21
        $this->query = is_array($query) ? $query : $this->getMatchAllQuery();
22 7
    }
23
24 7
    protected function getMatchAllQuery()
25 7
    {
26
        return ['query' => ['match_all' => new \stdClass()]];
27
    }
28
29
    public function build(): array
30
    {
31 1
        return $this->query;
32
    }
33 1
34 1
    public function setChunkCount(int $count): IQueryBuilder
35
    {
36
        $this->chunk = $count;
37
        return $this;
38
    }
39
40
    public function getChunkCount(): int
41 2
    {
42
        return $this->chunk;
43 2
    }
44 2
45
    public function __toString(): string
46
    {
47
        return json_encode($this->query);
48
    }
49
}
50