Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor for SOLID compliance - Breaking on
getAllIndexes
(method r…
…eturn) and `Index` class (renamed into `Indexes`) (#43) * wip * HTTP contract and client * let client be an independent class * let class Index be an endpoint instance * show index test passes * passing all testcases in IndexTest * dedicated endpoint classes * make client tests pass with the new classes * change the exception type since the php type system will prevent us from passing null values to a string * passes all documents test with added typehints * Keys and Permissions endpoint and test passes * change return type to mixed since few methods can return booleans as well compared to arrays * move endpoints to appropriate directories * delegate index related methods to handlesIndex trait and system related to handlesSystem trait * remove dead class * implement patch request * generate php doc for HTTP client class * extract common methods to abstract class * cleanup * extract related methods to appropriate traits * lint * change SearchRules to SearchSettings to be synonymous with meilisearch-cor * add return type * rename handlesSearchSettings -> handlesSettings to adhere to meilisearch naming conventions. * rename trait to capital camelcasing * added typehints and const visibility * more typehint * more typehints * more typehints and variable renameings * import proper traits * lint * lint * move method inside the trait * use camel case * fix class names and rename variables inside HTTPRequestException.php in camelCase * WIP * WIP * Move HTTPRequestException test inside ClientTest since with the new implementation, it directly throws NetworkException when its unable to connect to meilisearch, so refactored the test a little as well * lint
- Loading branch information
Showing
21 changed files
with
826 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace MeiliSearch\Contracts; | ||
|
||
abstract class Endpoint | ||
{ | ||
/** | ||
* @var Http | ||
*/ | ||
protected $http; | ||
|
||
public function __construct(Http $http) | ||
{ | ||
$this->http = $http; | ||
} | ||
|
||
public function show(): ?array | ||
{ | ||
return $this->http->get(static::PATH); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace MeiliSearch\Contracts; | ||
|
||
interface Http | ||
{ | ||
public function get($path, array $query = []); | ||
|
||
public function post(string $path, $body = null, array $query = []); | ||
|
||
public function put(string $path, $body = null, array $query = []); | ||
|
||
public function patch(string $path, $body = null, array $query = []); | ||
|
||
public function delete($path, array $query = []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace MeiliSearch\Delegates; | ||
|
||
use MeiliSearch\Endpoints\Indexes; | ||
use MeiliSearch\Exceptions\HTTPRequestException; | ||
|
||
trait HandlesIndex | ||
{ | ||
public function getAllIndexes(): array | ||
{ | ||
return $this->index->all(); | ||
} | ||
|
||
public function showIndex($uid): array | ||
{ | ||
return (new Indexes($this->http, $uid))->show(); | ||
} | ||
|
||
public function deleteIndex($uid): void | ||
{ | ||
(new Indexes($this->http, $uid))->delete(); | ||
} | ||
|
||
public function deleteAllIndexes(): void | ||
{ | ||
$indexes = $this->getAllIndexes(); | ||
foreach ($indexes as $index) { | ||
$index->delete(); | ||
} | ||
} | ||
|
||
public function getIndex($uid): Indexes | ||
{ | ||
return new Indexes($this->http, $uid); | ||
} | ||
|
||
public function createIndex($uid, $options = []): Indexes | ||
{ | ||
return $this->index->create($uid, $options); | ||
} | ||
|
||
/** | ||
* @throws HTTPRequestException | ||
*/ | ||
public function getOrCreateIndex(string $uid, array $options = []): Indexes | ||
{ | ||
$index = $this->getIndex($uid); | ||
|
||
try { | ||
$index = $this->createIndex($uid, $options); | ||
} catch (HTTPRequestException $e) { | ||
if (is_array($e->httpBody) && 'index_already_exists' !== $e->httpBody['errorCode']) { | ||
throw $e; | ||
} | ||
} | ||
|
||
return $index; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace MeiliSearch\Delegates; | ||
|
||
trait HandlesSystem | ||
{ | ||
public function health(): ?array | ||
{ | ||
return $this->health->show(); | ||
} | ||
|
||
public function version(): array | ||
{ | ||
return $this->version->show(); | ||
} | ||
|
||
public function sysInfo(): array | ||
{ | ||
return $this->sysInfo->show(); | ||
} | ||
|
||
public function prettySysInfo(): array | ||
{ | ||
return $this->sysInfo->pretty(); | ||
} | ||
|
||
public function stats(): array | ||
{ | ||
return $this->stats->show(); | ||
} | ||
|
||
public function getKeys(): array | ||
{ | ||
return $this->keys->show(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace MeiliSearch\Endpoints\Delegates; | ||
|
||
trait HandlesDocuments | ||
{ | ||
public function getDocument(string $document_id) | ||
{ | ||
return $this->http->get(self::PATH.'/'.$this->uid.'/documents/'.$document_id); | ||
} | ||
|
||
public function getDocuments(array $query = []) | ||
{ | ||
return $this->http->get(self::PATH.'/'.$this->uid.'/documents', $query); | ||
} | ||
|
||
public function addDocuments(array $documents, string $primaryKey = null) | ||
{ | ||
return $this->http->post(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primaryKey]); | ||
} | ||
|
||
public function updateDocuments(array $documents, string $primary_key = null) | ||
{ | ||
return $this->http->put(self::PATH.'/'.$this->uid.'/documents', $documents, ['primaryKey' => $primary_key]); | ||
} | ||
|
||
public function deleteAllDocuments(): array | ||
{ | ||
return $this->http->delete(self::PATH.'/'.$this->uid.'/documents'); | ||
} | ||
|
||
public function deleteDocument(string $document_id): array | ||
{ | ||
return $this->http->delete(self::PATH.'/'.$this->uid.'/documents/'.$document_id); | ||
} | ||
|
||
public function deleteDocuments(array $documents): array | ||
{ | ||
return $this->http->post(self::PATH.'/'.$this->uid.'/documents/delete-batch', $documents); | ||
} | ||
} |
Oops, something went wrong.