Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 42 additions & 64 deletions src/Services/CyclopsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Gcd\Cyclops\Exceptions\UserForbiddenException;
use Gcd\Cyclops\Http\CurlHttpClient;
use Gcd\Cyclops\Http\HttpRequest;
use Gcd\Cyclops\Http\HttpResponse;
use Gcd\Cyclops\Settings\CyclopsSettings;

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

private function logCyclopsErrors(HttpRequest $request, HttpResponse $response)
{
$loggableRequest = clone $request;
if (isset($loggableRequest->getHeaders()['Authorization'])) {
$loggableRequest->addHeader('Authorization', '[[REDACTED]]');
}
error_log('Cyclops exception response: ' . $response->getResponseCode() . ' ' . $response->getResponseBody() .
"\n Request: " . var_export($loggableRequest, true));
}

private function handleResponseCodes(HttpResponse $response, HttpRequest $request)
{
switch ($response->getResponseCode()) {
case 200:
break;
case 403:
$this->logCyclopsErrors($request, $response);
throw new UserForbiddenException();
break;
case 404:
$this->logCyclopsErrors($request, $response);
throw new CustomerNotFoundException();
break;
case 409:
$this->logCyclopsErrors($request, $response);
throw new ConflictException();
default:
$this->logCyclopsErrors($request, $response);
throw new CyclopsException();
}
}

public function loadCustomer(CyclopsIdentityEntity $identityEntity): CustomerEntity
{
if ($identityEntity->id != '') {
Expand All @@ -44,23 +77,13 @@ public function loadCustomer(CyclopsIdentityEntity $identityEntity): CustomerEnt
$request->addHeader('Authorization', 'Basic ' . $this->authorization);

$response = $this->doCyclopsRequest($request);
$responseBody = json_decode($response->getResponseBody());
if ($responseBody) {
$identityEntity->id = $responseBody->data[0]->cyclopsId;
}
}

switch ($response->getResponseCode()) {
case 200:
break;
case 403:
throw new UserForbiddenException();
break;
case 404:
throw new CustomerNotFoundException();
break;
default:
throw new CyclopsException();
$this->handleResponseCodes($response, $request);

$responseBody = json_decode($response->getResponseBody());
if ($responseBody) {
$identityEntity->id = $responseBody->data[0]->cyclopsId;
}

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

switch ($response->getResponseCode()) {
case 200:
break;
case 403:
throw new UserForbiddenException();
break;
case 404:
throw new CustomerNotFoundException();
break;
default:
throw new CyclopsException();
}

$this->handleResponseCodes($response, $request);
return $response;
}

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

switch ($response->getResponseCode()) {
case 200:
break;
case 403:
throw new UserForbiddenException();
break;
case 404:
throw new CustomerNotFoundException();
break;
default:
throw new CyclopsException();
}
$this->handleResponseCodes($response, $request);

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

switch ($response->getResponseCode()) {
case 200:
break;
case 403:
throw new UserForbiddenException();
break;
case 404:
throw new CustomerNotFoundException();
break;
case 409:
throw new ConflictException();
default:
throw new CyclopsException();
}

$this->handleResponseCodes($response, $request);
return $response;
}

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

switch ($response->getResponseCode()) {
case 200:
break;
case 403:
throw new UserForbiddenException();
break;
default:
throw new CyclopsException();
}
$this->handleResponseCodes($response, $request);

$changes = [];
if ($responseBody = json_decode($response->getResponseBody())) {
Expand Down
12 changes: 8 additions & 4 deletions src/UseCases/PushDeletedToCyclopsUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ public function __construct(CyclopsService $cyclopsService)
$this->cyclopsService = $cyclopsService;
}

public function execute(CyclopsCustomerListEntity $list, callable $onCustomerDeleted)
public function execute(CyclopsCustomerListEntity $list, callable $onCustomerDeleted = null)
{
foreach ($list->items as $item) {
try {
$this->cyclopsService->deleteCustomer($item->identity);
$onCustomerDeleted($item);
} catch (CustomerNotFoundException $exception) {
$onCustomerDeleted($item);
if ($onCustomerDeleted !== null) {
$onCustomerDeleted($item);
}
} catch (CustomerNotFoundException $exception) {
if ($onCustomerDeleted !== null) {
$onCustomerDeleted($item);
}
} catch (CyclopsException $exception) {
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/UseCases/PushStaleToCyclopsUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,23 @@ public function __construct(CyclopsService $cyclopsService)
$this->cyclopsService = $cyclopsService;
}

public function execute(CyclopsCustomerListEntity $list, callable $onCustomerCreated)
public function execute(CyclopsCustomerListEntity $list, callable $onCustomerCreated = null)
{
foreach ($list->items as $item) {
try {
$this->cyclopsService->setBrandOptInStatus($item);

$onCustomerCreated($item);
if ($onCustomerCreated !== null) {
$onCustomerCreated($item);
}
} catch (CustomerNotFoundException $exception) {
$customer = $this->cyclopsService->loadCustomer($item->identity);
$customer->brandOptIn = $item->brandOptIn;
$this->cyclopsService->setBrandOptInStatus($customer);

$onCustomerCreated($customer);
if ($onCustomerCreated !== null) {
$onCustomerCreated($customer);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/UseCases/PushStaleToCyclopsUseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function testStalePushedToCyclops()

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

$createEntity = function($email) use ($service) {
$createEntity = function ($email) use ($service) {
$id = new CyclopsIdentityEntity();
$id->email = $email;
return $service->loadCustomer($id);
Expand Down