Skip to content

Commit 9c46cd1

Browse files
committed
add tests & refactoring
1 parent bb9884f commit 9c46cd1

File tree

6 files changed

+131
-86
lines changed

6 files changed

+131
-86
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\Concerns;
4+
5+
use Exception;
6+
use Illuminate\Support\Facades\Http;
7+
8+
/**
9+
* @method static \Illuminate\Http\Client\Response get(string $path, array $data = [], bool $async = false, array $headers = [])
10+
* @method static \Illuminate\Http\Client\Response post(string $path, array $data = [], bool $async = false, array $headers = [])
11+
* @method static \Illuminate\Http\Client\Response put(string $path, array $data = [], bool $async = false, array $headers = [])
12+
* @method static \Illuminate\Http\Client\Response delete(string $path, array $data = [], bool $async = false, array $headers = [])
13+
* @method static \Illuminate\Http\Client\Response patch(string $path, array $data = [], bool $async = false, array $headers = [])
14+
*/
15+
trait InteractsWithHttpRequests
16+
{
17+
public function __call(string $function, array $args): mixed
18+
{
19+
$options = ['get', 'post', 'put', 'delete', 'patch'];
20+
$path = $args[0] ?? null;
21+
$data = $args[1] ?? [];
22+
$async = $args[2] ?? false;
23+
$headers = $args[3] ?? [];
24+
25+
if (! in_array($function, $options)) {
26+
throw new Exception("Method {$function} not found.");
27+
}
28+
29+
return self::sendRequest(
30+
type: $function,
31+
request: $path,
32+
data: $data,
33+
headers: $headers,
34+
async: $async
35+
);
36+
}
37+
38+
/**
39+
* @throws \Exception
40+
*/
41+
protected function sendRequest(string $type, string $request, array $data = [], array $headers = [], bool $async = false): mixed
42+
{
43+
try {
44+
$apiKey = config('laravel-sendy.api_key');
45+
$apiUrl = config('laravel-sendy.api_url');
46+
47+
throw_if(
48+
blank($apiKey),
49+
new Exception('API Key is not set in the config file.')
50+
);
51+
throw_if(
52+
blank($apiUrl),
53+
new Exception('API URL is not set in the config file.')
54+
);
55+
56+
$payload = array_merge($data, [
57+
'api_key' => $apiKey,
58+
]);
59+
60+
$url = rtrim($apiUrl, '/').'/'.ltrim($request, '/');
61+
62+
$client = Http::withHeaders(array_merge([
63+
'Content-Type' => 'application/json',
64+
'Accept' => 'application/json',
65+
], $headers ?? []));
66+
67+
return $async
68+
? $client->async()->{$type}($url, $payload)
69+
: $client->{$type}($url, $payload);
70+
71+
} catch (Exception $th) {
72+
throw new Exception('Error: '.$th->getMessage());
73+
}
74+
}
75+
}

src/DTOs/Lists/ListsDTO.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Lists;
4+
5+
use Spatie\LaravelData\Data;
6+
use Spatie\LaravelData\Support\Validation\ValidationContext;
7+
8+
class ListsDTO extends Data
9+
{
10+
public function __construct(
11+
public string $brand_id,
12+
public ?string $include_hidden = 'no',
13+
) {}
14+
15+
public static function rules(ValidationContext $context): array
16+
{
17+
return [
18+
'include_hidden' => ['in:yes,no'],
19+
];
20+
}
21+
}

src/Facades/LaravelSendy.php

