Skip to content

Commit

Permalink
Merge pull request #2 from samlev/feaure/docblock-cleanup
Browse files Browse the repository at this point in the history
Connection cleanups
  • Loading branch information
kyoungportable authored Sep 3, 2023
2 parents 5aae819 + 7f3dbb4 commit c5a1721
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 76 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"squizlabs/php_codesniffer": "^3.7",
"nunomaduro/larastan": "^2.6",
"orchestra/testbench": "^8.10"
},
"require": {
"php": "^8.1"
}
}
52 changes: 4 additions & 48 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 20 additions & 14 deletions src/Eloquent/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Portable\EloquentZoho\Eloquent\Query\Builder;
use Portable\EloquentZoho\Eloquent\Query\Grammar;
use Portable\EloquentZoho\Exceptions\ConfigurationException;
use Portable\EloquentZoho\ZohoClient;
use Illuminate\Http\Client\Response;

Expand All @@ -14,34 +15,39 @@ class Connection extends DatabaseConnection
/**
* @var ZohoClient
*/
protected ZohoClient $connection;
protected ZohoClient $client;

protected string $folderName;

public function __construct(protected array $zohoConfig)
{
$requiredKeys = ['api_url', 'api_email', 'auth_token', 'workspace_name', 'folder_name'];
$requiredKeys = ['api_url', 'api_email', 'workspace_name', 'folder_name'];
foreach ($requiredKeys as $key) {
if (! isset($zohoConfig[$key])) {
throw new \Exception("Missing required key '$key' in zoho config");
throw new ConfigurationException("Missing required key '$key' in zoho config");
}
}

$this->connection = new ZohoClient(
$this->client = new ZohoClient(
$zohoConfig['api_url'],
$zohoConfig['api_email'],
$zohoConfig['auth_token'],
$zohoConfig['workspace_name'],
$zohoConfig['auth_token'] ?? null,
);
$this->folderName = $zohoConfig['folder_name'];
$this->useDefaultPostProcessor();
$this->useDefaultQueryGrammar();
$this->useDefaultSchemaGrammar();
}

public function setAuthToken(?string $token): void
{
$this->client->setAuthToken($token);
}

public function generateAuthToken(string $username, string $password): ?string
{
return $this->connection->generateAuthToken($username, $password);
return $this->client->generateAuthToken($username, $password);
}

public function getFolderName(): string
Expand Down Expand Up @@ -80,7 +86,7 @@ public function getSchemaBuilder()
public function hasTable(string $table): bool
{
try {
$this->connection->exportTable($table, 'badcolumnname = 1');
$this->client->exportTable($table, 'badcolumnname = 1');

// If for some insane reason, the table exists and has a column called badcolumname
// then we'll get a successful response and we know the table exists
Expand All @@ -97,7 +103,7 @@ public function hasTable(string $table): bool

public function zohoSelect(string $fromTable, string $query): array
{
$data = $this->connection->exportTable($fromTable, $query);
$data = $this->client->exportTable($fromTable, $query);

$rows = [];
foreach ($data['response']['result']['rows'] as $row) {
Expand All @@ -109,7 +115,7 @@ public function zohoSelect(string $fromTable, string $query): array

public function zohoInsert(string $toTable, array $data): int
{
$result = $this->connection->addTableRow($toTable, $data);
$result = $this->client->addTableRow($toTable, $data);
$json = json_decode(str_replace("\\'", "'", $result->body()), true);
if (! $result->successful()) {
$msg = $json['response']['error']['message'];
Expand All @@ -121,7 +127,7 @@ public function zohoInsert(string $toTable, array $data): int

public function zohoUpdate(string $fromTable, array $data, string $where): int
{
$result = $this->connection->updateTableRow($fromTable, $data, $where);
$result = $this->client->updateTableRow($fromTable, $data, $where);
$json = json_decode(str_replace("\\'", "'", $result->body()), true);
if (! $result->successful()) {
$msg = $json['response']['error']['message'];
Expand All @@ -137,7 +143,7 @@ public function zohoUpsert(string $toTable, array $data, array|string $key): int
$key = [$key];
}

return $this->connection->importUpsert($toTable, $data, $key);
return $this->client->importUpsert($toTable, $data, $key);
}

public function zohoDelete(string $fromTable, string $where): int
Expand All @@ -148,7 +154,7 @@ public function zohoDelete(string $fromTable, string $where): int
// I have no idea why, but this is what works.
$where = str_replace('\\\\\\\\\\', '\\\\', $where);

$result = $this->connection->deleteTableRow($fromTable, $where);
$result = $this->client->deleteTableRow($fromTable, $where);
$json = json_decode(str_replace("\\'", "'", $result->body()), true);
if (! $result->successful()) {
$msg = $json['response']['error']['message'];
Expand All @@ -160,12 +166,12 @@ public function zohoDelete(string $fromTable, string $where): int

public function zohoCreateTable(array $tableDefinition): Response
{
return $this->connection->createTable($tableDefinition);
return $this->client->createTable($tableDefinition);
}

public function zohoDeleteTable(string $tableName): Response
{
return $this->connection->deleteTable($tableName);
return $this->client->deleteTable($tableName);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Eloquent/Facades/ZohoSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace Portable\EloquentZoho\Eloquent\Facades;

use Illuminate\Support\Facades\Facade;
use Portable\EloquentZoho\Eloquent\Schema\Builder;

/**
* @mixin Builder
*/
class ZohoSchema extends Facade
{
protected static function getFacadeAccessor()
protected static function getFacadeAccessor(): string
{
return 'zoho.builder';
}
Expand Down
7 changes: 7 additions & 0 deletions src/Exceptions/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Portable\EloquentZoho\Exceptions;

class ConfigurationException extends \RuntimeException
{
}
15 changes: 15 additions & 0 deletions src/Exceptions/NotConnectedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Portable\EloquentZoho\Exceptions;

class NotConnectedException extends \RuntimeException
{
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null)
{
parent::__construct(
$message ?: 'Cannot connect to Zoho Analytics with current configuration. Check URL and credentials.',
$code,
$previous,
);
}
}
26 changes: 13 additions & 13 deletions src/ZohoClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Portable\EloquentZoho\Events\ZohoCallCompleted;
use Portable\EloquentZoho\Exceptions\NotConnectedException;

class ZohoClient
{
Expand All @@ -15,9 +16,10 @@ class ZohoClient
public function __construct(
protected string $apiUrl,
protected string $apiEmail,
protected string $authToken,
protected string $workspaceName
protected string $workspaceName,
protected ?string $authToken = null,
) {
//
}

/**
Expand All @@ -32,6 +34,11 @@ public function connected(): bool
&& $this->authToken;
}

public function setAuthToken(?string $token): void
{
$this->authToken = $token;
}

/**
* Generates the authentication token to be used in subsequent requests to the API.
*
Expand All @@ -53,16 +60,11 @@ public function generateAuthToken(string $userEmail, string $userPassword): ?str
);

if ($response->successful()) {
$matches = [];
preg_match('#AUTHTOKEN=([a-f0-9]+)\b#', $response->body(), $matches);
if (isset($matches[1])) {
if (preg_match('#AUTHTOKEN=([a-f0-9]+)\b#', $response->body(), $matches)) {
return $matches[1];
} else {
return null;
}
} else {
return null;
}
return null;
}

/**
Expand All @@ -84,8 +86,7 @@ public function handleResponse(Response $response): ?Response
public function post(string $url, ?array $data = []): ?Response
{
if (! $this->connected()) {
throw new \Exception('Cannot connect to Zoho Analytics with current configuration.'
. ' Check URL and credentials.');
throw new NotConnectedException();
}

$data = array_merge($data, [
Expand Down Expand Up @@ -144,8 +145,7 @@ protected function importAction(
bool $isTransaction = false
): ?Response {
if (! $this->connected()) {
throw new \Exception('Cannot connect to Zoho Analytics with current configuration.'
. ' Check URL and credentials.');
throw new NotConnectedException();
}

$postData = [
Expand Down

0 comments on commit c5a1721

Please sign in to comment.