Passed
Branch master (8cb5b2)
by Christopher
04:37
created

Mapping::getDocumentIndex()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Triadev\Leopard\Business\Mapping;
3
4
use Illuminate\Database\Eloquent\Model;
5
use Triadev\Leopard\Business\Helper\IsModelSearchable;
6
use Triadev\Leopard\Facade\Leopard;
7
use Triadev\Leopard\Searchable;
8
9
abstract class Mapping
10
{
11
    use IsModelSearchable;
12
    
13
    /** @var Model|Searchable */
14
    protected $model;
15
    
16
    /** @var string */
17
    protected $index;
18
    
19
    /** @var string */
20
    protected $type;
21
    
22
    /**
23
     * Mapping constructor.
24
     *
25
     * @throws \InvalidArgumentException
26
     */
27 11
    public function __construct()
28
    {
29 11
        $modelClass = $this->model();
30 11
        $this->model = new $modelClass();
31
    
32 11
        $this->isModelSearchable($this->model);
33 10
    }
34
    
35
    /**
36
     * Get mapped eloquent model class
37
     *
38
     * @return string
39
     */
40
    abstract public function model() : string;
41
    
42
    /**
43
     * Map
44
     */
45
    abstract public function map();
46
    
47
    /**
48
     * @param string $index
49
     */
50 2
    public function setDocumentIndex(string $index): void
51
    {
52 2
        $this->index = $index;
53 2
    }
54
    
55
    /**
56
     * @param string $type
57
     */
58 2
    public function setDocumentType(string $type): void
59
    {
60 2
        $this->type = $type;
61 2
    }
62
    
63
    /**
64
     * Get es index
65
     *
66
     * @return string
67
     */
68 10
    public function getDocumentIndex() : string
69
    {
70 10
        if ($this->index) {
71 2
            return $this->index;
72
        }
73
        
74 9
        return $this->model->getDocumentIndex() ?: Leopard::getEsDefaultIndex();
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->model->get...rd::getEsDefaultIndex() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
75
    }
76
    
77
    /**
78
     * Get es type
79
     *
80
     * @return string
81
     */
82 9
    public function getDocumentType() : string
83
    {
84 9
        return $this->type ?: $this->model->getDocumentType();
1 ignored issue
show
Bug Best Practice introduced by
The expression return $this->type ?: $t...odel->getDocumentType() could return the type Illuminate\Database\Eloquent\Builder which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
85
    }
86
}
87