-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
* @method static \Coderflex\LaravelSendy\Resources\Lists lists()
1212
* @method static \Coderflex\LaravelSendy\Resources\Brands brands()
1313
* @method static \Coderflex\LaravelSendy\Resources\Campaigns campaigns()
14-
* @method static \Illuminate\Http\Client\Response get(string $path, array $data = [], bool $async = false, array $headers = [])
15-
* @method static \Illuminate\Http\Client\Response post(string $path, array $data = [], bool $async = false, array $headers = [])
16-
* @method static \Illuminate\Http\Client\Response put(string $path, array $data = [], bool $async = false, array $headers = [])
17-
* @method static \Illuminate\Http\Client\Response delete(string $path, array $data = [], bool $async = false, array $headers = [])
18-
* @method static \Illuminate\Http\Client\Response patch(string $path, array $data = [], bool $async = false, array $headers = [])
1914
*/
2015
class LaravelSendy extends Facade
2116
{

src/LaravelSendy.php

+2-74
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,11 @@
22

33
namespace Coderflex\LaravelSendy;
44

5-
use Exception;
6-
use Illuminate\Support\Facades\Http;
5+
use Coderflex\LaravelSendy\Concerns\InteractsWithHttpRequests;
76

87
class LaravelSendy
98
{
10-
protected string $apiKey;
11-
12-
protected string $apiUrl;
13-
14-
public function __construct()
15-
{
16-
if (blank(config('laravel-sendy.api_key'))) {
17-
throw new Exception('API Key is not set in the config file.');
18-
}
19-
20-
if (blank(config('laravel-sendy.api_url'))) {
21-
throw new Exception('API URL is not set in the config file.');
22-
}
23-
24-
$this->apiKey = config('laravel-sendy.api_key');
25-
$this->apiUrl = config('laravel-sendy.api_url');
26-
}
9+
use InteractsWithHttpRequests;
2710

2811
public function subscribers(): Resources\Subscribers
2912
{
@@ -44,59 +27,4 @@ public function campaigns(): Resources\Campaigns
4427
{
4528
return new Resources\Campaigns;
4629
}
47-
48-
public function __call(string $function, array $args): mixed
49-
{
50-
$options = ['get', 'post', 'put', 'delete', 'patch'];
51-
$path = $args[0] ?? null;
52-
$data = $args[1] ?? [];
53-
$async = $args[2] ?? false;
54-
$headers = $args[3] ?? [];
55-
56-
if (! in_array($function, $options)) {
57-
throw new Exception("Method {$function} not found.");
58-
}
59-
60-
return self::sendRequest(
61-
type: $function,
62-
request: $path,
63-
data: $data,
64-
headers: $headers,
65-
async: $async
66-
);
67-
}
68-
69-
/**
70-
* @throws \Exception
71-
*/
72-
protected function sendRequest(string $type, string $request, array $data = [], array $headers = [], bool $async = false): mixed
73-
{
74-
try {
75-
$mainHeaders = array_merge([
76-
'Content-Type' => 'application/json',
77-
'Accept' => 'application/json',
78-
], $headers ?? []);
79-
80-
$payload = array_merge($data, [
81-
'api_key' => $this->apiKey,
82-
]);
83-
84-
$url = rtrim($this->apiUrl, '/').'/'.ltrim($request, '/');
85-
86-
$client = Http::withHeaders($headers);
87-
88-
return $async
89-
? $client->async()->{$type}($url, $payload)
90-
: $client->{$type}($url, $payload);
91-
92-
} catch (Exception $th) {
93-
throw new Exception('Error: '.$th->getMessage());
94-
}
95-
}
96-
97-
protected function isJson(string $string): bool
98-
{
99-
return is_array(json_decode($string)) &&
100-
(json_last_error() === JSON_ERROR_NONE);
101-
}
10230
}

src/Resources/Lists.php

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Coderflex\LaravelSendy\Resources;
44

5+
use Coderflex\LaravelSendy\DTOs\Lists\ListsDTO;
56
use Coderflex\LaravelSendy\Facades\LaravelSendy;
67

78
class Lists
@@ -11,13 +12,10 @@ class Lists
1112
*
1213
* @return array
1314
*/
14-
public function get(int $brandId, bool $includeHidden = false)
15+
public function get(array $data, bool $async = false)
1516
{
16-
$params = http_build_query([
17-
'brand_id' => $brandId,
18-
'include_hidden' => $includeHidden,
19-
]);
17+
$data = ListsDTO::validate($data);
2018

21-
return LaravelSendy::get('/api/lists/get-lists.php', $params);
19+
return LaravelSendy::post('/api/lists/get-lists.php', $data, $async);
2220
}
2321
}

tests/Resources/ListsTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
<?php
22

3-
//
3+
use Coderflex\LaravelSendy\Facades\LaravelSendy;
4+
use Illuminate\Support\Facades\Http;
5+
6+
beforeEach(function () {
7+
config([
8+
'laravel-sendy.api_key' => 'test_api_key',
9+
'laravel-sendy.api_url' => 'https://sendy.test/',
10+
]);
11+
});
12+
13+
it('can get subscriber lists', function () {
14+
Http::fake([
15+
'https://sendy.test/api/lists/get-lists.php' => Http::response([123 => 'Custom List'], 200),
16+
]);
17+
18+
$response = LaravelSendy::lists()->get([
19+
'brand_id' => 123,
20+
'include_hidden' => 'yes',
21+
]);
22+
23+
expect($response->json())->toBe([123 => 'Custom List']);
24+
25+
Http::assertSent(function ($request) {
26+
return $request->url() === 'https://sendy.test/api/lists/get-lists.php' &&
27+
$request['brand_id'] === 123 &&
28+
$request['include_hidden'] === 'yes' &&
29+
$request['api_key'] === 'test_api_key';
30+
});
31+
});

0 commit comments

Comments
 (0)