Skip to content

Commit 67a67e6

Browse files
committed
Add messages client
1 parent 58fa926 commit 67a67e6

30 files changed

+758
-16
lines changed

src/Data.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function toArray(): array
5050

5151
if (str_contains($type, '[]')) {
5252
/** @phpstan-ignore property.dynamicName */
53-
$data[$snakeCaseKey] = array_map(fn ($item) => $item->toArray(), $this->$key);
53+
$data[$snakeCaseKey] = array_map(fn($item) => $item->toArray(), $this->$key);
5454

5555
continue;
5656
}
@@ -78,7 +78,7 @@ public function fromArray(array $incomingData): static
7878
$className = str_replace('[]', '', $type);
7979
$className = ltrim($className, '\\');
8080
/** @phpstan-ignore property.dynamicName */
81-
$this->$key = array_map(fn ($item) => (new $className())->fromArray($item), $incomingData[$snakeCaseKey]);
81+
$this->$key = array_map(fn($item) => (new $className())->fromArray($item), $incomingData[$snakeCaseKey]);
8282

8383
continue;
8484
}

src/Domain/Client.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
use TempMailIo\TempMailPhp\Domain\Data\Response\GetAvailableDomainResponse;
1313
use TempMailIo\TempMailPhp\Domain\Data\Response\GetAvailableDomainsSuccessResponse;
1414
use TempMailIo\TempMailPhp\GenericData\ErrorResponse;
15-
use TempMailIo\TempMailPhp\RateLimitReader;
15+
use TempMailIo\TempMailPhp\RateLimitReaderInterface;
1616

