1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Isswp101\Persimmon\Repository; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
6
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
7
|
|
|
use Isswp101\Persimmon\Collection\ElasticsearchCollection; |
8
|
|
|
use Isswp101\Persimmon\Collection\IElasticsearchCollection; |
9
|
|
|
use Isswp101\Persimmon\CollectionParser\ElasticsearchCollectionParser; |
10
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
11
|
|
|
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException; |
12
|
|
|
use Isswp101\Persimmon\Exceptions\ModelNotFoundException; |
13
|
|
|
use Isswp101\Persimmon\Model\IElasticsearchModel; |
14
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
15
|
|
|
use Isswp101\Persimmon\Relationship\RelationshipKey; |
16
|
|
|
use Isswp101\Persimmon\Response\ElasticsearchCollectionResponse; |
17
|
|
|
use Isswp101\Persimmon\Response\ElasticsearchItemResponse; |
18
|
|
|
|
19
|
|
|
class ElasticsearchRepository implements IRepository |
20
|
|
|
{ |
21
|
|
|
private $client; |
22
|
|
|
|
23
|
|
|
public function __construct(Client $client) |
24
|
|
|
{ |
25
|
|
|
$this->client = $client; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function instantiate(string $class): Storable |
29
|
|
|
{ |
30
|
|
|
$instance = new $class; |
31
|
|
|
if (!$instance instanceof IElasticsearchModel) { |
32
|
|
|
throw new ClassTypeErrorException(IElasticsearchModel::class); |
33
|
|
|
} |
34
|
|
|
return $instance; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
protected function fill( |
38
|
|
|
Storable $model, |
39
|
|
|
ElasticsearchItemResponse $response, |
40
|
|
|
RelationshipKey $relationshipKey = null |
41
|
|
|
) { |
42
|
|
|
$model->fill($response->source()); |
43
|
|
|
$model->setPrimaryKey($relationshipKey != null ? $relationshipKey->build() : $response->id()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function find($id, string $class, array $columns = []): Storable |
47
|
|
|
{ |
48
|
|
|
$model = $this->instantiate($class); |
49
|
|
|
$relationshipKey = RelationshipKey::parse($id); |
50
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
51
|
|
|
$params = [ |
52
|
|
|
'index' => $collection->getIndex(), |
53
|
|
|
'type' => $collection->getType(), |
54
|
|
|
'id' => $relationshipKey->getId(), |
55
|
|
|
'parent' => $relationshipKey->getParentId(), |
56
|
|
|
'_source' => $columns |
57
|
|
|
]; |
58
|
|
|
try { |
59
|
|
|
$response = new ElasticsearchItemResponse($this->client->get($params)); |
|
|
|
|
60
|
|
|
} catch (Missing404Exception $e) { |
61
|
|
|
throw new ModelNotFoundException($class, $id); |
62
|
|
|
} |
63
|
|
|
$this->fill($model, $response, $relationshipKey); |
64
|
|
|
return $model; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): IElasticsearchCollection |
68
|
|
|
{ |
69
|
|
|
$model = $this->instantiate($class); |
70
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
71
|
|
|
$params = [ |
72
|
|
|
'index' => $collection->getIndex(), |
73
|
|
|
'type' => $collection->getType(), |
74
|
|
|
'body' => $query->build() |
75
|
|
|
]; |
76
|
|
|
$response = new ElasticsearchCollectionResponse($this->client->search($params)); |
|
|
|
|
77
|
|
|
$models = new ElasticsearchCollection([], $response); |
78
|
|
|
foreach ($response->hits() as $hit) { |
79
|
|
|
$model = $this->instantiate($class); |
80
|
|
|
$item = new ElasticsearchItemResponse($hit); |
81
|
|
|
$this->fill($model, $item/*, new RelationshipKey($item->id())*/); |
|
|
|
|
82
|
|
|
if ($callback != null) { |
83
|
|
|
$callback($model); |
84
|
|
|
} |
85
|
|
|
$models->put($model->getPrimaryKey(), $model); |
86
|
|
|
} |
87
|
|
|
return $models; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
View Code Duplication |
public function insert(Storable $model) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$relationshipKey = RelationshipKey::parse($model->getPrimaryKey()); |
93
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
94
|
|
|
$params = [ |
95
|
|
|
'index' => $collection->getIndex(), |
96
|
|
|
'type' => $collection->getType(), |
97
|
|
|
'id' => $relationshipKey->getId(), |
98
|
|
|
'parent' => $relationshipKey->getParentId(), |
99
|
|
|
'body' => $model->toArray() |
100
|
|
|
]; |
101
|
|
|
$this->client->index($params); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
View Code Duplication |
public function update(Storable $model) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$relationshipKey = RelationshipKey::parse($model->getPrimaryKey()); |
107
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
108
|
|
|
$params = [ |
109
|
|
|
'index' => $collection->getIndex(), |
110
|
|
|
'type' => $collection->getType(), |
111
|
|
|
'id' => $relationshipKey->getId(), |
112
|
|
|
'parent' => $relationshipKey->getParentId(), |
113
|
|
|
'body' => $model->toArray() |
114
|
|
|
]; |
115
|
|
|
$this->client->index($params); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function delete(Storable $model) |
119
|
|
|
{ |
120
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
121
|
|
|
$params = [ |
122
|
|
|
'index' => $collection->getIndex(), |
123
|
|
|
'type' => $collection->getType(), |
124
|
|
|
'id' => $model->getPrimaryKey() |
125
|
|
|
]; |
126
|
|
|
$this->client->delete($params); |
127
|
|
|
} |
128
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: