Skip to content

Commit 11d638a

Browse files
committed
v2.0
1 parent 4afecd6 commit 11d638a

File tree

11 files changed

+76
-49
lines changed

11 files changed

+76
-49
lines changed

README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@
99
This package provides a convenient wrapper to the [Cuttly API](https://cutt.ly/api-documentation/regular-api) for Laravel applications.
1010

1111
## Requirements
12-
13-
- PHP 8.0+
14-
- Laravel 9.x
12+
- PHP 8.1
13+
- Laravel 9.x | 10.x
1514

1615
## Installation
17-
1816
To install this package tou can use composer:
19-
2017
```bash
2118
composer require slvler/cuttly
2219
```
2320

2421
## Usage
25-
2622
#### Find player
2723
```php
2824
$data['short'] = 'google.com';
@@ -88,17 +84,14 @@ URL Stats:
8884
```
8985

9086
## Testing
91-
9287
```bash
9388
composer test
9489
```
9590

9691
## Credits
97-
9892
- [slvler](https://github.com/slvler)
9993

10094
## License
101-
10295
The MIT License (MIT). Please see [License File](https://github.com/slvler/balldontlie-service/blob/main/LICENSE.md) for more information.
10396

10497
## Contributing

composer.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
}
1717
],
1818
"require": {
19-
"php": "^8.0.2",
19+
"php": "^8.1",
2020
"guzzlehttp/guzzle": "^7.2",
21-
"illuminate/support": "^9.0"
21+
"illuminate/support": "^9.0|^10.0"
2222
},
2323
"require-dev": {
2424
"friendsofphp/php-cs-fixer": "^3.6",
2525
"orchestra/testbench": "^7.0",
26-
"phpunit/phpunit": "^9.5.8"
26+
"phpunit/phpunit": "^9.5.8",
27+
"laravel/pint": "^1.18"
2728
},
2829
"autoload": {
2930
"psr-4": {
@@ -37,6 +38,7 @@
3738
},
3839
"scripts": {
3940
"test": "vendor/bin/phpunit tests",
41+
"pint": "vendor/bin/pint",
4042
"post-create-project-cmd": [
4143
"@php artisan key:generate --ansi"
4244
]

config/cuttly.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22

33
return [
44
'base_uri' => 'https://cutt.ly/api/api.php',
5-
'api_key' => 'xxxxxxxxxxxxxxxxx'
5+
'api_key' => '',
66
];

src/Cuttly.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Slvler\Cuttly;
46

57
use Illuminate\Contracts\Container\Container;
6-
8+
use InvalidArgumentException;
9+
use Slvler\Cuttly\Exceptions\MissingApiKey;
710

811
class Cuttly extends CuttlyApiWrapper
912
{
1013
public array $data;
1114

1215
public function __construct(Container $app)
1316
{
14-
parent::__construct(
15-
[
16-
'base_uri' => $app['config']->get('cuttly.base_uri')
17-
]
18-
);
17+
$apiKey = $app['config']->get('cuttly.api_key');
18+
19+
if (empty($apiKey) || ! isset($apiKey)) {
20+
throw MissingApiKey::create();
21+
}
22+
23+
$baseURL = $app['config']->get('cuttly.api_key');
24+
25+
if (empty($baseURL) || ! isset($baseURL)) {
26+
throw new InvalidArgumentException('Invalid Cuttly API base URL.');
27+
}
1928

20-
$this->key = $app['config']->get('cuttly.api_key');
29+
parent::__construct($baseURL);
2130

31+
$this->key = $apiKey;
2232
}
2333

2434
public function short(array $data): string
@@ -28,8 +38,9 @@ public function short(array $data): string
2838
$this->data['short'] = urlencode($data['short']);
2939
$sendData = http_build_query($this->data);
3040

31-
$response = $this->getHttpClient()->request('GET','?'.$sendData);
32-
$value = new HttpResponse($response);
41+
$response = $this->getHttpClient()->request('GET', '?'.$sendData);
42+
$value = new HttpResponse($response);
43+
3344
return $value->getBody();
3445
}
3546

@@ -40,22 +51,22 @@ public function edit(array $data): string
4051
$this->data['edit'] = $data['edit'];
4152
$sendData = http_build_query($this->data);
4253

43-
$response = $this->getHttpClient()->request('GET','?'.$sendData);
44-
$value = new HttpResponse($response);
54+
$response = $this->getHttpClient()->request('GET', '?'.$sendData);
55+
$value = new HttpResponse($response);
56+
4557
return $value->getBody();
4658
}
4759

48-
4960
public function stats(array $data): string
5061
{
5162
$this->data = $data;
5263
$this->data['key'] = $this->key;
5364
$this->data['stats'] = $data['stats'];
5465
$sendData = http_build_query($this->data);
5566

56-
$response = $this->getHttpClient()->request('GET','?'.$sendData);
57-
$value = new HttpResponse($response);
67+
$response = $this->getHttpClient()->request('GET', '?'.$sendData);
68+
$value = new HttpResponse($response);
69+
5870
return $value->getBody();
5971
}
60-
6172
}

src/CuttlyApiWrapper.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Slvler\Cuttly;
46

57
use GuzzleHttp\Client;
@@ -8,10 +10,11 @@ class CuttlyApiWrapper
810
{
911
private Client $httpClient;
1012

11-
public function __construct(array $parameters)
13+
public function __construct($baseUrl)
1214
{
13-
$this->httpClient = new Client(
14-
['base_uri' => $parameters['base_uri']]
15+
$this->httpClient = new Client([
16+
'base_uri' => $baseUrl,
17+
]
1518
);
1619
}
1720

src/CuttlyServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Slvler\Cuttly;
46

5-
use Illuminate\Support\Facades\Http;
6-
use Illuminate\Support\ServiceProvider;
77
use Illuminate\Contracts\Container\Container;
8-
9-
8+
use Illuminate\Support\ServiceProvider;
109

1110
class CuttlyServiceProvider extends ServiceProvider
1211
{
@@ -18,6 +17,7 @@ public function boot()
1817
], 'config');
1918
}
2019
}
20+
2121
public function register()
2222
{
2323
$this->mergeConfigFrom(__DIR__.'/../config/cuttly.php', 'cuttly');

src/Exceptions/MissingApiKey.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Slvler\Cuttly\Exceptions;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* @internal
9+
*/
10+
class MissingApiKey extends InvalidArgumentException
11+
{
12+
public static function create(): self
13+
{
14+
return new self(
15+
'The Cuttly API Key is missing. Please publish the [cuttly.php] configuration file and set the [api_key].'
16+
);
17+
}
18+
}

src/Facades/Cuttly.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace Slvler\Cuttly\Facades;
54

65
use Illuminate\Support\Facades\Facade;

src/HttpResponse.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Slvler\Cuttly;
46

57
class HttpResponse
@@ -13,13 +15,13 @@ public function __construct($response)
1315

1416
public function getBody(): string
1517
{
16-
return (string)$this->response->getBody();
18+
return (string) $this->response->getBody();
1719
}
1820

1921
public function toObject(): object
2022
{
21-
$body = (string)$this->response->getBody();
23+
$body = (string) $this->response->getBody();
2224

23-
return json_decode($body) ?? (object)[];
25+
return json_decode($body) ?? (object) [];
2426
}
2527
}

