Completed
Push — master ( dceab7...a92a53 )
by Christopher
08:16 queued 04:04
created

AbstractSearch   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 110
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A toDsl() 0 3 1
A getQuery() 0 3 1
A __construct() 0 9 2
A getEsType() 0 3 1
A getCurrentSearch() 0 3 1
A getSearch() 0 3 1
A getEsIndex() 0 3 2
A esType() 0 4 1
A esIndex() 0 4 1
1
<?php
2
namespace Triadev\Es\Dsl\Dsl;
3
4
use ONGR\ElasticsearchDSL\BuilderInterface;
5
use ONGR\ElasticsearchDSL\Search as OngrSearch;
6
use Triadev\Es\Dsl\Dsl\Query\Compound;
7
use Triadev\Es\Dsl\Dsl\Query\Fulltext;
8
use Triadev\Es\Dsl\Dsl\Query\Geo;
9
use Triadev\Es\Dsl\Dsl\Query\InnerHit;
10
use Triadev\Es\Dsl\Dsl\Query\Joining;
11
use Triadev\Es\Dsl\Dsl\Query\Specialized;
12
use Triadev\Es\Dsl\Dsl\Query\TermLevel;
13
14
abstract class AbstractSearch
15
{
16
    /** @var OngrSearch */
17
    protected $search;
18
    
19
    /** @var string|null */
20
    private $esIndex;
21
    
22
    /** @var string|null */
23
    private $esType;
24
    
25
    /**
26
     * AbstractDsl constructor.
27
     * @param OngrSearch|null $search
28
     * @param string|null $esIndex
29
     * @param string|null $esType
30
     */
31 23
    public function __construct(
32
        ?OngrSearch $search = null,
33
        ?string $esIndex = null,
34
        ?string $esType = null
35
    ) {
36 23
        $this->search = $search ?: new OngrSearch();
37
        
38 23
        $this->esIndex = $esIndex;
39 23
        $this->esType = $esType;
40 23
    }
41
    
42
    /**
43
     * Overwrite the default elasticsearch index
44
     *
45
     * @param string $index
46
     * @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized
47
     */
48 1
    public function esIndex(string $index) : AbstractSearch
49
    {
50 1
        $this->esIndex = $index;
51 1
        return $this;
52
    }
53
    
54
    /**
55
     * Get elasticsearch index
56
     *
57
     * @return string
58
     */
59 14
    public function getEsIndex() : string
60
    {
61 14
        return $this->esIndex ?: config('laravel-elasticsearch-dsl.index');
62
    }
63
    
64
    /**
65
     * Overwrite the default elasticsearch type
66
     *
67
     * @param string $type
68
     * @return AbstractDsl|Search|TermLevel|Compound|Fulltext|Geo|InnerHit|Joining|Specialized
69
     */
70 1
    public function esType(string $type) : AbstractSearch
71
    {
72 1
        $this->esType = $type;
73 1
        return $this;
74
    }
75
    
76
    /**
77
     * Get elasticsearch type
78
     *
79
     * @return string|null
80
     */
81 14
    public function getEsType() : ?string
82
    {
83 14
        return $this->esType;
84
    }
85
    
86
    /**
87
     * Get current search
88
     *
89
     * @return OngrSearch
90
     */
91 14
    protected function getCurrentSearch() : OngrSearch
92
    {
93 14
        return $this->search;
94
    }
95
    
96
    /**
97
     * To dsl
98
     *
99
     * @return array
100
     */
101 23
    public function toDsl() : array
102
    {
103 23
        return $this->search->toArray();
104
    }
105
    
106
    /**
107
     * Get search
108
     *
109
     * @return OngrSearch
110
     */
111 2
    public function getSearch() : OngrSearch
112
    {
113 2
        return $this->search;
114
    }
115
    
116
    /**
117
     * Get query
118
     *
119
     * @return BuilderInterface
120
     */
121 11
    public function getQuery() : BuilderInterface
122
    {
123 11
        return $this->search->getQueries();
124
    }
125
}
126