Passed
Pull Request — master (#15)
by Christopher
03:06
created

SearchDsl   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 74
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 10 3
A __construct() 0 4 1
A __call() 0 5 1
A model() 0 13 2
1
<?php
2
namespace Triadev\Leopard\Business\Dsl;
3
4
use ONGR\ElasticsearchDSL\Search as OngrSearch;
5
use Illuminate\Database\Eloquent\Model;
6
use Triadev\Es\Dsl\Facade\ElasticDsl;
7
use Triadev\Es\Dsl\Model\SearchResult;
8
use Triadev\Leopard\Business\Filler\EloquentFiller;
9
use Triadev\Leopard\Business\Helper\IsModelSearchable;
10
use Triadev\Leopard\Contract\FillerContract;
11
use Triadev\Leopard\Searchable;
12
13
class SearchDsl
14
{
15
    use IsModelSearchable;
16
    
17
    /** @var \Triadev\Es\Dsl\Dsl\Search */
18
    private $dsl;
19
    
20
    /** @var Model|null */
21
    public $model;
22
    
23
    /**
24
     * SearchDsl constructor.
25
     * @param OngrSearch|null $search
26
     * @param Model|null $model
27
     */
28 2
    public function __construct(?OngrSearch $search = null, ?Model $model = null)
29
    {
30 2
        $this->dsl = ElasticDsl::search($search);
31 2
        $this->model = $model;
32 2
    }
33
    
34
    /**
35
     * Add model
36
     *
37
     * @param Model|Searchable $model
38
     * @return SearchDsl|\Triadev\Es\Dsl\Dsl\Search
39
     *
40
     * @throws \InvalidArgumentException
41
     */
42 1
    public function model(Model $model) : SearchDsl
43
    {
44 1
        $this->isModelSearchable($model);
45
        
46 1
        $this->model = $model;
47
        
48 1
        if (is_string($index = $model->getDocumentIndex())) {
49 1
            $this->dsl->esIndex($index);
50
        }
51
        
52 1
        $this->dsl->esType($model->getDocumentType());
0 ignored issues
show
Bug introduced by
It seems like $model->getDocumentType() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $type of Triadev\Es\Dsl\Dsl\AbstractSearch::esType() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
        $this->dsl->esType(/** @scrutinizer ignore-type */ $model->getDocumentType());
Loading history...
53
        
54 1
        return $this;
55
    }
56
    
57
    /**
58
     * Get
59
     *
60
     * @param FillerContract|null $filler
61
     * @return SearchResult
62
     */
63 2
    public function get(?FillerContract $filler = null) : SearchResult
64
    {
65 2
        $searchResult = $this->dsl->get();
66
        
67 2
        if ($this->model) {
68 1
            $filler = $filler ?: new EloquentFiller();
69 1
            $filler->fill($this->model, $searchResult);
70
        }
71
        
72 2
        return $searchResult;
73
    }
74
    
75
    /**
76
     * Call methods in elasticsearch dsl
77
     *
78
     * @param $name
79
     * @param $arguments
80
     * @return SearchDsl
81
     */
82 2
    public function __call($name, $arguments) : SearchDsl
83
    {
84 2
        call_user_func_array([$this->dsl, $name], $arguments);
85
        
86 2
        return $this;
87
    }
88
}
89