Skip to content

Commit 542eaf9

Browse files
committed
List bounces endpoint
1 parent 2e57ef2 commit 542eaf9

4 files changed

Lines changed: 149 additions & 0 deletions

File tree

src/Endpoint/BouncesClient.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpList\RestApiClient\Client;
88
use PhpList\RestApiClient\Exception\ApiException;
9+
use PhpList\RestApiClient\Response\Bounces\BounceCollection;
910

1011
/**
1112
* Client for bounce-related API endpoints.
@@ -32,6 +33,20 @@ public function listRegex(): array
3233
return $this->client->get('bounces/regex');
3334
}
3435

36+
/**
37+
* Get a list of all bounces.
38+
*
39+
* GET /api/v2/bounces
40+
*
41+
* @return BounceCollection
42+
* @throws ApiException If an API error occurs
43+
*/
44+
public function list(): BounceCollection
45+
{
46+
$response = $this->client->get('bounces');
47+
return new BounceCollection($response);
48+
}
49+
3550
/**
3651
* Create or update a bounce regex rule.
3752
*
@@ -73,4 +88,18 @@ public function deleteRegexByHash(string $regexHash): array
7388
{
7489
return $this->client->delete('bounces/regex/' . rawurlencode($regexHash));
7590
}
91+
92+
/**
93+
* Delete a bounce by its id.
94+
*
95+
* DELETE /api/v2/bounces/{bounceId}
96+
*
97+
* @param string $bounceId The bounce id
98+
* @return array Empty response on success
99+
* @throws ApiException If an API error occurs
100+
*/
101+
public function deleteById(string $bounceId): array
102+
{
103+
return $this->client->delete('bounces/' . rawurlencode($bounceId));
104+
}
76105
}

src/Entity/Bounce.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestApiClient\Entity;
6+
7+
use PhpList\RestApiClient\Response\AbstractResponse;
8+
9+
/**
10+
* Entity representing a bounce record.
11+
*/
12+
class Bounce extends AbstractResponse
13+
{
14+
public int $id;
15+
public ?string $status = null;
16+
public ?string $date = null;
17+
public int $messageId;
18+
public ?string $messageSubject = null;
19+
public ?int $subscriberId = null;
20+
public ?string $subscriberEmail = null;
21+
public ?string $comment = null;
22+
23+
public function __construct(array $data)
24+
{
25+
$this->id = isset($data['id']) ? (int)$data['id'] : 0;
26+
$this->status = isset($data['status']) ? (string)$data['status'] : null;
27+
$this->date = isset($data['date']) ? (string)$data['date'] : null;
28+
$this->messageId = isset($data['message_id']) ? (int)$data['message_id'] : 0;
29+
$this->messageSubject = isset($data['message_subject']) ? (string)$data['message_subject'] : null;
30+
$this->subscriberId = isset($data['subscriber_id']) ? (int)$data['subscriber_id'] : null;
31+
$this->subscriberEmail = isset($data['subscriber_email']) ? (string)$data['subscriber_email'] : null;
32+
$this->comment = isset($data['comment']) ? (string)$data['comment'] : null;
33+
}
34+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestApiClient\Response\Bounces;
6+
7+
use PhpList\RestApiClient\Entity\Bounce;
8+
use PhpList\RestApiClient\Response\AbstractCollectionResponse;
9+
10+
/**
11+
* Response class for a list of bounces.
12+
*/
13+
class BounceCollection extends AbstractCollectionResponse
14+
{
15+
/**
16+
* @var Bounce[] The list of bounces
17+
*/
18+
public array $items = [];
19+
20+
public function __construct(array $data)
21+
{
22+
if (!isset($data['items']) || !is_array($data['items'])) {
23+
$data = [
24+
'items' => $data,
25+
'pagination' => [
26+
'total' => count($data),
27+
'limit' => count($data),
28+
'has_more' => false,
29+
'next_cursor' => null,
30+
],
31+
];
32+
}
33+
34+
parent::__construct($data);
35+
}
36+
37+
protected function processItems(array $items): void
38+
{
39+
$this->items = [];
40+
foreach ($items as $item) {
41+
$this->items[] = new Bounce($item);
42+
}
43+
}
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestApiClient\Tests\Endpoint;
6+
7+
use PhpList\RestApiClient\Client;
8+
use PhpList\RestApiClient\Endpoint\BouncesClient;
9+
use PhpList\RestApiClient\Response\Bounces\BounceCollection;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class BouncesClientMethodTest extends TestCase
13+
{
14+
public function testListReturnsBounceCollection(): void
15+
{
16+
$mockClient = $this->createMock(Client::class);
17+
$mockClient->expects($this->once())
18+
->method('get')
19+
->with('bounces')
20+
->willReturn([
21+
[
22+
'id' => 10,
23+
'status' => 'not processed',
24+
'date' => '2023-01-01T12:00:00Z',
25+
'message_id' => 123,
26+
'message_subject' => 'Newsletter',
27+
'subscriber_id' => 456,
28+
'subscriber_email' => 'user@example.com',
29+
'comment' => 'Soft bounce',
30+
],
31+
]);
32+
33+
$bouncesClient = new BouncesClient($mockClient);
34+
$result = $bouncesClient->list();
35+
36+
$this->assertInstanceOf(BounceCollection::class, $result);
37+
$this->assertCount(1, $result->items);
38+
$this->assertSame(10, $result->items[0]->id);
39+
$this->assertSame(123, $result->items[0]->messageId);
40+
$this->assertSame('user@example.com', $result->items[0]->subscriberEmail);
41+
}
42+
}

0 commit comments

Comments
 (0)