tests/Feature/CuttlyTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace Slvler\Cuttly\Tests\Feature;
54

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

1615
Config::set([
17-
'cuttly.base_uri' => 'https://cutt.ly/api/api.php',
18-
'cuttly.api_key' => 'b4fa4f9976b41cbc0c9880c444fce27a28984',
19-
]);
16+
'cuttly.base_uri' => 'https://cutt.ly/api/api.php',
17+
'cuttly.api_key' => '',
18+
]);
2019
}
2120

2221
/**
@@ -25,27 +24,29 @@ public function setUp(): void
2524
public function test_short()
2625
{
2726
$data = [
28-
'short' => 'google.com'
27+
'short' => 'google.com',
2928
];
3029
$this->assertIsString(Cuttly::short($data));
3130
}
31+
3232
/**
3333
* @test
3434
*/
3535
public function test_edit()
3636
{
3737
$data = [
38-
'edit' => 'cutt.ly/LwdCoBmo'
38+
'edit' => 'cutt.ly/LwdCoBmo',
3939
];
4040
$this->assertIsString(Cuttly::edit($data));
4141
}
42+
4243
/**
4344
* @test
4445
*/
4546
public function test_stast()
4647
{
4748
$data = [
48-
'stats' => 'cutt.ly/ewdVijlY'
49+
'stats' => 'cutt.ly/ewdVijlY',
4950
];
5051
$this->assertIsString(Cuttly::stats($data));
5152
}

0 commit comments

Comments
 (0)