Skip to content

Commit eb505e6

Browse files
committed
refactoring & create first test
1 parent 1547e70 commit eb505e6

File tree

8 files changed

+93
-72
lines changed

8 files changed

+93
-72
lines changed

src/DTOs/SubscribersDTO.php

+14-14
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ public function __construct(
1919
public ?bool $boolean,
2020
) {}
2121

22-
public static function rules(ValidationContext $context): array
23-
{
24-
return [
25-
'name' => ['string', 'nullable'],
26-
'email' => ['required', 'string', 'email'],
27-
'list' => ['required', 'string'],
28-
'country' => ['string', 'nullable'],
29-
'ipaddress' => ['string', 'nullable', 'ip'],
30-
'referrer' => ['string', 'nullable'],
31-
'gdpr' => ['boolean', 'nullable'],
32-
'silent' => ['boolean', 'nullable'],
33-
'boolean' => ['boolean', 'nullable'],
34-
];
35-
}
22+
// public static function rules(ValidationContext $context): array
23+
// {
24+
// return [
25+
// 'name' => ['string', 'nullable'],
26+
// 'email' => ['required', 'string', 'email'],
27+
// 'list' => ['required', 'string'],
28+
// 'country' => ['string', 'nullable'],
29+
// 'ipaddress' => ['string', 'nullable', 'ip'],
30+
// 'referrer' => ['string', 'nullable'],
31+
// 'gdpr' => ['boolean', 'nullable'],
32+
// 'silent' => ['boolean', 'nullable'],
33+
// 'boolean' => ['boolean', 'nullable'],
34+
// ];
35+
// }
3636
}

src/LaravelSendy.php

+34-41
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@
22

33
namespace Coderflex\LaravelSendy;
44

5-
use Coderflex\LaravelSendy\Resources\Resources\Brands;
6-
use Coderflex\LaravelSendy\Resources\Resources\Campaigns;
7-
use Coderflex\LaravelSendy\Resources\Resources\Lists;
8-
use Coderflex\LaravelSendy\Resources\Resources\Subscribers;
95
use Exception;
10-
use GuzzleHttp\Client;
11-
use GuzzleHttp\Exception\ClientException;
6+
use Illuminate\Support\Facades\Http;
127

138
class LaravelSendy
149
{
@@ -30,77 +25,75 @@ public function __construct()
3025
$this->apiUrl = config('laravel-sendy.api_url');
3126
}
3227

33-
public function subscribers(): Subscribers
28+
public function subscribers(): Resources\Subscribers
3429
{
35-
return new Subscribers;
30+
return new Resources\Subscribers;
3631
}
3732

38-
public function lists(): Lists
33+
public function lists(): Resources\Lists
3934
{
40-
return new Lists;
35+
return new Resources\Lists;
4136
}
4237

43-
public function brands(): Brands
38+
public function brands(): Resources\Brands
4439
{
45-
return new Brands;
40+
return new Resources\Brands;
4641
}
4742

48-
public function campaigns(): Campaigns
43+
public function campaigns(): Resources\Campaigns
4944
{
50-
return new Campaigns;
45+
return new Resources\Campaigns;
5146
}
5247

