Skip to content

Commit 67d836a

Browse files
committed
refactoring
1 parent 1979c24 commit 67d836a

File tree

7 files changed

+50
-33
lines changed

7 files changed

+50
-33
lines changed

src/Concerns/InteractsWithHttpRequests.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Coderflex\LaravelSendy\Exceptions\InvalidApiKeyException;
66
use Coderflex\LaravelSendy\Exceptions\InvalidApiUrlException;
77
use Exception;
8+
use Illuminate\Http\Client\Response;
89
use Illuminate\Support\Facades\Http;
910

1011
/**
@@ -16,7 +17,7 @@
1617
*/
1718
trait InteractsWithHttpRequests
1819
{
19-
public function __call(string $function, array $args): mixed
20+
public function __call(string $function, array $args): Response
2021
{
2122
$options = ['get', 'post', 'put', 'delete', 'patch'];
2223
$path = $args[0] ?? null;
@@ -65,7 +66,7 @@ protected function sendRequest(string $type, string $request, array $data = [],
6566
$client = Http::withHeaders(array_merge([
6667
'Content-Type' => 'application/json',
6768
'Accept' => 'application/json',
68-
], $headers ?? []));
69+
], $headers));
6970

7071
return $async
7172
? $client->async()->{$type}($url, $payload)

src/Facades/Sendy.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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 mixed post(string $endpoint, array $data, bool $async = false)
1415
*/
1516
class Sendy extends Facade
1617
{

src/Resources/Brands.php

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
namespace Coderflex\LaravelSendy\Resources;
44

5-
use Coderflex\LaravelSendy\Facades\Sendy;
5+
use Coderflex\LaravelSendy\Sendy;
6+
use Illuminate\Http\Client\Response;
67

78
class Brands
89
{
9-
public function get()
10+
public function __construct(
11+
protected Sendy $sendy
12+
) {}
13+
14+
public function get(): Response
1015
{
11-
return Sendy::post('/api/brands/get-brands.php');
16+
return $this->sendy->post('/api/brands/get-brands.php');
1217
}
1318
}

src/Resources/Campaigns.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33
namespace Coderflex\LaravelSendy\Resources;
44

55
use Coderflex\LaravelSendy\DTOs\Campaigns\CampaignDTO;
6-
use Coderflex\LaravelSendy\Facades\Sendy;
6+
use Coderflex\LaravelSendy\Sendy;
7+
use Illuminate\Http\Client\Response;
78

89
class Campaigns
910
{
10-
public function create(array $data, bool $async = false)
11+
public function __construct(
12+
protected Sendy $sendy
13+
) {}
14+
15+
public function create(array $data, bool $async = false): Response
1116
{
1217
$data = CampaignDTO::validate($data);
1318

14-
return Sendy::post('/api/campaigns/create.php', $data, $async);
19+
return $this->sendy->post('/api/campaigns/create.php', $data, $async);
1520
}
1621

17-
public function createAndSend(array $data, bool $async = false)
22+
public function createAndSend(array $data, bool $async = false): Response
1823
{
1924
$data = array_merge($data, [
2025
'send_compaign' => 1,
2126
]);
2227

23-
return Sendy::post('/api/campaigns/create.php', $data, $async);
28+
return $this->sendy->post('/api/campaigns/create.php', $data, $async);
2429
}
2530
}

src/Resources/Lists.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace Coderflex\LaravelSendy\Resources;
44

55
use Coderflex\LaravelSendy\DTOs\Lists\ListsDTO;
6-
use Coderflex\LaravelSendy\Facades\Sendy;
6+
use Coderflex\LaravelSendy\Sendy;
7+
use Illuminate\Http\Client\Response;
78

89
class Lists
910
{
10-
/**
11-
* Get all lists for a specific brand.
12-
*
13-
* @return array
14-
*/
15-
public function get(array $data, bool $async = false)
11+
public function __construct(
12+
protected Sendy $sendy
13+
) {}
14+
15+
public function get(array $data, bool $async = false): Response
1616
{
1717
$data = ListsDTO::validate($data);
1818

19-
return Sendy::post('/api/lists/get-lists.php', $data, $async);
19+
return $this->sendy->post('/api/lists/get-lists.php', $data, $async);
2020
}
2121
}

src/Resources/Subscribers.php

+16-11
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,49 @@
66
use Coderflex\LaravelSendy\DTOs\Subscribers\SubscribeDTO;
77
use Coderflex\LaravelSendy\DTOs\Subscribers\SubscriberStatusDTO;
88
use Coderflex\LaravelSendy\DTOs\Subscribers\UnsubscribeDTO;
9-
use Coderflex\LaravelSendy\Facades\Sendy;
9+
use Coderflex\LaravelSendy\Sendy;
10+
use Illuminate\Http\Client\Response;
1011

1112
class Subscribers
1213
{
13-
public function subscribe(array $data, bool $async = false)
14+
public function __construct(
15+
protected Sendy $sendy
16+
) {}
17+
18+
public function subscribe(array $data, bool $async = false): Response
1419
{
1520
$data = SubscribeDTO::validate($data);
1621

17-
return Sendy::post('subscribe', $data, $async);
22+
return $this->sendy->post('subscribe', $data, $async);
1823
}
1924

20-
public function unsubscribe(array $data, bool $async = false)
25+
public function unsubscribe(array $data, bool $async = false): Response
2126
{
2227
$data = UnsubscribeDTO::validate($data);
2328

24-
return Sendy::post('api/subscribers/unsubscribe.php', $data, $async);
29+
return $this->sendy->post('api/subscribers/unsubscribe.php', $data, $async);
2530
}
2631

27-
public function delete(array $data, bool $async = false)
32+
public function delete(array $data, bool $async = false): Response
2833
{
2934
$data = DeleteSubscriberDTO::validate($data);
3035

31-
return Sendy::post('api/subscribers/delete.php', $data, $async);
36+
return $this->sendy->post('api/subscribers/delete.php', $data, $async);
3237
}
3338

34-
public function status(array $data, bool $async = false)
39+
public function status(array $data, bool $async = false): Response
3540
{
3641
$data = SubscriberStatusDTO::validate($data);
3742

38-
return Sendy::post('api/subscribers/subscription-status.php', $data, $async);
43+
return $this->sendy->post('api/subscribers/subscription-status.php', $data, $async);
3944
}
4045

41-
public function count(int $listId, bool $async = false)
46+
public function count(int $listId, bool $async = false): Response
4247
{
4348
$data = [
4449
'list_id' => $listId,
4550
];
4651

47-
return Sendy::post('api/subscribers/subscriber-count.php', $data, $async);
52+
return $this->sendy->post('api/subscribers/subscriber-count.php', $data, $async);
4853
}
4954
}

src/Sendy.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ class Sendy
1010

1111
public function subscribers(): Resources\Subscribers
1212
{
13-
return new Resources\Subscribers;
13+
return new Resources\Subscribers($this);
1414
}
1515

1616
public function lists(): Resources\Lists
1717
{
18-
return new Resources\Lists;
18+
return new Resources\Lists($this);
1919
}
2020

2121
public function brands(): Resources\Brands
2222
{
23-
return new Resources\Brands;
23+
return new Resources\Brands($this);
2424
}
2525

2626
public function campaigns(): Resources\Campaigns
2727
{
28-
return new Resources\Campaigns;
28+
return new Resources\Campaigns($this);
2929
}
3030
}

0 commit comments

Comments
 (0)