| Total Complexity | 21 |
| Total Lines | 138 |
| Duplicated Lines | 0 % |
| Coverage | 86.67% |
| Changes | 0 | ||
| 1 | <?php |
||
| 26 | trait Searchable |
||
| 27 | { |
||
| 28 | /** @var bool */ |
||
| 29 | public $isDocument = false; |
||
|
1 ignored issue
–
show
|
|||
| 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) { |
||
|
1 ignored issue
–
show
|
|||
| 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) { |
||
|
1 ignored issue
–
show
|
|||
| 50 | /** @var Model|Searchable $model */ |
||
| 51 | if ($model->shouldSyncDocument()) { |
||
| 52 | $model->repository()->delete(/** @scrutinizer ignore-type */$model); |
||
|
1 ignored issue
–
show
|
|||
| 53 | } |
||
| 54 | 21 | }); |
|
| 55 | 21 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Repository |
||
| 59 | * |
||
| 60 | * @return ElasticsearchRepositoryContract |
||
| 61 | */ |
||
| 62 | 1 | public function repository() : ElasticsearchRepositoryContract |
|
| 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 |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Get document data |
||
| 111 | * |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | 9 | public function getDocumentData() : array |
|
| 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) |
|
| 164 | } |
||
| 165 | } |
||
| 166 |