Skip to content

Commit 44537ae

Browse files
authored
Merge pull request #5 from coderflexx/tests-scaffolding
Tests - API Resource.
2 parents 4d29f9c + 500dd0c commit 44537ae

24 files changed

+484
-178
lines changed

composer.json

-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
"php": "^8.4",
2020
"ext-curl": "*",
2121
"ext-json": "*",
22-
"guzzlehttp/guzzle": "7.x",
2322
"illuminate/contracts": "^10.0||^11.0||^12.0",
24-
"league/oauth2-client": "^2",
2523
"spatie/laravel-data": "^4.15",
2624
"spatie/laravel-package-tools": "^1.16"
2725
},
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\Concerns;
4+
5+
use Coderflex\LaravelSendy\Exceptions\InvalidApiKeyException;
6+
use Coderflex\LaravelSendy\Exceptions\InvalidApiUrlException;
7+
use Exception;
8+
use Illuminate\Support\Facades\Http;
9+
10+
/**
11+
* @method static \Illuminate\Http\Client\Response get(string $path, array $data = [], bool $async = false, array $headers = [])
12+
* @method static \Illuminate\Http\Client\Response post(string $path, array $data = [], bool $async = false, array $headers = [])
13+
* @method static \Illuminate\Http\Client\Response put(string $path, array $data = [], bool $async = false, array $headers = [])
14+
* @method static \Illuminate\Http\Client\Response delete(string $path, array $data = [], bool $async = false, array $headers = [])
15+
* @method static \Illuminate\Http\Client\Response patch(string $path, array $data = [], bool $async = false, array $headers = [])
16+
*/
17+
trait InteractsWithHttpRequests
18+
{
19+
public function __call(string $function, array $args): mixed
20+
{
21+
$options = ['get', 'post', 'put', 'delete', 'patch'];
22+
$path = $args[0] ?? null;
23+
$data = $args[1] ?? [];
24+
$async = $args[2] ?? false;
25+
$headers = $args[3] ?? [];
26+
27+
if (! in_array($function, $options)) {
28+
throw new Exception("Method {$function} not found.");
29+
}
30+
31+
return self::sendRequest(
32+
type: $function,
33+
request: $path,
34+
data: $data,
35+
headers: $headers,
36+
async: $async
37+
);
38+
}
39+
40+
/**
41+
* @throws \Exception
42+
*/
43+
protected function sendRequest(string $type, string $request, array $data = [], array $headers = [], bool $async = false): mixed
44+
{
45+
try {
46+
$apiKey = config('laravel-sendy.api_key');
47+
$apiUrl = config('laravel-sendy.api_url');
48+
49+
throw_if(
50+
blank($apiKey),
51+
InvalidApiKeyException::class,
52+
);
53+
54+
throw_if(
55+
blank($apiUrl),
56+
InvalidApiUrlException::class,
57+
);
58+
59+
$payload = array_merge($data, [
60+
'api_key' => $apiKey,
61+
]);
62+
63+
$url = rtrim($apiUrl, '/').'/'.ltrim($request, '/');
64+
65+
$client = Http::withHeaders(array_merge([
66+
'Content-Type' => 'application/json',
67+
'Accept' => 'application/json',
68+
], $headers ?? []));
69+
70+
return $async
71+
? $client->async()->{$type}($url, $payload)
72+
: $client->{$type}($url, $payload);
73+
74+
} catch (InvalidApiKeyException $th) {
75+
throw new InvalidApiKeyException('Error: '.$th->getMessage());
76+
} catch (InvalidApiUrlException $th) {
77+
throw new InvalidApiUrlException('Error: '.$th->getMessage());
78+
} catch (Exception $th) {
79+
throw new Exception('Error: '.$th->getMessage());
80+
}
81+
}
82+
}

src/DTOs/CompaignDTO.php renamed to src/DTOs/Campaigns/CampaignDTO.php

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

3-
namespace Coderflex\LaravelSendy\DTOs;
3+
namespace Coderflex\LaravelSendy\DTOs\Campaigns;
44

55
use Spatie\LaravelData\Data;
66
use Spatie\LaravelData\Support\Validation\ValidationContext;
77

