Skip to content

Commit e28fdd4

Browse files
authored
Merge pull request #5 from GCDTech/feature/HandleErrors
Handle errors
2 parents c56f45a + 0bf3a4a commit e28fdd4

File tree

4 files changed

+58
-72
lines changed

4 files changed

+58
-72
lines changed

src/Services/CyclopsService.php

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Gcd\Cyclops\Exceptions\UserForbiddenException;
1111
use Gcd\Cyclops\Http\CurlHttpClient;
1212
use Gcd\Cyclops\Http\HttpRequest;
13+
use Gcd\Cyclops\Http\HttpResponse;
1314
use Gcd\Cyclops\Settings\CyclopsSettings;
1415

1516
class CyclopsService
@@ -31,6 +32,38 @@ public function __construct(int $brandId)
3132
$this->authorization = base64_encode($settings->authorizationUsername . ':' . $settings->authorizationPassword);
3233
}
3334

35+
private function logCyclopsErrors(HttpRequest $request, HttpResponse $response)
36+
{
37+
$loggableRequest = clone $request;
38+
if (isset($loggableRequest->getHeaders()['Authorization'])) {
39+
$loggableRequest->addHeader('Authorization', '[[REDACTED]]');
40+
}
41+
error_log('Cyclops exception response: ' . $response->getResponseCode() . ' ' . $response->getResponseBody() .
42+
"\n Request: " . var_export($loggableRequest, true));
43+
}
44+
45+
private function handleResponseCodes(HttpResponse $response, HttpRequest $request)
46+
{
47+
switch ($response->getResponseCode()) {
48+
case 200:
49+
break;
50+
case 403:
51+
$this->logCyclopsErrors($request, $response);
52+
throw new UserForbiddenException();
53+
break;
54+
case 404:
55+
$this->logCyclopsErrors($request, $response);
56+
throw new CustomerNotFoundException();
57+
break;
58+
case 409:
59+
$this->logCyclopsErrors($request, $response);
60+
throw new ConflictException();
61+
default:
62+
$this->logCyclopsErrors($request, $response);
63+
throw new CyclopsException();
64+
}
65+
}
66+
3467
public function loadCustomer(CyclopsIdentityEntity $identityEntity): CustomerEntity
3568
{
3669
if ($identityEntity->id != '') {
@@ -44,23 +77,13 @@ public function loadCustomer(CyclopsIdentityEntity $identityEntity): CustomerEnt
4477
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
4578

4679
$response = $this->doCyclopsRequest($request);
47-
$responseBody = json_decode($response->getResponseBody());
48-
if ($responseBody) {
49-
$identityEntity->id = $responseBody->data[0]->cyclopsId;
50-
}
5180
}
5281

53-
switch ($response->getResponseCode()) {
54-
case 200:
55-
break;
56-
case 403:
57-
throw new UserForbiddenException();
58-
break;
59-
case 404:
60-
throw new CustomerNotFoundException();
61-
break;
62-
default:
63-
throw new CyclopsException();
82+
$this->handleResponseCodes($response, $request);
83+
84+
$responseBody = json_decode($response->getResponseBody());
85+
if ($responseBody) {
86+
$identityEntity->id = $responseBody->data[0]->cyclopsId;
6487
}
6588

6689
$customer = new CustomerEntity();
@@ -80,19 +103,7 @@ public function deleteCustomer(CyclopsIdentityEntity $identityEntity)
80103
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
81104
$response = $this->doCyclopsRequest($request);
82105

83-
switch ($response->getResponseCode()) {
84-
case 200:
85-
break;
86-
case 403:
87-
throw new UserForbiddenException();
88-
break;
89-
case 404:
90-
throw new CustomerNotFoundException();
91-
break;
92-
default:
93-
throw new CyclopsException();
94-
}
95-
106+
$this->handleResponseCodes($response, $request);
96107
return $response;
97108
}
98109

@@ -104,18 +115,7 @@ public function getBrandOptInStatus(CustomerEntity $customerEntity): bool
104115
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
105116
$response = $this->doCyclopsRequest($request);
106117

107-
switch ($response->getResponseCode()) {
108-
case 200:
109-
break;
110-
case 403:
111-
throw new UserForbiddenException();
112-
break;
113-
case 404:
114-
throw new CustomerNotFoundException();
115-
break;
116-
default:
117-
throw new CyclopsException();
118-
}
118+
$this->handleResponseCodes($response, $request);
119119

120120
if ($responseBody = json_decode($response->getResponseBody())) {
121121
foreach ($responseBody->data as $data) {
@@ -138,21 +138,7 @@ public function setBrandOptInStatus(CustomerEntity $customerEntity)
138138
$request->addHeader('Content-Type', 'application/json');
139139
$response = $this->doCyclopsRequest($request);
140140

141-
switch ($response->getResponseCode()) {
142-
case 200:
143-
break;
144-
case 403:
145-
throw new UserForbiddenException();
146-
break;
147-
case 404:
148-
throw new CustomerNotFoundException();
149-
break;
150-
case 409:
151-
throw new ConflictException();
152-
default:
153-
throw new CyclopsException();
154-
}
155-
141+
$this->handleResponseCodes($response, $request);
156142
return $response;
157143
}
158144

@@ -163,15 +149,7 @@ public function getBrandOptInStatusChanges(\DateTime $startingDate, int $page =
163149
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
164150
$response = $this->doCyclopsRequest($request);
165151

166-
switch ($response->getResponseCode()) {
167-
case 200:
168-
break;
169-
case 403:
170-
throw new UserForbiddenException();
171-
break;
172-
default:
173-
throw new CyclopsException();
174-
}
152+
$this->handleResponseCodes($response, $request);
175153

176154
$changes = [];
177155
if ($responseBody = json_decode($response->getResponseBody())) {

src/UseCases/PushDeletedToCyclopsUseCase.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ public function __construct(CyclopsService $cyclopsService)
1919
$this->cyclopsService = $cyclopsService;
2020
}
2121

22-
public function execute(CyclopsCustomerListEntity $list, callable $onCustomerDeleted)
22+
public function execute(CyclopsCustomerListEntity $list, callable $onCustomerDeleted = null)
2323
{
2424
foreach ($list->items as $item) {
2525
try {
2626
$this->cyclopsService->deleteCustomer($item->identity);
27-
$onCustomerDeleted($item);
28-
} catch (CustomerNotFoundException $exception) {
29-
$onCustomerDeleted($item);
27+
if ($onCustomerDeleted !== null) {
28+
$onCustomerDeleted($item);
29+
}
30+
} catch (CustomerNotFoundException $exception) {
31+
if ($onCustomerDeleted !== null) {
32+
$onCustomerDeleted($item);
33+
}
3034
} catch (CyclopsException $exception) {
3135
}
3236
}

src/UseCases/PushStaleToCyclopsUseCase.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,23 @@ public function __construct(CyclopsService $cyclopsService)
1818
$this->cyclopsService = $cyclopsService;
1919
}
2020

21-
public function execute(CyclopsCustomerListEntity $list, callable $onCustomerCreated)
21+
public function execute(CyclopsCustomerListEntity $list, callable $onCustomerCreated = null)
2222
{
2323
foreach ($list->items as $item) {
2424
try {
2525
$this->cyclopsService->setBrandOptInStatus($item);
2626

27-
$onCustomerCreated($item);
27+
if ($onCustomerCreated !== null) {
28+
$onCustomerCreated($item);
29+
}
2830
} catch (CustomerNotFoundException $exception) {
2931
$customer = $this->cyclopsService->loadCustomer($item->identity);
3032
$customer->brandOptIn = $item->brandOptIn;
3133
$this->cyclopsService->setBrandOptInStatus($customer);
3234

33-
$onCustomerCreated($customer);
35+
if ($onCustomerCreated !== null) {
36+
$onCustomerCreated($customer);
37+
}
3438
}
3539
}
3640
}

tests/unit/UseCases/PushStaleToCyclopsUseCaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testStalePushedToCyclops()
3030

3131
verify($staleCount)->equals(0);
3232

33-
$createEntity = function($email) use ($service) {
33+
$createEntity = function ($email) use ($service) {
3434
$id = new CyclopsIdentityEntity();
3535
$id->email = $email;
3636
return $service->loadCustomer($id);

0 commit comments

Comments
 (0)