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\ICollection; |
9
|
|
|
use Isswp101\Persimmon\CollectionParser\ElasticsearchCollectionParser; |
10
|
|
|
use Isswp101\Persimmon\Contracts\Storable; |
11
|
|
|
use Isswp101\Persimmon\Exceptions\ClassTypeErrorException; |
12
|
|
|
use Isswp101\Persimmon\QueryBuilder\IQueryBuilder; |
13
|
|
|
use Isswp101\Persimmon\Relationship\RelationshipKey; |
14
|
|
|
use Isswp101\Persimmon\Response\ElasticsearchCollectionResponse; |
15
|
|
|
use Isswp101\Persimmon\Response\ElasticsearchItemResponse; |
16
|
|
|
|
17
|
|
|
class ElasticsearchRepository implements IRepository |
18
|
|
|
{ |
19
|
|
|
private $client; |
20
|
|
|
|
21
|
|
|
public function __construct(Client $client) |
22
|
|
|
{ |
23
|
|
|
$this->client = $client; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function instantiate(string $class): Storable |
27
|
|
|
{ |
28
|
|
|
$instance = new $class; |
29
|
|
|
if (!$instance instanceof Storable) { |
30
|
|
|
throw new ClassTypeErrorException(Storable::class); |
31
|
|
|
} |
32
|
|
|
return $instance; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function fill(Storable $model, ElasticsearchItemResponse $response) |
36
|
|
|
{ |
37
|
|
|
$relationshipKey = new RelationshipKey($response->id(), $response->parent()); |
38
|
|
|
$model->setPrimaryKey($relationshipKey->build()); |
39
|
|
|
$model->fill($response->source()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function find(string $id, string $class, array $columns = []): ?Storable |
43
|
|
|
{ |
44
|
|
|
$model = $this->instantiate($class); |
45
|
|
|
$relationshipKey = RelationshipKey::parse($id); |
46
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
47
|
|
|
$params = [ |
48
|
|
|
'index' => $collection->getIndex(), |
49
|
|
|
'type' => $collection->getType(), |
50
|
|
|
'id' => $relationshipKey->getId(), |
51
|
|
|
'parent' => $relationshipKey->getParentId(), |
52
|
|
|
'_source' => $columns |
53
|
|
|
]; |
54
|
|
|
try { |
55
|
|
|
$response = new ElasticsearchItemResponse($this->client->get($params)); |
|
|
|
|
56
|
|
|
} catch (Missing404Exception $e) { |
57
|
|
|
return null; |
58
|
|
|
} |
59
|
|
|
$this->fill($model, $response); |
60
|
|
|
return $model; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function all(IQueryBuilder $query, string $class, callable $callback = null): ICollection |
64
|
|
|
{ |
65
|
|
|
$model = $this->instantiate($class); |
66
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
67
|
|
|
$params = [ |
68
|
|
|
'index' => $collection->getIndex(), |
69
|
|
|
'type' => $collection->getType(), |
70
|
|
|
'body' => $query->build() |
71
|
|
|
]; |
72
|
|
|
$response = new ElasticsearchCollectionResponse($this->client->search($params)); |
|
|
|
|
73
|
|
|
$models = new ElasticsearchCollection([], $response); |
74
|
|
|
foreach ($response->hits() as $hit) { |
75
|
|
|
$model = $this->instantiate($class); |
76
|
|
|
$this->fill($model, new ElasticsearchItemResponse($hit)); |
77
|
|
|
if ($callback != null) { |
78
|
|
|
$callback($model); |
79
|
|
|
} |
80
|
|
|
$models->put($model->getPrimaryKey(), $model); |
81
|
|
|
} |
82
|
|
|
return $models; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function insert(Storable $model): void |
86
|
|
|
{ |
87
|
|
|
$relationshipKey = RelationshipKey::parse($model->getPrimaryKey()); |
88
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
89
|
|
|
$params = [ |
90
|
|
|
'index' => $collection->getIndex(), |
91
|
|
|
'type' => $collection->getType(), |
92
|
|
|
'id' => $relationshipKey->getId(), |
93
|
|
|
'parent' => $relationshipKey->getParentId(), |
94
|
|
|
'body' => $model->toArray() |
95
|
|
|
]; |
96
|
|
|
$this->client->index($params); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function update(Storable $model): void |
100
|
|
|
{ |
101
|
|
|
$relationshipKey = RelationshipKey::parse($model->getPrimaryKey()); |
102
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
103
|
|
|
$params = [ |
104
|
|
|
'index' => $collection->getIndex(), |
105
|
|
|
'type' => $collection->getType(), |
106
|
|
|
'id' => $relationshipKey->getId(), |
107
|
|
|
'parent' => $relationshipKey->getParentId(), |
108
|
|
|
'body' => $model->toArray() |
109
|
|
|
]; |
110
|
|
|
$this->client->index($params); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function delete(Storable $model): void |
114
|
|
|
{ |
115
|
|
|
$relationshipKey = RelationshipKey::parse($model->getPrimaryKey()); |
116
|
|
|
$collection = new ElasticsearchCollectionParser($model->getCollection()); |
117
|
|
|
$params = [ |
118
|
|
|
'index' => $collection->getIndex(), |
119
|
|
|
'type' => $collection->getType(), |
120
|
|
|
'id' => $relationshipKey->getId(), |
121
|
|
|
'parent' => $relationshipKey->getParentId() |
122
|
|
|
]; |
123
|
|
|
$this->client->delete($params); |
124
|
|
|
} |
125
|
|
|
} |
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: