Skip to content

Commit 0d3bae1

Browse files
committed
options added for error logging and api calls
1 parent 587eae1 commit 0d3bae1

File tree

2 files changed

+45
-39
lines changed

2 files changed

+45
-39
lines changed

src/Services/CyclopsService.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,19 @@ class CyclopsService
2323

2424
private $authorization;
2525

26+
private $enableLogging;
27+
28+
private $enableApi;
29+
2630
public function __construct(int $brandId)
2731
{
2832
$this->brandId = $brandId;
2933
$this->httpClient = new CurlHttpClient();
3034
$settings = CyclopsSettings::singleton();
3135
$this->cyclopsUrl = $settings->cyclopsUrl;
3236
$this->authorization = base64_encode($settings->authorizationUsername . ':' . $settings->authorizationPassword);
37+
$this->enableLogging = $settings->enableLogging;
38+
$this->enableApi = $settings->enableApi;
3339
}
3440

3541
private function logCyclopsErrors(HttpRequest $request, HttpResponse $response)
@@ -38,29 +44,14 @@ private function logCyclopsErrors(HttpRequest $request, HttpResponse $response)
3844
if (isset($loggableRequest->getHeaders()['Authorization'])) {
3945
$loggableRequest->addHeader('Authorization', '[[REDACTED]]');
4046
}
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();
47+
if ($this->enableLogging) {
48+
error_log('Cyclops exception response: '
49+
. $response->getResponseCode()
50+
. ' '
51+
. $response->getResponseBody()
52+
.
53+
"\n Request: "
54+
. var_export($loggableRequest, true));
6455
}
6556
}
6657

@@ -79,8 +70,6 @@ public function loadCustomer(CyclopsIdentityEntity $identityEntity): CustomerEnt
7970
$response = $this->doCyclopsRequest($request);
8071
}
8172

82-
$this->handleResponseCodes($response, $request);
83-
8473
$responseBody = json_decode($response->getResponseBody());
8574
if ($responseBody) {
8675
$identityEntity->id = $responseBody->data[0]->cyclopsId;
@@ -93,18 +82,40 @@ public function loadCustomer(CyclopsIdentityEntity $identityEntity): CustomerEnt
9382

9483
protected function doCyclopsRequest(HttpRequest $request)
9584
{
96-
return $this->httpClient->getResponse($request);
85+
if (!$this->enableApi) {
86+
throw new CyclopsException('Cyclops API disabled');
87+
}
88+
89+
$response = $this->httpClient->getResponse($request);
90+
91+
switch ($response->getResponseCode()) {
92+
case 200:
93+
break;
94+
case 403:
95+
$this->logCyclopsErrors($request, $response);
96+
throw new UserForbiddenException();
97+
break;
98+
case 404:
99+
$this->logCyclopsErrors($request, $response);
100+
throw new CustomerNotFoundException();
101+
break;
102+
case 409:
103+
$this->logCyclopsErrors($request, $response);
104+
throw new ConflictException();
105+
default:
106+
$this->logCyclopsErrors($request, $response);
107+
throw new CyclopsException();
108+
}
109+
110+
return $response;
97111
}
98112

99113
public function deleteCustomer(CyclopsIdentityEntity $identityEntity)
100114
{
101115
$url = $this->cyclopsUrl . "customer/{$identityEntity->id}";
102116
$request = new HttpRequest($url, 'delete');
103117
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
104-
$response = $this->doCyclopsRequest($request);
105-
106-
$this->handleResponseCodes($response, $request);
107-
return $response;
118+
return $this->doCyclopsRequest($request);
108119
}
109120

110121
public function getBrandOptInStatus(CustomerEntity $customerEntity): bool
@@ -115,8 +126,6 @@ public function getBrandOptInStatus(CustomerEntity $customerEntity): bool
115126
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
116127
$response = $this->doCyclopsRequest($request);
117128

118-
$this->handleResponseCodes($response, $request);
119-
120129
if ($responseBody = json_decode($response->getResponseBody())) {
121130
foreach ($responseBody->data as $data) {
122131
if ($data->brandId == $this->brandId) {
@@ -136,10 +145,7 @@ public function setBrandOptInStatus(CustomerEntity $customerEntity)
136145
$request = new HttpRequest($url, 'post', $brands);
137146
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
138147
$request->addHeader('Content-Type', 'application/json');
139-
$response = $this->doCyclopsRequest($request);
140-
141-
$this->handleResponseCodes($response, $request);
142-
return $response;
148+
return $this->doCyclopsRequest($request);
143149
}
144150

145151
public function getBrandOptInStatusChanges(\DateTime $startingDate, int $page = 1)
@@ -149,8 +155,6 @@ public function getBrandOptInStatusChanges(\DateTime $startingDate, int $page =
149155
$request->addHeader('Authorization', 'Basic ' . $this->authorization);
150156
$response = $this->doCyclopsRequest($request);
151157

152-
$this->handleResponseCodes($response, $request);
153-
154158
$changes = [];
155159
if ($responseBody = json_decode($response->getResponseBody())) {
156160
foreach ($responseBody->data as $data) {

src/Settings/CyclopsSettings.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ class CyclopsSettings
77
public $cyclopsUrl = '';
88
public $authorizationUsername = '';
99
public $authorizationPassword = '';
10+
public $enableLogging = false;
11+
public $enableApi = false;
1012

1113
static $singleton;
1214

@@ -17,4 +19,4 @@ public static function singleton()
1719
}
1820
return self::$singleton;
1921
}
20-
}
22+
}

0 commit comments

Comments
 (0)