8-
class CompaignDTO extends Data
8+
class CampaignDTO extends Data
99
{
1010
public function __construct(
1111
public string $from_name,
@@ -15,15 +15,15 @@ public function __construct(
1515
public string $subject,
1616
public ?string $plain_text,
1717
public string $html_text,
18-
public string $list_ids,
19-
public string $segment_ids,
18+
public ?string $list_ids,
19+
public ?string $segment_ids,
2020
public ?string $exclude_list_ids,
2121
public ?string $exclude_segment_ids,
22-
public string $brand_id,
22+
public ?string $brand_id,
2323
public ?string $query_string,
2424
public ?int $track_opens,
2525
public ?int $track_clicks,
26-
public ?int $send_compaign,
26+
public ?int $send_campaign,
2727
public ?string $schedule_date_time,
2828
public ?string $schedule_timezone,
2929
) {}
@@ -38,15 +38,15 @@ public static function rules(ValidationContext $context): array
3838
'subject' => ['required', 'string'],
3939
'plain_text' => ['string', 'nullable'],
4040
'html_text' => ['required', 'string'],
41-
'list_ids' => ['required', 'string'],
42-
'segment_ids' => ['required', 'string'],
41+
'list_ids' => ['required_if:send_campaign,1', 'string'],
42+
'segment_ids' => ['required_if:send_campaign,1', 'string'],
4343
'exclude_list_ids' => ['string', 'nullable'],
4444
'exclude_segment_ids' => ['string', 'nullable'],
45-
'brand_id' => ['required', 'string'],
45+
'brand_id' => ['required_if:send_campaign,0', 'string'],
4646
'query_string' => ['string', 'nullable'],
4747
'track_opens' => ['integer', 'nullable', 'in:0,1,2'],
4848
'track_clicks' => ['integer', 'nullable', 'in:0,1,2'],
49-
'send_compaign' => ['integer', 'nullable', 'in:0,1'],
49+
'send_campaign' => ['integer', 'nullable', 'in:0,1'],
5050
'schedule_date_time' => ['date', 'nullable'],
5151
'schedule_timezone' => ['date', 'nullable'],
5252
];

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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
4+
5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
6+
use Spatie\LaravelData\Data;
7+
use Spatie\LaravelData\Support\Validation\ValidationContext;
8+
9+
#[MergeValidationRules]
10+
class DeleteSubscriberDTO extends Data
11+
{
12+
public function __construct(
13+
public string $list_id,
14+
public string $email,
15+
) {}
16+
17+
public static function rules(ValidationContext $context): array
18+
{
19+
return [
20+
'email' => ['email'],
21+
];
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
<?php
22

3-
namespace Coderflex\LaravelSendy\DTOs;
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
44

5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
56
use Spatie\LaravelData\Data;
67
use Spatie\LaravelData\Support\Validation\ValidationContext;
78

8-
class SubscribersDTO extends Data
9+
#[MergeValidationRules]
10+
class SubscribeDTO extends Data
911
{
1012
public function __construct(
1113
public ?string $name,
@@ -22,15 +24,8 @@ public function __construct(
2224
public static function rules(ValidationContext $context): array
2325
{
2426
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'],
27+
'email' => ['email'],
28+
'ipaddress' => ['ip'],
3429
];
3530
}
3631
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
4+
5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
6+
use Spatie\LaravelData\Data;
7+
use Spatie\LaravelData\Support\Validation\ValidationContext;
8+
9+
#[MergeValidationRules]
10+
class SubscriberStatusDTO extends Data
11+
{
12+
public function __construct(
13+
public string $list_id,
14+
public string $email,
15+
) {}
16+
17+
public static function rules(ValidationContext $context): array
18+
{
19+
return [
20+
'email' => ['email'],
21+
];
22+
}
23+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\DTOs\Subscribers;
4+
5+
use Spatie\LaravelData\Attributes\MergeValidationRules;
6+
use Spatie\LaravelData\Data;
7+
use Spatie\LaravelData\Support\Validation\ValidationContext;
8+
9+
#[MergeValidationRules]
10+
class UnsubscribeDTO extends Data
11+
{
12+
public function __construct(
13+
public string $list,
14+
public string $email,
15+
public ?bool $boolean = false, // plain text response
16+
) {}
17+
18+
public static function rules(ValidationContext $context): array
19+
{
20+
return [
21+
'email' => ['email'],
22+
];
23+
}
24+
}

src/Exceptions/CompaingException.php

-8
This file was deleted.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\Exceptions;
4+
5+
class InvalidApiKeyException extends \Exception
6+
{
7+
protected $message = 'The API key is invalid. Please check your configuration and try again.';
8+
9+
protected $code = 401;
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Coderflex\LaravelSendy\Exceptions;
4+
5+
class InvalidApiUrlException extends \Exception
6+
{
7+
protected $message = 'The API URL is invalid. Please check your configuration and try again.';
8+
9+
protected $code = 401;
10+
}

src/Exceptions/SubscribersException.php

-8
This file was deleted.

src/Facades/LaravelSendy.php

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
/**
88
* @see \Coderflex\LaravelSendy\LaravelSendy
9+
*
10+
* @method static \Coderflex\LaravelSendy\Resources\Subscribers subscribers()
11+
* @method static \Coderflex\LaravelSendy\Resources\Lists lists()
12+
* @method static \Coderflex\LaravelSendy\Resources\Brands brands()
13+
* @method static \Coderflex\LaravelSendy\Resources\Campaigns campaigns()
914
*/
1015
class LaravelSendy extends Facade
1116
{

0 commit comments

Comments
 (0)