| Total Complexity | 10 |
| Total Lines | 127 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 11 | class ElasticsearchRepository implements ElasticsearchRepositoryContract |
||
| 12 | { |
||
| 13 | use IsModelSearchable; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Save |
||
| 17 | * |
||
| 18 | * @param Model|Searchable $model |
||
| 19 | * @return array |
||
| 20 | * |
||
| 21 | * @throws \InvalidArgumentException |
||
| 22 | */ |
||
| 23 | 5 | public function save(Model $model): array |
|
| 24 | { |
||
| 25 | 5 | $this->isModelSearchable($model); |
|
| 26 | |||
| 27 | 5 | return Leopard::indexStatement([ |
|
|
1 ignored issue
–
show
|
|||
| 28 | 5 | 'index' => $model->getDocumentIndex(), |
|
|
1 ignored issue
–
show
|
|||
| 29 | 5 | 'type' => $model->getDocumentType(), |
|
|
1 ignored issue
–
show
|
|||
| 30 | 5 | 'id' => $model->getKey(), |
|
|
1 ignored issue
–
show
|
|||
| 31 | 5 | 'body' => $model->getDocumentData() |
|
|
1 ignored issue
–
show
|
|||
| 32 | ]); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Update |
||
| 37 | * |
||
| 38 | * @param Model|Searchable $model |
||
| 39 | * @return array |
||
| 40 | * |
||
| 41 | * @throws \InvalidArgumentException |
||
| 42 | */ |
||
| 43 | 1 | public function update(Model $model): array |
|
| 44 | { |
||
| 45 | 1 | $this->isModelSearchable($model); |
|
| 46 | |||
| 47 | 1 | return Leopard::updateStatement([ |
|
|
1 ignored issue
–
show
|
|||
| 48 | 1 | 'index' => $model->getDocumentIndex(), |
|
|
1 ignored issue
–
show
|
|||
| 49 | 1 | 'type' => $model->getDocumentType(), |
|
|
1 ignored issue
–
show
|
|||
| 50 | 1 | 'id' => $model->getKey(), |
|
|
1 ignored issue
–
show
|
|||
| 51 | 'body' => [ |
||
|
1 ignored issue
–
show
|
|||
| 52 | 1 | 'doc' => $model->getDocumentData() |
|
| 53 | ] |
||
| 54 | ]); |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Delete |
||
| 59 | * |
||
| 60 | * @param Model|Searchable $model |
||
| 61 | * @return bool |
||
| 62 | * |
||
| 63 | * @throws \InvalidArgumentException |
||
| 64 | */ |
||
| 65 | 1 | public function delete(Model $model): bool |
|
| 66 | { |
||
| 67 | 1 | $this->isModelSearchable($model); |
|
| 68 | |||
| 69 | $params = [ |
||
| 70 | 1 | 'index' => $model->getDocumentIndex(), |
|
| 71 | 1 | 'type' => $model->getDocumentType(), |
|
| 72 | 1 | 'id' => $model->getKey(), |
|
| 73 | ]; |
||
| 74 | |||
| 75 | 1 | if (Leopard::existStatement($params)) { |
|
| 76 | 1 | Leopard::deleteStatement($params); |
|
| 77 | } |
||
| 78 | |||
| 79 | 1 | return true; |
|
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Bulk save |
||
| 84 | * |
||
| 85 | * @param array|Collection $models |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 2 | public function bulkSave($models) : array |
|
| 89 | { |
||
| 90 | 2 | $params = []; |
|
| 91 | |||
| 92 | 2 | $defaultIndex = Leopard::getEsDefaultIndex(); |
|
| 93 | |||
| 94 | 2 | foreach ($models as $model) { |
|
| 95 | /** @var Model|Searchable $model */ |
||
| 96 | 2 | $this->isModelSearchable($model); |
|
|
1 ignored issue
–
show
|
|||
| 97 | |||
| 98 | 2 | $params['body'][] = [ |
|
| 99 | 'index' => [ |
||
| 100 | 2 | '_index' => $model->getDocumentIndex() ?: $defaultIndex, |
|
| 101 | 2 | '_type' => $model->getDocumentType(), |
|
| 102 | 2 | '_id' => $model->getKey() |
|
| 103 | ] |
||
| 104 | ]; |
||
| 105 | |||
| 106 | 2 | $params['body'][] = $model->getDocumentData(); |
|
| 107 | } |
||
| 108 | |||
| 109 | 2 | return Leopard::bulkStatement($params); |
|
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Bulk delete |
||
| 114 | * |
||
| 115 | * @param array $models |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | 1 | public function bulkDelete(array $models): array |
|
| 138 | } |
||
| 139 | } |
||
| 140 |