53-
public function __call(string $function, array $args)
48+
public function __call(string $function, array $args): mixed
5449
{
5550
$options = ['get', 'post', 'put', 'delete', 'patch'];
56-
$path = (isset($args[0])) ? $args[0] : null;
57-
$data = (isset($args[1])) ? $args[1] : [];
58-
$headers = (isset($args[2])) ? $args[2] : [];
51+
$path = $args[0] ?? null;
52+
$data = $args[1] ?? [];
53+
$async = $args[2] ?? false;
54+
$headers = $args[3] ?? [];
5955

6056
if (! in_array($function, $options)) {
6157
throw new Exception("Method {$function} not found.");
6258
}
6359

64-
return self::guzzle(
60+
return self::sendRequest(
6561
type: $function,
6662
request: $path,
6763
data: $data,
68-
headers: $headers
64+
headers: $headers,
65+
async: $async
6966
);
7067
}
7168

7269
/**
7370
* @throws \Exception
7471
*/
75-
protected function guzzle(string $type, string $request, array $data = [], array $headers = []): mixed
76-
{
72+
protected function sendRequest(
73+
string $type,
74+
string $request,
75+
array $data = [],
76+
array $headers = [],
77+
bool $async = false
78+
): mixed {
7779
try {
78-
$client = new Client;
79-
80-
$mainHeaders = [
80+
$mainHeaders = array_merge([
8181
'Content-Type' => 'application/json',
8282
'Accept' => 'application/json',
83-
];
84-
85-
$headers = is_array($headers) && count($headers) > 0
86-
? array_merge($mainHeaders, $headers)
87-
: $mainHeaders;
83+
], $headers ?? []);
8884

89-
$response = $client->{$type}($this->apiUrl.$request, [
90-
'headers' => $headers,
91-
'body' => json_encode(array_merge($data, [
92-
'api_key' => $this->apiKey,
93-
])),
85+
$payload = array_merge($data, [
86+
'api_key' => $this->apiKey,
9487
]);
9588

96-
$responseObject = $response->getBody()->getContents();
89+
$url = str_replace('//', '/', "$this->apiUrl/$request");
9790

98-
return $this->isJson($responseObject)
99-
? json_decode($responseObject, true)
100-
: $responseObject;
91+
$client = Http::withHeaders($headers);
92+
93+
return $async
94+
? $client->async()->{$type}($url, $payload)
95+
: $client->{$type}($url, $payload);
10196

102-
} catch (ClientException $th) {
103-
throw new Exception('Error: '.$th->getMessage());
10497
} catch (Exception $th) {
10598
throw new Exception('Error: '.$th->getMessage());
10699
}

src/Resources/Brands.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Coderflex\LaravelSendy\Resources\Resources;
3+
namespace Coderflex\LaravelSendy\Resources;
44

55
use Coderflex\LaravelSendy\Facades\LaravelSendy;
66

src/Resources/Campaigns.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Coderflex\LaravelSendy\Resources\Resources;
3+
namespace Coderflex\LaravelSendy\Resources;
44

55
use Coderflex\LaravelSendy\DTOs\CompaignDTO;
66
use Coderflex\LaravelSendy\Facades\LaravelSendy;

src/Resources/Lists.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Coderflex\LaravelSendy\Resources\Resources;
3+
namespace Coderflex\LaravelSendy\Resources;
44

55
use Coderflex\LaravelSendy\Facades\LaravelSendy;
66

src/Resources/Subscribers.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
<?php
22

3-
namespace Coderflex\LaravelSendy\Resources\Resources;
3+
namespace Coderflex\LaravelSendy\Resources;
44

55
use Coderflex\LaravelSendy\DTOs\SubscribersDTO;
66
use Coderflex\LaravelSendy\Facades\LaravelSendy;
77

88
class Subscribers
99
{
10-
public function subscribe(array $data)
10+
public function subscribe(array $data, bool $async = false)
1111
{
1212
$data = SubscribersDTO::validateAndCreate($data)->toArray();
1313

14-
return LaravelSendy::post('subscribe', $data);
14+
return LaravelSendy::post('subscribe', $data, $async);
1515
}
1616

17-
public function unsubscribe(int $listId, string $email, bool $plainTextResponse)
17+
public function unsubscribe(int $listId, string $email, bool $plainTextResponse, bool $async = false)
1818
{
1919
$data = http_build_query([
2020
'list' => $listId,
2121
'email' => $email,
2222
'boolean' => $plainTextResponse,
2323
]);
2424

25-
return LaravelSendy::post('/api/subscribers/unsubscribe.php', $data);
25+
return LaravelSendy::post('api/subscribers/unsubscribe.php', $data, $async);
2626
}
2727

28-
public function delete(int $listId, string $email)
28+
public function delete(int $listId, string $email, bool $async = false)
2929
{
3030
$data = http_build_query([
3131
'list_id' => $listId,
3232
'email' => $email,
3333
]);
3434

35-
return LaravelSendy::post('/api/subscribers/delete.php', $data);
35+
return LaravelSendy::post('api/subscribers/delete.php', $data, $async);
3636
}
3737

38-
public function status(int $listId, string $email)
38+
public function status(int $listId, string $email, bool $async = false)
3939
{
4040
$data = http_build_query([
4141
'list_id' => $listId,
4242
'email' => $email,
4343
]);
4444

45-
return LaravelSendy::post('/api/subscribers/subscription-status.php', $data);
45+
return LaravelSendy::post('api/subscribers/subscription-status.php', $data, $async);
4646
}
4747

48-
public function count(int $listId)
48+
public function count(int $listId, bool $async = false)
4949
{
5050
$data = http_build_query([
5151
'list_id' => $listId,
5252
]);
5353

54-
return LaravelSendy::post('/api/subscribers/active-subscriber-count.php', $data);
54+
return LaravelSendy::post('api/subscribers/subscriber-count.php', $data, $async);
5555
}
5656
}

tests/Resources/BrandsTest.php

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
<?php
2-
3-
//

tests/Resources/SubscribersTest.php

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,33 @@
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 subscribe a user', function () {
14+
Http::fake([
15+
'https://sendy.test/subscribe' => Http::response(['success' => true], 200),
16+
]);
17+
18+
$response = LaravelSendy::subscribers()->subscribe([
19+
'name' => 'John Doe',
20+
'email' => 'john@example.com',
21+
'list' => 'abc123',
22+
'country' => 'UAE',
23+
]);
24+
25+
expect($response)->toBe(['success' => true]);
26+
27+
Http::assertSent(function ($request) {
28+
return $request->url() === 'https://sendy.test/subscribe' &&
29+
$request['email'] === 'john@example.com' &&
30+
$request['list'] === 'abc123' &&
31+
$request['api_key'] === 'test_api_key';
32+
});
33+
});

0 commit comments

Comments
 (0)