Skip to content

Commit 220753c

Browse files
authored
Merge pull request #64 from Humpty-Dumpty/master
Minor changes to improve code navigation and documentation
2 parents 741fb62 + bae8eff commit 220753c

File tree

3 files changed

+56
-58
lines changed

3 files changed

+56
-58
lines changed

src/Connection.php

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Basemkhirat\Elasticsearch;
44

5+
use Elasticsearch\Client;
56
use Elasticsearch\ClientBuilder;
7+
use Symfony\Component\HttpKernel\Exception\HttpException as HttpException;
68

79
/**
810
* Class Connection
@@ -13,7 +15,7 @@ class Connection
1315

1416
/**
1517
* Laravel app instance
16-
* @var \Illuminate\Foundation\Application|mixed
18+
* @var \Illuminate\contracts\Foundation\Application|mixed
1719
*/
1820
protected $app;
1921

@@ -25,13 +27,13 @@ class Connection
2527

2628
/**
2729
* The current connection
28-
* @var
30+
* @var Client
2931
*/
3032
protected $connection;
3133

3234
/**
3335
* all available connections
34-
* @var array
36+
* @var Client[]
3537
*/
3638
protected $connections = [];
3739

@@ -77,11 +79,11 @@ public static function create($config)
7779
/**
7880
* Create a connection for laravel or lumen frameworks
7981
* @param $name
82+
* @throws HttpException
8083
* @return Query
8184
*/
8285
function connection($name)
8386
{
84-
8587
// Check if connection is already loaded.
8688

8789
if ($this->isLoaded($name)) {
@@ -121,6 +123,16 @@ function connection($name)
121123
}
122124

123125

126+
/**
127+
* @param string $name
128+
* @throws HttpException
129+
* @return Query
130+
*/
131+
function connectionByName($name = 'default') {
132+
return $this->connection($this->config[$name]);
133+
}
134+
135+
124136
/**
125137
* route the request to the query class
126138
* @param $connection
@@ -148,35 +160,22 @@ function newQuery($connection)
148160
function isLoaded($name)
149161
{
150162

151-
if (array_key_exists($name, $this->connections)) {
152-
return true;
153-
}
154-
155-
return false;
163+
return array_key_exists($name, $this->connections);
156164
}
157165

158166

159167
/**
160168
* Set the default connection
161169
* @param $name
162170
* @param $arguments
171+
* @throws HttpException
163172
* @return mixed
164173
*/
165174
function __call($name, $arguments)
166175
{
167-
if (method_exists($this, $name)) {
168-
169-
return call_user_func_array([$this, $name], $arguments);
170-
171-
} else {
172176

173-
// if no connection, use default.
177+
$query = $this->connectionByName('default');
174178

175-
$query = $this->connection($this->config["default"]);
176-
177-
return call_user_func_array([$query, $name], $arguments);
178-
179-
}
179+
return call_user_func_array([$query, $name], $arguments);
180180
}
181-
182181
}

src/Model.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Basemkhirat\Elasticsearch;
44

55
use ArrayAccess;
6-
use Illuminate\Support\Collection;
76

87
/**
98
* Elasticsearch data model
@@ -100,6 +99,7 @@ public function getConnection()
10099

101100
/**
102101
* Set current connection
102+
* @param $connection
103103
* @return void
104104
*/
105105
public function setConnection($connection)
@@ -119,7 +119,7 @@ public function getIndex()
119119

120120
/**
121121
* Set index name
122-
*
122+
* @param $index
123123
* @return void
124124
*/
125125
public function setIndex($index)
@@ -138,6 +138,7 @@ public function getType()
138138

139139
/**
140140
* Set type name
141+
* @param $type
141142
* @return void
142143
*/
143144
public function setType($type)
@@ -271,11 +272,11 @@ public function __set($name, $value)
271272