1717
class Client implements ClientInterface
1818
{
1919
public function __construct(
20-
private readonly GuzzleClientInterface $guzzleClient,
21-
private readonly RateLimitReader $rateLimitReader,
22-
private readonly string $apiKey,
23-
) {
20+
private readonly GuzzleClientInterface $guzzleClient,
21+
private readonly RateLimitReaderInterface $rateLimitReader,
22+
private readonly string $apiKey,
23+
)
24+
{
2425
}
2526

2627
/**

src/Domain/ClientInterface.php

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

3+
declare(strict_types=1);
4+
35
namespace TempMailIo\TempMailPhp\Domain;
46

57
use GuzzleHttp\Exception\GuzzleException;

src/Domain/Data/Response/Domain.php

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

3+
declare(strict_types=1);
4+
35
namespace TempMailIo\TempMailPhp\Domain\Data\Response;
46

57
use TempMailIo\TempMailPhp\Data;

src/Domain/Data/Response/GetAvailableDomainResponse.php

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

3+
declare(strict_types=1);
4+
35
namespace TempMailIo\TempMailPhp\Domain\Data\Response;
46

57
use TempMailIo\TempMailPhp\Data;

src/Email/Client.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
use TempMailIo\TempMailPhp\Email\Data\Response\GetMessagesResponse;
1717
use TempMailIo\TempMailPhp\Email\Data\Response\GetMessagesSuccessResponse;
1818
use TempMailIo\TempMailPhp\GenericData\ErrorResponse;
19-
use TempMailIo\TempMailPhp\GenericData\RateLimit;
2019
use TempMailIo\TempMailPhp\GenericData\SuccessResponse;
21-
use TempMailIo\TempMailPhp\RateLimitReader;
20+
use TempMailIo\TempMailPhp\RateLimitReaderInterface;
2221

2322
class Client implements ClientInterface
2423
{
2524
public function __construct(
26-
private readonly GuzzleClientInterface $guzzleClient,
27-
private readonly RateLimitReader $rateLimitReader,
28-
private readonly string $apiKey,
29-
) {
25+
private readonly GuzzleClientInterface $guzzleClient,
26+
private readonly RateLimitReaderInterface $rateLimitReader,
27+
private readonly string $apiKey,
28+
)
29+
{
3030
}
3131

3232
/**

src/Factory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use TempMailIo\TempMailPhp\Domain\ClientInterface as DomainClientInterface;
1212
use TempMailIo\TempMailPhp\RateLimit\Client as RateLimitClient;
1313
use TempMailIo\TempMailPhp\RateLimit\ClientInterface as RateLimitClientInterface;
14+
use TempMailIo\TempMailPhp\Message\Client as MessageClient;
15+
use TempMailIo\TempMailPhp\Message\ClientInterface as MessageClientInterface;
1416

1517
class Factory
1618
{
@@ -28,4 +30,9 @@ public static function createRateLimitClient(string $apiKey): RateLimitClientInt
2830
{
2931
return new RateLimitClient(new Client(), $apiKey);
3032
}
33+
34+
public static function createMessageClient(string $apiKey): MessageClientInterface
35+
{
36+
return new MessageClient(new Client(), new RateLimitReader(), $apiKey);
37+
}
3138
}

src/Message/Client.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TempMailIo\TempMailPhp\Message;
6+
7+
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
8+
use GuzzleHttp\Exception\ClientException;
9+
use GuzzleHttp\Exception\GuzzleException;
10+
use GuzzleHttp\Exception\ServerException;
11+
use TempMailIo\TempMailPhp\Constants;
12+
use TempMailIo\TempMailPhp\GenericData\ErrorResponse;
13+
use TempMailIo\TempMailPhp\GenericData\SuccessResponse;
14+
use TempMailIo\TempMailPhp\Message\Data\Response\DeleteResponse;
15+
use TempMailIo\TempMailPhp\Message\Data\Response\DownloadAttachmentResponse;
16+
use TempMailIo\TempMailPhp\Message\Data\Response\DownloadAttachmentSuccessResponse;
17+
use TempMailIo\TempMailPhp\Message\Data\Response\GetMessageResponse;
18+
use TempMailIo\TempMailPhp\Message\Data\Response\GetMessageSourceCodeResponse;
19+
use TempMailIo\TempMailPhp\Message\Data\Response\GetMessageSourceCodeSuccessResponse;
20+
use TempMailIo\TempMailPhp\Message\Data\Response\GetMessageSuccessResponse;
21+
use TempMailIo\TempMailPhp\RateLimitReaderInterface;
22+
23+
class Client implements ClientInterface
24+
{
25+
public function __construct(
26+
private readonly GuzzleClientInterface $guzzleClient,
27+
private readonly RateLimitReaderInterface $rateLimitReader,
28+
private readonly string $apiKey,
29+
)
30+
{
31+
}
32+
33+
/**
34+
* @throws GuzzleException|\ReflectionException|ServerException
35+
*/
36+
public function getMessage(string $id): GetMessageResponse
37+
{
38+
$getMessageResponse = GetMessageResponse::create();
39+
40+
try {
41+
$response = $this->guzzleClient->request('GET', Constants::API_V1_URL . "/messages/{$id}", [
42+
'headers' => [
43+
Constants::API_KEY_HEADER => $this->apiKey,
44+
'Content-Type' => 'application/json',
45+
'Accept' => 'application/json',
46+
],
47+
]);
48+
49+
if ($response->getStatusCode() === 200) {
50+
$getMessageResponse->successResponse = GetMessageSuccessResponse::create()
51+
->fromArray(['message' => json_decode($response->getBody()->getContents(), true)]);
52+
$getMessageResponse->successResponse->rateLimit = $this->rateLimitReader->createRateLimitFromHeaders($response->getHeaders());
53+
54+
return $getMessageResponse;
55+
}
56+
} catch (ClientException $exception) {
57+
$response = $exception->getResponse();
58+
59+
$getMessageResponse->errorResponse = ErrorResponse::create()
60+
->fromArray(json_decode($response->getBody()->getContents(), true));
61+
62+
return $getMessageResponse;
63+
}
64+
65+
return $getMessageResponse;
66+
}
67+
68+
public function getMessageSourceCode(string $id): GetMessageSourceCodeResponse
69+
{
70+
$getMessageSourceCodeResponse = GetMessageSourceCodeResponse::create();
71+
72+
try {
73+
$response = $this->guzzleClient->request('GET', Constants::API_V1_URL . "/messages/{$id}/source_code", [
74+
'headers' => [
75+
Constants::API_KEY_HEADER => $this->apiKey,
76+
'Content-Type' => 'application/json',
77+
'Accept' => 'application/json',
78+
],
79+
]);
80+
81+
if ($response->getStatusCode() === 200) {
82+
$getMessageSourceCodeResponse->successResponse = GetMessageSourceCodeSuccessResponse::create()
83+
->fromArray(json_decode($response->getBody()->getContents(), true));
84+
$getMessageSourceCodeResponse->successResponse->rateLimit = $this->rateLimitReader->createRateLimitFromHeaders($response->getHeaders());
85+
86+
return $getMessageSourceCodeResponse;
87+
}
88+
} catch (ClientException $exception) {
89+
$response = $exception->getResponse();
90+
91+
$getMessageSourceCodeResponse->errorResponse = ErrorResponse::create()
92+
->fromArray(json_decode($response->getBody()->getContents(), true));
93+
94+
return $getMessageSourceCodeResponse;
95+
}
96+
97+
return $getMessageSourceCodeResponse;
98+
}
99+
100+
/**
101+
* @throws GuzzleException|\ReflectionException|ServerException
102+
*/
103+
public function delete(string $id): DeleteResponse
104+
{
105+
$deleteResponse = DeleteResponse::create();
106+
107+
try {
108+
$response = $this->guzzleClient->request('DELETE', Constants::API_V1_URL . "/messages/{$id}", [
109+
'headers' => [
110+
Constants::API_KEY_HEADER => $this->apiKey,
111+
'Content-Type' => 'application/json',
112+
'Accept' => 'application/json',
113+
]
114+
]);
115+
116+
if ($response->getStatusCode() === 200) {
117+
$deleteResponse->successResponse = SuccessResponse::create();
118+
$deleteResponse->successResponse->rateLimit = $this->rateLimitReader->createRateLimitFromHeaders($response->getHeaders());
119+
120+
return $deleteResponse;
121+
}
122+
} catch (ClientException $exception) {
123+
$response = $exception->getResponse();
124+
125+
$deleteResponse->errorResponse = ErrorResponse::create()
126+
->fromArray(json_decode($response->getBody()->getContents(), true));
127+
128+
return $deleteResponse;
129+
}
130+
131+
return $deleteResponse;
132+
}
133+
134+
public function downloadAttachment(string $id): DownloadAttachmentResponse
135+
{
136+
$downloadAttachmentResponse = DownloadAttachmentResponse::create();
137+
138+
try {
139+
$response = $this->guzzleClient->request('GET', Constants::API_V1_URL . "/attachments/{$id}", [
140+
'headers' => [
141+
Constants::API_KEY_HEADER => $this->apiKey,
142+
'Content-Type' => 'application/json',
143+
'Accept' => 'application/json',
144+
],
145+
]);
146+
147+
if ($response->getStatusCode() === 200) {
148+
$downloadAttachmentResponse->successResponse = DownloadAttachmentSuccessResponse::create()
149+
->fromArray(json_decode($response->getBody()->getContents(), true));
150+
$downloadAttachmentResponse->successResponse->rateLimit = $this->rateLimitReader->createRateLimitFromHeaders($response->getHeaders());
151+
152+
return $downloadAttachmentResponse;
153+
}
154+
} catch (ClientException $exception) {
155+
$response = $exception->getResponse();
156+
157+
$downloadAttachmentResponse->errorResponse = ErrorResponse::create()
158+
->fromArray(json_decode($response->getBody()->getContents(), true));
159+
160+
return $downloadAttachmentResponse;
161+
}
162+
163+
return $downloadAttachmentResponse;
164+
}
165+
}

