Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
slvler committed Nov 18, 2024
1 parent 4afecd6 commit 11d638a
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 49 deletions.
11 changes: 2 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -37,6 +38,7 @@
},
"scripts": {
"test": "vendor/bin/phpunit tests",
"pint": "vendor/bin/pint",
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
Expand Down
2 changes: 1 addition & 1 deletion config/cuttly.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

return [
'base_uri' => 'https://cutt.ly/api/api.php',
'api_key' => 'xxxxxxxxxxxxxxxxx'
'api_key' => '',
];
41 changes: 26 additions & 15 deletions src/Cuttly.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
<?php

declare(strict_types=1);

namespace Slvler\Cuttly;

use Illuminate\Contracts\Container\Container;

use InvalidArgumentException;
use Slvler\Cuttly\Exceptions\MissingApiKey;

class Cuttly extends CuttlyApiWrapper
{
public array $data;

public function __construct(Container $app)
{
parent::__construct(
[
'base_uri' => $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
Expand All @@ -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();
}

Expand All @@ -40,22 +51,22 @@ 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;
$this->data['key'] = $this->key;
$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();
}

}
9 changes: 6 additions & 3 deletions src/CuttlyApiWrapper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Slvler\Cuttly;

use GuzzleHttp\Client;
Expand All @@ -8,10 +10,11 @@ class CuttlyApiWrapper
{
private Client $httpClient;

public function __construct(array $parameters)
public function __construct($baseUrl)
{
$this->httpClient = new Client(
['base_uri' => $parameters['base_uri']]
$this->httpClient = new Client([
'base_uri' => $baseUrl,
]
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/CuttlyServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<?php

declare(strict_types=1);

namespace Slvler\Cuttly;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Container\Container;


use Illuminate\Support\ServiceProvider;

class CuttlyServiceProvider extends ServiceProvider
{
Expand All @@ -18,6 +17,7 @@ public function boot()
], 'config');
}
}

public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/cuttly.php', 'cuttly');
Expand Down
18 changes: 18 additions & 0 deletions src/Exceptions/MissingApiKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Slvler\Cuttly\Exceptions;

use InvalidArgumentException;

/**
* @internal
*/
class MissingApiKey extends InvalidArgumentException
{
public static function create(): self
{
return new self(
'The Cuttly API Key is missing. Please publish the [cuttly.php] configuration file and set the [api_key].'
);
}
}
1 change: 0 additions & 1 deletion src/Facades/Cuttly.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Slvler\Cuttly\Facades;

use Illuminate\Support\Facades\Facade;
Expand Down
8 changes: 5 additions & 3 deletions src/HttpResponse.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Slvler\Cuttly;

class HttpResponse
Expand All @@ -13,13 +15,13 @@ public function __construct($response)

public function getBody(): string
{
return (string)$this->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) [];
}
}
15 changes: 8 additions & 7 deletions tests/Feature/CuttlyTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


namespace Slvler\Cuttly\Tests\Feature;

use Illuminate\Support\Facades\Config;
Expand All @@ -14,9 +13,9 @@ public function setUp(): void
parent::setUp();

Config::set([
'cuttly.base_uri' => 'https://cutt.ly/api/api.php',
'cuttly.api_key' => 'b4fa4f9976b41cbc0c9880c444fce27a28984',
]);
'cuttly.base_uri' => 'https://cutt.ly/api/api.php',
'cuttly.api_key' => '',
]);
}

/**
Expand All @@ -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));
}
Expand Down
4 changes: 1 addition & 3 deletions tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ protected function getPackageProviders($app)
];
}

protected function getEnvironmentSetUp($app)
{
}
protected function getEnvironmentSetUp($app) {}
}

0 comments on commit 11d638a

Please sign in to comment.