272273
/**
273274
* Create a new model query
274-
* @return mixed
275+
* @return Query
275276
*/
276277
protected function newQuery()
277278
{
278-
$query = app("es")->setModel($this);
279+
$query = app("es")->connectionByName()->setModel($this);
279280

280281
$query->connection($this->getConnection());
281282

@@ -292,6 +293,7 @@ protected function newQuery()
292293

293294
/**
294295
* Get all model records
296+
* @throws \Exception
295297
* @return mixed
296298
*/
297299
public static function all()
@@ -306,6 +308,7 @@ public static function all()
306308
/**
307309
* Get model by key
308310
* @param $key
311+
* @throws \Exception
309312
* @return mixed
310313
*/
311314
public static function find($key)

src/Query.php

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
use Basemkhirat\Elasticsearch\Classes\Bulk;
66
use Basemkhirat\Elasticsearch\Classes\Search;
7-
use Basemkhirat\Elasticsearch\Collection;
7+
use Elasticsearch\Client;
8+
use Elasticsearch\Common\Exceptions\BadMethodCallException;
89

910

1011
/**
@@ -16,13 +17,13 @@ class Query
1617

1718
/**
1819
* Native elasticsearch connection instance
19-
* @var Connection
20+
* @var Client
2021
*/
2122
public $connection;
2223

2324
/**
2425
* Ignored HTTP errors
25-
* @var array
26+
* @var string[]
2627
*/
2728
public $ignores = [];
2829

@@ -157,14 +158,14 @@ class Query
157158

158159
/**
159160
* Elastic model instance
160-
* @var \Basemkhirat\Elasticsearch\Model
161+
* @var Model
161162
*/
162163
public $model;
163164

164165

165166
/**
166167
* Query constructor.
167-
* @param $connection
168+
* @param Client|NULL $connection
168169
*/
169170
function __construct($connection = NULL)
170171
{
@@ -196,7 +197,7 @@ public function getIndex()
196197
/**
197198
* Set the type name
198199
* @param $type
199-
* @return $this
200+
* @return Query $this
200201
*/
201202
public function type($type)
202203
{
@@ -256,7 +257,7 @@ public function searchType($type)
256257

257258
/**
258259
* get the query search type
259-
* @return $this
260+
* @return int
260261
*/
261262
public function getSearchType()
262263
{
@@ -265,7 +266,7 @@ public function getSearchType()
265266

266267
/**
267268
* Get the query scroll
268-
* @return $this
269+
* @return string
269270
*/
270271
public function getScroll()
271272
{
@@ -362,11 +363,7 @@ public function orderBy($field, $direction = "asc")
362363
protected function isOperator($string)
363364
{
364365

365-
if (in_array($string, $this->operators)) {
366-
return true;
367-
}
368-
369-
return false;
366+
return in_array($string, $this->operators);
370367
}
371368

372369
/**
@@ -656,6 +653,7 @@ public function distance($name, $value, $distance)
656653
/**
657654
* Search the entire document fields
658655
* @param null $q
656+
* @param NULL $settings
659657
* @return $this
660658
*/
661659
public function search($q = NULL, $settings = NULL)
@@ -788,7 +786,8 @@ public function clear($scroll_id = NULL)
788786

789787
/**
790788
* Get the collection of results
791-
* @return array|Collection
789+
* @throws \Exception
790+
* @return Collection
792791
*/
793792
public function get()
794793
{
@@ -801,6 +800,7 @@ public function get()
801800
/**
802801
* Get the first object of results
803802
* @param string $scroll_id
803+
* @throws \Exception
804804
* @return object
805805
*/
806806
public function first($scroll_id = NULL)
@@ -816,6 +816,7 @@ public function first($scroll_id = NULL)
816816
/**
817817
* Get query result
818818
* @param $scroll_id
819+
* @throws \Exception
819820
* @return mixed
820821
*/
821822
protected function getResult($scroll_id)
@@ -839,7 +840,8 @@ protected function getResult($scroll_id)
839840
/**
840841
* Get non cached results
841842
* @param null $scroll_id
842-
* @return mixed
843+
* @throws \Exception
844+
* @return array
843845
*/
844846
public function response($scroll_id = NULL)
845847
{
@@ -902,7 +904,7 @@ function setModel($model)
902904
/**
903905
* Retrieve all records
904906
* @param array $result
905-
* @return array|Collection
907+
* @return Collection
906908
*/
907909
protected function getAll($result = [])
908910
{
@@ -981,6 +983,7 @@ protected function getFirst($result = [])
981983
* @param int $per_page
982984
* @param $page_name
983985
* @param null $page
986+
* @throws \Exception
984987
* @return Pagination
985988
*/
986989
public function paginate($per_page = 10, $page_name = "page", $page = null)
@@ -1001,7 +1004,7 @@ public function paginate($per_page = 10, $page_name = "page", $page = null)
10011004
* Insert a document
10021005
* @param $data
10031006
* @param null $_id
1004-
* @return object
1007+
* @return array
10051008
*/
10061009
public function insert($data, $_id = NULL)
10071010
{
@@ -1027,12 +1030,12 @@ public function insert($data, $_id = NULL)
10271030
$parameters["id"] = $this->_id;
10281031
}
10291032

1030-
return (object)$this->connection->index($parameters);
1033+
return $this->connection->index($parameters);
10311034
}
10321035

10331036
/**
10341037
* Insert a bulk of documents
1035-
* @param $data multidimensional array of [id => data] pairs
1038+
* @param $data callable|array : multidimensional array of [id => data] pairs
10361039
* @return object
10371040
*/
10381041
public function bulk($data)
@@ -1309,7 +1312,6 @@ public function CachePrefix($prefix)
13091312

13101313
/**
13111314
* Get a unique cache key for the complete query.
1312-
*
13131315
* @return string
13141316
*/
13151317
public function getCacheKey()
@@ -1345,7 +1347,7 @@ public function remember($minutes, $key = null)
13451347
/**
13461348
* Indicate that the query results should be cached forever.
13471349
* @param string $key
1348-
* @return \Illuminate\Database\Query\Builder|static
1350+
* @return $this
13491351
*/
13501352
public function rememberForever($key = null)
13511353
{
@@ -1360,20 +1362,14 @@ public function rememberForever($key = null)
13601362
function __call($method, $parameters)
13611363
{
13621364

1363-
if (method_exists($this, $method)) {
1364-
return $this->$method(...$parameters);
1365-
} else {
1366-
1367-
// Check for model scopes
1368-
1369-
$method = "scope" . ucfirst($method);
1365+
$method = "scope" . ucfirst($method);
13701366

1371-
if (method_exists($this->model, $method)) {
1372-
$parameters = array_merge([$this], $parameters);
1373-
$this->model->$method(...$parameters);
1374-
return $this;
1375-
}
1367+
if (method_exists($this->model, $method)) {
1368+
$parameters = array_merge([$this], $parameters);
1369+
$this->model->$method(...$parameters);
1370+
return $this;
13761371
}
13771372

1373+
throw new BadMethodCallException("Method $method does not exist.");
13781374
}
13791375
}

0 commit comments

Comments
 (0)