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

Searchable   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 138
ccs 39
cts 45
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 21

8 Methods

Rating   Name   Duplication   Size   Complexity  
A repository() 0 3 1
A shouldSyncDocument() 0 7 2
A getDocumentData() 0 11 4
A getDocumentType() 0 7 2
A bootSearchable() 0 13 3
A __call() 0 11 3
A getDocumentIndex() 0 7 2
A buildDocumentFromArray() 0 19 4
1
<?php
2
namespace Triadev\Leopard;
3
4
use Carbon\Carbon;
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use Triadev\Leopard\Contract\ElasticsearchManagerContract;
8
use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract;
9
use Triadev\Leopard\Facade\Leopard;
10
11
/**
12
 * Trait Searchable
13
 * @package Triadev\Leopard
14
 *
15
 * @property string $documentIndex
16
 * @property string $documentType
17
 * @property bool $syncDocument
18
 * @property array $searchable
19
 *
20
 * @method static ElasticsearchManagerContract search()
21
 * @methdo static ElasticsearchManagerContract suggest()
22
 * @method array buildDocument()
23
 * @method array toArray()
24
 * @method string getTable()
25
 */
26
trait Searchable
27
{
28
    /** @var bool */
29
    public $isDocument = false;
30
    
31
    /** @var float|null */
32
    public $documentScore;
33
    
34
    /** @var int|null */
35
    public $documentVersion;
36
    
37
    /**
38
     * Searchable boot model.
39
     */
40 21
    public static function bootSearchable()
41
    {
42
        static::saved(function (Model $model) {
43
            /** @var Model|Searchable $model */
44 1
            if ($model->shouldSyncDocument()) {
45 1
                $model->repository()->save(/** @scrutinizer ignore-type */$model);
46
            }
47 21
        });
48
    
49
        static::deleted(function (Model $model) {
50
            /** @var Model|Searchable $model */
51
            if ($model->shouldSyncDocument()) {
52
                $model->repository()->delete(/** @scrutinizer ignore-type */$model);
1 ignored issue
show
Unused Code introduced by
The call to Illuminate\Database\Eloquent\Builder::delete() has too many arguments starting with $model. ( Ignorable by Annotation )

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

52
                $model->repository()->/** @scrutinizer ignore-call */ delete(/** @scrutinizer ignore-type */$model);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
53
            }
54 21
        });
55 21
    }
56
    
57
    /**
58
     * Repository
59
     *
60
     * @return ElasticsearchRepositoryContract
61
     */
62 1
    public function repository() : ElasticsearchRepositoryContract
63
    {
64 1
        return app(ElasticsearchRepositoryContract::class);
65
    }
66
    
67
    /**
68
     * Get document index
69
     *
70
     * @return string|null
71
     */
72 15
    public function getDocumentIndex(): ?string
73
    {
74 15
        if (property_exists($this, 'documentIndex')) {
75 14
            return $this->documentIndex;
76
        }
77
        
78 1
        return null;
79
    }
80
    
81
    /**
82
     * Get document type
83
     *
84
     * @return string
85
     */
86 13
    public function getDocumentType(): string
87
    {
88 13
        if (property_exists($this, 'documentType')) {
89 12
            return (string)$this->documentType;
90
        }
91
        
92 1
        return (string)$this->getTable();
93
    }
94
    
95
    /**
96
     * Should sync document
97
     *
98
     * @return bool
99
     */
100 2
    public function shouldSyncDocument() : bool
101
    {
102 2
        if (property_exists($this, 'syncDocument')) {
103 2
            return (bool)$this->syncDocument;
104
        }
105
        
106 1
        return true;
107
    }
108
    
109
    /**
110
     * Get document data
111
     *
112
     * @return array
113
     */
114 9
    public function getDocumentData() : array
115
    {
116 9
        if (method_exists($this, 'buildDocument')) {
117 7
            return $this->buildDocument();
118
        }
119
        
120 2
        if (property_exists($this, 'searchable') && is_array($this->searchable)) {
121 1
            return $this->buildDocumentFromArray($this->searchable);
122
        }
123
        
124 1
        return $this->toArray();
125
    }
126
    
127 1
    private function buildDocumentFromArray(array $searchable)
128
    {
129 1
        $document = [];
130
        
131 1
        foreach ($searchable as $value) {
132 1
            $result = $this->$value;
133
            
134 1
            if ($result instanceof Collection) {
135
                $result = $result->toArray();
136 1
            } elseif ($result instanceof Carbon) {
137
                $result = $result->toDateTimeString();
138
            } else {
139 1
                $result = $this->$value;
140
            }
141
            
142 1
            $document[$value] = $result;
143
        }
144
        
145 1
        return $document;
146
    }
147
    
148
    /**
149
     * @param $name
150
     * @param $arguments
151
     * @return mixed
152
     */
153 1
    public function __call($name, $arguments)
154
    {
155 1
        if ($name == 'search') {
156
            return Leopard::search()->model($this);
1 ignored issue
show
Bug introduced by
$this of type Triadev\Leopard\Searchable is incompatible with the type Illuminate\Database\Eloquent\Model expected by parameter $model of Triadev\Leopard\Business\Dsl\Search::model(). ( Ignorable by Annotation )

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

156
            return Leopard::search()->model(/** @scrutinizer ignore-type */ $this);
Loading history...
157
        }
158
        
159 1
        if ($name == 'suggest') {
160
            return Leopard::suggest();
161
        }
162
        
163 1
        return parent::__call($name, $arguments);
164
    }
165
}
166