Skip to content

Commit 853a2ca

Browse files
committed
update errors fixed
1 parent 3e439d8 commit 853a2ca

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

phpstan.neon.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ parameters:
1010
tmpDir: build/phpstan
1111
checkOctaneCompatibility: true
1212
checkModelProperties: true
13+
14+

src/Brevo.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public function __construct(?ContactsApi $contactsApi = null)
2222
$this->contactsApi = $contactsApi ?? $this->createDefaultContactsApi();
2323
}
2424

25+
/**
26+
* @return \Brevo\Client\Api\ContactsApi
27+
*/
2528
private function createDefaultContactsApi(): ContactsApi
2629
{
2730
$config = Configuration::getDefaultConfiguration()
@@ -30,11 +33,21 @@ private function createDefaultContactsApi(): ContactsApi
3033
return new ContactsApi(new Client, $config);
3134
}
3235

36+
/**
37+
* @param \Brevo\Client\Api\ContactsApi $contactsApi
38+
*
39+
* @return void
40+
*/
3341
public function setContactsApi(ContactsApi $contactsApi): void
3442
{
3543
$this->contactsApi = $contactsApi;
3644
}
3745

46+
/**
47+
* @param string $email
48+
*
49+
* @return \Brevo\Client\Model\GetExtendedContactDetails|null
50+
*/
3851
public function getContactInfo(string $email): ?GetExtendedContactDetails
3952
{
4053
try {
@@ -46,7 +59,14 @@ public function getContactInfo(string $email): ?GetExtendedContactDetails
4659
}
4760
}
4861

49-
public function subscribe(string $email, string $listId, array $attributes = []): bool
62+
/**
63+
* @param string $email
64+
* @param integer $listId
65+
* @param array $attributes
66+
*
67+
* @return bool
68+
*/
69+
public function subscribe(string $email, int $listId, array $attributes = []): bool
5070
{
5171
try {
5272
// Check if contact exists
@@ -76,7 +96,7 @@ public function subscribe(string $email, string $listId, array $attributes = [])
7696
// Subscribe to list
7797
$addContactToList = new AddContactToList;
7898
$addContactToList->setEmails([$email]);
79-
$this->contactsApi->addContactToList($listId, $addContactToList);
99+
$this->contactsApi->addContactToList( $listId, $addContactToList);
80100

81101
return true;
82102
} catch (ApiException $e) {
@@ -86,7 +106,13 @@ public function subscribe(string $email, string $listId, array $attributes = [])
86106
}
87107
}
88108

89-
public function unsubscribe(string $email, string $listId): bool
109+
/**
110+
* @param string $email
111+
* @param integer $listId
112+
*
113+
* @return bool
114+
*/
115+
public function unsubscribe(string $email, int $listId): bool
90116
{
91117
try {
92118
$this->removeContactFromList($email, $listId);
@@ -103,14 +129,13 @@ public function unsubscribe(string $email, string $listId): bool
103129
/**
104130
* @throws \Brevo\Client\ApiException
105131
*/
106-
private function createOrUpdateContact(string $email, array $attributes): void
132+
public function createOrUpdateContact(string $email, array $attributes): void
107133
{
108134
try {
109135
$this->updateExistingContact($email, $attributes);
110136
} catch (ApiException $e) {
111137
if ($e->getCode() === 404) {
112138
$this->createNewContact($email, $attributes);
113-
114139
return;
115140
}
116141
throw $e;
@@ -134,7 +159,7 @@ private function updateExistingContact(string $email, array $attributes): void
134159
/**
135160
* @throws \Brevo\Client\ApiException
136161
*/
137-
private function createNewContact(string $email, array $attributes): void
162+
public function createNewContact(string $email, array $attributes): void
138163
{
139164
$createContact = new CreateContact;
140165
$createContact->setEmail($email);
@@ -149,23 +174,30 @@ private function createNewContact(string $email, array $attributes): void
149174
/**
150175
* @throws \Brevo\Client\ApiException
151176
*/
152-
private function addContactToList(string $email, string $listId): void
177+
public function addContactToList(string $email, int $listId): void
153178
{
154-
$listRequest = new AddContactToList;
179+
$listRequest = new AddContactToList();
155180
$listRequest->setEmails([$email]);
156181
$this->contactsApi->addContactToList($listId, $listRequest);
157182
}
158183

159184
/**
160185
* @throws \Brevo\Client\ApiException
161186
*/
162-
private function removeContactFromList(string $email, string $listId): void
187+
private function removeContactFromList(string $email, int $listId): void
163188
{
164189
$listRequest = new RemoveContactFromList;
165190
$listRequest->setEmails([$email]);
166191
$this->contactsApi->removeContactFromList($listId, $listRequest);
167192
}
168193

194+
/**
195+
* @param \Brevo\Client\ApiException $e
196+
* @param string $context
197+
* @param bool $isWarning
198+
*
199+
* @return void
200+
*/
169201
private function handleApiException(ApiException $e, string $context, bool $isWarning = false): void
170202
{
171203
$logMethod = $isWarning ? 'warning' : 'error';

tests/BrevoNewsletterTest.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
test('subscribes new contact with attributes', function () {
4949
$email = '[email protected]';
50-
$listId = 'list-123';
50+
$listId = 144;
5151
$attributes = ['name' => 'John Doe'];
5252

5353
// Mock the initial contact check (contact doesn't exist)
@@ -82,7 +82,7 @@
8282

8383
test('updates existing contact and subscribes', function () {
8484
$email = '[email protected]';
85-
$listId = 'list-456';
85+
$listId = 456;
8686
$attributes = ['name' => 'Updated Name'];
8787

8888
// Mock the initial contact check (contact exists)
@@ -120,14 +120,14 @@
120120

121121
Log::shouldReceive('error')->once();
122122

123-
$result = $this->brevo->subscribe('[email protected]', 'list-123');
123+
$result = $this->brevo->subscribe('[email protected]', 144);
124124

125125
expect($result)->toBeFalse();
126126
});
127127

128128
test('unsubscribes contact successfully', function () {
129129
$email = '[email protected]';
130-
$listId = 'list-789';
130+
$listId = 789;
131131

132132
$this->contactsApi->shouldReceive('removeContactFromList')
133133
->once()
@@ -146,7 +146,7 @@
146146

147147
Log::shouldReceive('warning')->once();
148148

149-
$result = $this->brevo->unsubscribe('[email protected]', 'list-123');
149+
$result = $this->brevo->unsubscribe('[email protected]', 144);
150150

151151
expect($result)->toBeTrue();
152152
});
@@ -157,11 +157,13 @@
157157

158158
Log::shouldReceive('error')->once();
159159

160-
$result = $this->brevo->unsubscribe('[email protected]', 'list-123');
160+
$result = $this->brevo->unsubscribe('[email protected]', 144);
161161

162162
expect($result)->toBeFalse();
163163
});
164164

165+
166+
165167
afterEach(function () {
166168
Mockery::close();
167169
});

0 commit comments

Comments
 (0)