src/Message/ClientInterface.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TempMailIo\TempMailPhp\Message;
6+
7+
use GuzzleHttp\Exception\GuzzleException;
8+
use GuzzleHttp\Exception\ServerException;
9+
use TempMailIo\TempMailPhp\Message\Data\Response\DeleteResponse;
10+
use TempMailIo\TempMailPhp\Message\Data\Response\DownloadAttachmentResponse;
11+
use TempMailIo\TempMailPhp\Message\Data\Response\GetMessageResponse;
12+
use TempMailIo\TempMailPhp\Message\Data\Response\GetMessageSourceCodeResponse;
13+
14+
interface ClientInterface
15+
{
16+
/**
17+
* @throws GuzzleException|\ReflectionException|ServerException
18+
*/
19+
public function getMessage(string $id): GetMessageResponse;
20+
21+
public function getMessageSourceCode(string $id): GetMessageSourceCodeResponse;
22+
23+
/**
24+
* @throws GuzzleException|\ReflectionException|ServerException
25+
*/
26+
public function delete(string $id): DeleteResponse;
27+
28+
/**
29+
* @throws GuzzleException|\ReflectionException|ServerException
30+
*/
31+
public function downloadAttachment(string $id): DownloadAttachmentResponse;
32+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TempMailIo\TempMailPhp\Message\Data\Response;
6+
7+
use TempMailIo\TempMailPhp\Data;
8+
use TempMailIo\TempMailPhp\GenericData\ErrorResponse;
9+
use TempMailIo\TempMailPhp\GenericData\SuccessResponse;
10+
11+
class DeleteResponse extends Data
12+
{
13+
public ?SuccessResponse $successResponse = null;
14+
15+
public ?ErrorResponse $errorResponse = null;
16+
}

0 commit comments

Comments
 (0)