From 7f3dbb4c7ef17cb030a854809ea24bbcf464fa36 Mon Sep 17 00:00:00 2001 From: Samuel Levy Date: Fri, 1 Sep 2023 17:08:53 +1000 Subject: [PATCH] Connection cleanups --- composer.json | 3 ++ composer.lock | 52 ++--------------------- src/Eloquent/Connection.php | 34 +++++++++------ src/Eloquent/Facades/ZohoSchema.php | 6 ++- src/Exceptions/ConfigurationException.php | 7 +++ src/Exceptions/NotConnectedException.php | 15 +++++++ src/ZohoClient.php | 26 ++++++------ 7 files changed, 67 insertions(+), 76 deletions(-) create mode 100644 src/Exceptions/ConfigurationException.php create mode 100644 src/Exceptions/NotConnectedException.php diff --git a/composer.json b/composer.json index 742e914..b009caf 100644 --- a/composer.json +++ b/composer.json @@ -25,5 +25,8 @@ "squizlabs/php_codesniffer": "^3.7", "nunomaduro/larastan": "^2.6", "orchestra/testbench": "^8.10" + }, + "require": { + "php": "^8.1" } } diff --git a/composer.lock b/composer.lock index dadc7b9..b98cc60 100644 --- a/composer.lock +++ b/composer.lock @@ -4,55 +4,9 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "305c1ea3afc6360783868bf481d454ed", + "content-hash": "fe3218a0da9628fd7bc4026555173f71", "packages": [], "packages-dev": [ - { - "name": "archtechx/enums", - "version": "v0.3.2", - "source": { - "type": "git", - "url": "https://github.com/archtechx/enums.git", - "reference": "475f45e682b0771253707f9403b704759a08da5f" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/archtechx/enums/zipball/475f45e682b0771253707f9403b704759a08da5f", - "reference": "475f45e682b0771253707f9403b704759a08da5f", - "shasum": "" - }, - "require": { - "php": "^8.1" - }, - "require-dev": { - "nunomaduro/larastan": "^1.0|^2.4", - "orchestra/testbench": "^6.9|^7.0|^8.0", - "pestphp/pest": "^1.2|^2.0", - "pestphp/pest-plugin-laravel": "^1.0|^2.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "ArchTech\\Enums\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Samuel Ć tancl", - "email": "samuel@archte.ch" - } - ], - "description": "Helpers for making PHP enums more lovable.", - "support": { - "issues": "https://github.com/archtechx/enums/issues", - "source": "https://github.com/archtechx/enums/tree/v0.3.2" - }, - "time": "2023-02-15T13:05:41+00:00" - }, { "name": "brick/math", "version": "0.11.0", @@ -8631,7 +8585,9 @@ "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, - "platform": [], + "platform": { + "php": "^8.1" + }, "platform-dev": [], "plugin-api-version": "2.3.0" } diff --git a/src/Eloquent/Connection.php b/src/Eloquent/Connection.php index a367325..025b549 100644 --- a/src/Eloquent/Connection.php +++ b/src/Eloquent/Connection.php @@ -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; @@ -14,24 +15,24 @@ 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(); @@ -39,9 +40,14 @@ public function __construct(protected array $zohoConfig) $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 @@ -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 @@ -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) { @@ -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']; @@ -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']; @@ -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 @@ -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']; @@ -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); } /** diff --git a/src/Eloquent/Facades/ZohoSchema.php b/src/Eloquent/Facades/ZohoSchema.php index cea722c..21b011a 100644 --- a/src/Eloquent/Facades/ZohoSchema.php +++ b/src/Eloquent/Facades/ZohoSchema.php @@ -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'; } diff --git a/src/Exceptions/ConfigurationException.php b/src/Exceptions/ConfigurationException.php new file mode 100644 index 0000000..161b202 --- /dev/null +++ b/src/Exceptions/ConfigurationException.php @@ -0,0 +1,7 @@ +authToken; } + public function setAuthToken(?string $token): void + { + $this->authToken = $token; + } + /** * Generates the authentication token to be used in subsequent requests to the API. * @@ -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; } /** @@ -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, [ @@ -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 = [