diff --git a/README.md b/README.md index 581ed79..59ad535 100644 --- a/README.md +++ b/README.md @@ -9,20 +9,16 @@ This package provides a convenient wrapper to the [Cuttly API](https://cutt.ly/api-documentation/regular-api) for Laravel applications. ## Requirements - -- PHP 8.0+ -- Laravel 9.x +- PHP 8.1 +- Laravel 9.x | 10.x ## Installation - To install this package tou can use composer: - ```bash composer require slvler/cuttly ``` ## Usage - #### Find player ```php $data['short'] = 'google.com'; @@ -88,17 +84,14 @@ URL Stats: ``` ## Testing - ```bash composer test ``` ## Credits - - [slvler](https://github.com/slvler) ## License - The MIT License (MIT). Please see [License File](https://github.com/slvler/balldontlie-service/blob/main/LICENSE.md) for more information. ## Contributing diff --git a/composer.json b/composer.json index d544c7b..a696596 100644 --- a/composer.json +++ b/composer.json @@ -16,14 +16,15 @@ } ], "require": { - "php": "^8.0.2", + "php": "^8.1", "guzzlehttp/guzzle": "^7.2", - "illuminate/support": "^9.0" + "illuminate/support": "^9.0|^10.0" }, "require-dev": { "friendsofphp/php-cs-fixer": "^3.6", "orchestra/testbench": "^7.0", - "phpunit/phpunit": "^9.5.8" + "phpunit/phpunit": "^9.5.8", + "laravel/pint": "^1.18" }, "autoload": { "psr-4": { @@ -37,6 +38,7 @@ }, "scripts": { "test": "vendor/bin/phpunit tests", + "pint": "vendor/bin/pint", "post-create-project-cmd": [ "@php artisan key:generate --ansi" ] diff --git a/config/cuttly.php b/config/cuttly.php index 58385ea..ec1e5fe 100644 --- a/config/cuttly.php +++ b/config/cuttly.php @@ -2,5 +2,5 @@ return [ 'base_uri' => 'https://cutt.ly/api/api.php', - 'api_key' => 'xxxxxxxxxxxxxxxxx' + 'api_key' => '', ]; diff --git a/src/Cuttly.php b/src/Cuttly.php index 0cbc9ee..3c325bd 100644 --- a/src/Cuttly.php +++ b/src/Cuttly.php @@ -1,9 +1,12 @@ $app['config']->get('cuttly.base_uri') - ] - ); + $apiKey = $app['config']->get('cuttly.api_key'); + + if (empty($apiKey) || ! isset($apiKey)) { + throw MissingApiKey::create(); + } + + $baseURL = $app['config']->get('cuttly.api_key'); + + if (empty($baseURL) || ! isset($baseURL)) { + throw new InvalidArgumentException('Invalid Cuttly API base URL.'); + } - $this->key = $app['config']->get('cuttly.api_key'); + parent::__construct($baseURL); + $this->key = $apiKey; } public function short(array $data): string @@ -28,8 +38,9 @@ public function short(array $data): string $this->data['short'] = urlencode($data['short']); $sendData = http_build_query($this->data); - $response = $this->getHttpClient()->request('GET','?'.$sendData); - $value = new HttpResponse($response); + $response = $this->getHttpClient()->request('GET', '?'.$sendData); + $value = new HttpResponse($response); + return $value->getBody(); } @@ -40,12 +51,12 @@ public function edit(array $data): string $this->data['edit'] = $data['edit']; $sendData = http_build_query($this->data); - $response = $this->getHttpClient()->request('GET','?'.$sendData); - $value = new HttpResponse($response); + $response = $this->getHttpClient()->request('GET', '?'.$sendData); + $value = new HttpResponse($response); + return $value->getBody(); } - public function stats(array $data): string { $this->data = $data; @@ -53,9 +64,9 @@ public function stats(array $data): string $this->data['stats'] = $data['stats']; $sendData = http_build_query($this->data); - $response = $this->getHttpClient()->request('GET','?'.$sendData); - $value = new HttpResponse($response); + $response = $this->getHttpClient()->request('GET', '?'.$sendData); + $value = new HttpResponse($response); + return $value->getBody(); } - } diff --git a/src/CuttlyApiWrapper.php b/src/CuttlyApiWrapper.php index f500be6..398d619 100644 --- a/src/CuttlyApiWrapper.php +++ b/src/CuttlyApiWrapper.php @@ -1,5 +1,7 @@ httpClient = new Client( - ['base_uri' => $parameters['base_uri']] + $this->httpClient = new Client([ + 'base_uri' => $baseUrl, + ] ); } diff --git a/src/CuttlyServiceProvider.php b/src/CuttlyServiceProvider.php index 176620f..b490d26 100644 --- a/src/CuttlyServiceProvider.php +++ b/src/CuttlyServiceProvider.php @@ -1,12 +1,11 @@ mergeConfigFrom(__DIR__.'/../config/cuttly.php', 'cuttly'); diff --git a/src/Exceptions/MissingApiKey.php b/src/Exceptions/MissingApiKey.php new file mode 100644 index 0000000..abe2c09 --- /dev/null +++ b/src/Exceptions/MissingApiKey.php @@ -0,0 +1,18 @@ +response->getBody(); + return (string) $this->response->getBody(); } public function toObject(): object { - $body = (string)$this->response->getBody(); + $body = (string) $this->response->getBody(); - return json_decode($body) ?? (object)[]; + return json_decode($body) ?? (object) []; } } diff --git a/tests/Feature/CuttlyTest.php b/tests/Feature/CuttlyTest.php index 419b126..7dd50c2 100644 --- a/tests/Feature/CuttlyTest.php +++ b/tests/Feature/CuttlyTest.php @@ -1,6 +1,5 @@ 'https://cutt.ly/api/api.php', - 'cuttly.api_key' => 'b4fa4f9976b41cbc0c9880c444fce27a28984', - ]); + 'cuttly.base_uri' => 'https://cutt.ly/api/api.php', + 'cuttly.api_key' => '', + ]); } /** @@ -25,27 +24,29 @@ public function setUp(): void public function test_short() { $data = [ - 'short' => 'google.com' + 'short' => 'google.com', ]; $this->assertIsString(Cuttly::short($data)); } + /** * @test */ public function test_edit() { $data = [ - 'edit' => 'cutt.ly/LwdCoBmo' + 'edit' => 'cutt.ly/LwdCoBmo', ]; $this->assertIsString(Cuttly::edit($data)); } + /** * @test */ public function test_stast() { $data = [ - 'stats' => 'cutt.ly/ewdVijlY' + 'stats' => 'cutt.ly/ewdVijlY', ]; $this->assertIsString(Cuttly::stats($data)); } diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php index e25e5fd..4b8ca2d 100644 --- a/tests/Unit/TestCase.php +++ b/tests/Unit/TestCase.php @@ -18,7 +18,5 @@ protected function getPackageProviders($app) ]; } - protected function getEnvironmentSetUp($app) - { - } + protected function getEnvironmentSetUp($app) {} }