Skip to content

Commit

Permalink
feat: exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
faustoq committed Jul 19, 2024
1 parent 240ad3d commit 0ea17bd
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
8 changes: 6 additions & 2 deletions src/Client/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OramaCloud\Client\QueryParams\SortBy;
use OramaCloud\Client\QueryParams\SortByOrder;
use OramaCloud\Client\QueryParams\Where;
use OramaCloud\Exceptions\QueryException;

class Query
{
Expand All @@ -17,12 +18,15 @@ class Query

public function __construct($term = '', $mode = 'fulltext')
{
$this->term = $term;
$this->mode = $mode;
$this->term($term)->mode($mode);
}

public function term(string $term)
{
if (!is_string($term)) {
throw new QueryException('Invalid search term.');
}

$this->term = $term;
return $this;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Client/QueryParams/SortBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OramaCloud\Client\QueryParams;

use OramaCloud\Exceptions\QueryException;

class SortBy
{
private $property;
Expand Down Expand Up @@ -30,7 +32,7 @@ public function toArray()
private function validate()
{
if (!in_array($this->order, $this->availableOrders)) {
throw new \InvalidArgumentException('Invalid $order parameter in SortBy');
throw new QueryException('Invalid $order parameter in SortBy');
}
}
}
10 changes: 8 additions & 2 deletions src/Client/QueryParams/Where.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace OramaCloud\Client\QueryParams;

use OramaCloud\Exceptions\QueryException;

class Where
{
private $property;
Expand Down Expand Up @@ -38,16 +40,20 @@ public function toArray()

private function validate()
{
if (empty($this->property)) {
throw new QueryException("Missing or invalid property: {$this->property}");
}

if (!in_array($this->operator, $this->availableOperators)) {
throw new \InvalidArgumentException("Invalid operator {$this->operator}");
throw new QueryException("Invalid where operator: {$this->operator}");
}

if (in_array($this->operator, [
WhereOperator::BETWEEN,
WhereOperator::IN,
WhereOperator::NIN
]) && !is_array($this->value)) {
throw new \InvalidArgumentException('Where $value parameter must be an array');
throw new QueryException('Where $value argument must be an array');
}
}
}
11 changes: 11 additions & 0 deletions src/Exceptions/IndexManagerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace OramaCloud\Exceptions;

class IndexManagerException extends \Exception
{
public function __construct($message)
{
parent::__construct($message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace OramaCloud\Client;
namespace OramaCloud\Exceptions;

class QueryException extends \Exception
{
Expand Down
27 changes: 17 additions & 10 deletions src/Manager/CloudManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OramaCloud\Manager;

use GuzzleHttp\Client as HttpClient;
use OramaCloud\Exceptions\IndexManagerException;
use OramaCloud\Manager\IndexManager;

class CloudManager
Expand All @@ -25,23 +26,29 @@ public function setIndexId(string $id): void
public function callIndexWebhook(string $endpoint, $payload = null)
{
if (!$this->indexId) {
throw new \Exception('Index ID is not set');
throw new IndexManagerException('Index ID is not set');
}

$config = [
'headers' => [
try {
$config['headers'] = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->apiKey
]
];
];

if (!is_null($payload)) {
$config['body'] = json_encode($payload);
}
if (!is_null($payload)) {
$config['body'] = json_encode($payload);
}

$response = $this->http->request('POST', $this->getEndpoint($endpoint), $config);

$response = $this->http->request('POST', $this->getEndpoint($endpoint), $config);
if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
throw new IndexManagerException('Error calling webhook: ' . $response->getBody()->getContents());
}

return json_decode($response->getBody()->getContents());
return json_decode($response->getBody()->getContents());
} catch (\Exception $e) {
throw new IndexManagerException($e->getMessage());
}
}

private function getEndpoint(string $endpoint): string
Expand Down
2 changes: 1 addition & 1 deletion src/Manager/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private function init($manager)
{
if ($manager instanceof CloudManager) {
$this->manager = $manager;
} else if ($manager instanceof string) {
} else if (gettype($manager) === 'string') {
$this->manager = new CloudManager($manager);
} else {
throw new \Exception('Invalid manager parameter. It should be an instance of CloudManager or an API key string.');
Expand Down

0 comments on commit 0ea17bd

Please sign in to comment.