Skip to content

Commit

Permalink
Refactor for SOLID compliance - Breaking on getAllIndexes (method r…
Browse files Browse the repository at this point in the history
…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
ppshobi committed Jul 3, 2020
1 parent cfe0a45 commit 0089410
Show file tree
Hide file tree
Showing 21 changed files with 826 additions and 581 deletions.
138 changes: 46 additions & 92 deletions src/Client.php
Expand Up @@ -2,109 +2,63 @@

namespace MeiliSearch;

use MeiliSearch\Exceptions\HTTPRequestException;

class Client extends HTTPRequest
use MeiliSearch\Delegates\HandlesIndex;
use MeiliSearch\Delegates\HandlesSystem;
use MeiliSearch\Endpoints\Health;
use MeiliSearch\Endpoints\Indexes;
use MeiliSearch\Endpoints\Keys;
use MeiliSearch\Endpoints\Stats;
use MeiliSearch\Endpoints\SysInfo;
use MeiliSearch\Endpoints\Version;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;

class Client
{
// Indexes
use HandlesIndex;
use HandlesSystem;

public function getAllIndexes()
{
return $this->httpGet('/indexes');
}

public function showIndex($uid)
{
return $this->indexInstance($uid)->show();
}

public function deleteIndex($uid)
{
return $this->indexInstance($uid)->delete();
}

public function deleteAllIndexes()
{
foreach ($this->getAllIndexes() as $index) {
$this->deleteIndex($index['uid']);
}
}

public function getIndex($uid)
{
return $this->indexInstance($uid);
}

public function createIndex($index_uid, $options = [])
{
$body = array_merge(
$options,
['uid' => $index_uid]
);
$response = $this->httpPost('/indexes', $body);
$uid = $response['uid'];

return $this->indexInstance($uid);
}
private $http;

/**
* @throws HTTPRequestException
* @var Indexes
*/
public function getOrCreateIndex(string $uid, array $options = []): Index
{
$index = $this->getIndex($uid);

try {
$index = $this->createIndex($uid, $options);
} catch (HTTPRequestException $e) {
if (is_array($e->http_body) && 'index_already_exists' !== $e->http_body['errorCode']) {
throw $e;
}
}

return $index;
}

// Health

public function health()
{
return $this->httpGet('/health');
}
private $index;

// Stats

public function version()
{
return $this->httpGet('/version');
}

public function sysInfo()
{
return $this->httpGet('/sys-info');
}

public function prettySysInfo()
{
return $this->httpGet('/sys-info/pretty');
}
/**
* @var Health
*/
private $health;

public function stats()
{
return $this->httpGet('/stats');
}
/**
* @var Version
*/
private $version;

// Keys
/**
* @var SysInfo
*/
private $sysInfo;

public function getKeys()
{
return $this->httpGet('/keys');
}
/**
* @var Keys
*/
private $keys;

// Private methods
/**
* @var Stats
*/
private $stats;

private function indexInstance($uid)
public function __construct(string $url, string $apiKey = null, ClientInterface $httpClient = null, RequestFactoryInterface $requestFactory = null, StreamFactoryInterface $streamFactory = null)
{
return new Index($uid, $this->base_url, $this->api_key);
$this->http = new Http\Client($url, $apiKey, $httpClient, $requestFactory, $streamFactory);
$this->index = new Indexes($this->http);
$this->health = new Health($this->http);
$this->version = new Version($this->http);
$this->sysInfo = new SysInfo($this->http);
$this->stats = new Stats($this->http);
$this->keys = new Keys($this->http);
}
}
21 changes: 21 additions & 0 deletions src/Contracts/Endpoint.php
@@ -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);
}
}
16 changes: 16 additions & 0 deletions src/Contracts/Http.php
@@ -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 = []);
}
60 changes: 60 additions & 0 deletions src/Delegates/HandlesIndex.php
@@ -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;
}
}
36 changes: 36 additions & 0 deletions src/Delegates/HandlesSystem.php
@@ -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();
}
}
41 changes: 41 additions & 0 deletions src/Endpoints/Delegates/HandlesDocuments.php
@@ -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);
}
}

0 comments on commit 0089410

Please sign in to comment.