1010use Gcd \Cyclops \Exceptions \UserForbiddenException ;
1111use Gcd \Cyclops \Http \CurlHttpClient ;
1212use Gcd \Cyclops \Http \HttpRequest ;
13+ use Gcd \Cyclops \Http \HttpResponse ;
1314use Gcd \Cyclops \Settings \CyclopsSettings ;
1415
1516class 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 ())) {
0 commit comments