Passed
Pull Request — master (#12)
by kenny
02:28
created
src/Publisher.php 2 patches
Indentation   +469 added lines, -469 removed lines patch added patch discarded remove patch
@@ -16,474 +16,474 @@
 block discarded – undo
16 16
  * main class to be used that interfacing to the API
17 17
  */
18 18
 class Publisher implements LoggerAwareInterface {
19
-	const DEFAULT_MAX_ATTEMPT = 4;
20
-
21
-	const REST_SERVER = 'https://dev.one.co.id';
22
-	const AUTHENTICATION = '/oauth/token';
23
-	const ARTICLE_CHECK_ENDPOINT = '/api/article';
24
-	const ARTICLE_ENDPOINT = '/api/publisher/article';
25
-
26
-	/**
27
-	 * attachment url destination
28
-	 *
29
-	 * @var array
30
-	 */
31
-	private $attachmentUrl;
32
-
33
-	/**
34
-	 * Logger variable, if set log activity to this obejct each time sending request and receiving response
35
-	 *
36
-	 * @var \Psr\Log\LoggerInterface
37
-	 */
38
-	private $logger = null;
39
-
40
-	/**
41
-	 * credentials props
42
-	 *
43
-	 * @var string $clientId
44
-	 * @var string $clientSecret
45
-	 */
46
-	private $clientId;
47
-	private $clientSecret;
48
-
49
-	/**
50
-	 * Oauth access token response
51
-	 *
52
-	 * @var string $accessToken
53
-	 */
54
-	private $accessToken = null;
55
-
56
-	/**
57
-	 * publisher custom options
58
-	 *
59
-	 * @var \One\Collection $options
60
-	 */
61
-	private $options;
62
-
63
-	/**
64
-	 * http transaction Client
65
-	 *
66
-	 * @var \Guzzle\Http\Client
67
-	 */
68
-	private $httpClient;
69
-
70
-	/**
71
-	 * constructor
72
-	 *
73
-	 * @param string $clientId
74
-	 * @param string $clientSecret
75
-	 * @param array $options
76
-	 */
77
-	public function __construct($clientId, $clientSecret, $options = array()) {
78
-		$this->clientId = $clientId;
79
-		$this->clientSecret = $clientSecret;
80
-
81
-		$this->assessOptions($options);
82
-
83
-		$this->attachmentUrl = array(
84
-			Article::ATTACHMENT_FIELD_GALLERY => self::ARTICLE_ENDPOINT . '/{article_id}/gallery',
85
-			Article::ATTACHMENT_FIELD_PAGE => self::ARTICLE_ENDPOINT . '/{article_id}/page',
86
-			Article::ATTACHMENT_FIELD_PHOTO => self::ARTICLE_ENDPOINT . '/{article_id}/photo',
87
-			Article::ATTACHMENT_FIELD_VIDEO => self::ARTICLE_ENDPOINT . '/{article_id}/video',
88
-		);
89
-	}
90
-
91
-	/**
92
-	 * recycleToken from callback. If use external token storage could leveraged on this
93
-	 *
94
-	 * @param \Closure $tokenProducer
95
-	 * @return self
96
-	 */
97
-	public function recycleToken(\Closure $tokenProducer) {
98
-		return $this->setAuthorizationHeader($tokenProducer());
99
-	}
100
-
101
-	/**
102
-	 * assessing and custom option
103
-	 *
104
-	 * @param array $options
105
-	 * @return void
106
-	 */
107
-	private function assessOptions($options) {
108
-		$defaultOptions = array(
109
-			'rest_server' => self::REST_SERVER,
110
-			'auth_url' => self::AUTHENTICATION,
111
-			'max_attempt' => self::DEFAULT_MAX_ATTEMPT,
112
-			'default_headers' => array(
113
-				"Accept" => "application/json",
114
-			),
115
-		);
116
-
117
-		$this->options = new Collection(
118
-			array_merge(
119
-				$defaultOptions,
120
-				$options
121
-			)
122
-		);
123
-
124
-		if (isset($options['access_token'])) {
125
-			$this->setAuthorizationHeader($options['access_token']);
126
-		}
127
-
128
-		$this->httpClient = new Client(
129
-			$this->options->get('rest_server')
130
-		);
131
-	}
132
-
133
-	/**
134
-	 * one gate menu for request creation.
135
-	 *
136
-	 * @param string $method
137
-	 * @param string $path
138
-	 * @param \One\Collection|array $header
139
-	 * @param \One\Collection|array $body
140
-	 * @param array $options
141
-	 * @return string
142
-	 */
143
-	private function requestGate($method, $path, $header = array(), $body = array(), $options = array()) {
144
-		if (empty($this->accessToken)) {
145
-			$this->renewAuthToken();
146
-		}
147
-
148
-		return (string) $this->sendRequest(
149
-			$this->httpClient->createRequest(
150
-				$method,
151
-				$path,
152
-				array_merge(
153
-					$this->options->get('default_headers'),
154
-					$header
155
-				),
156
-				$body,
157
-				$options
158
-			)
159
-		);
160
-	}
161
-
162
-	/**
163
-	 * actually send request created here, separated for easier attempt count and handling exception
164
-	 *
165
-	 * @param \Guzzle\Http\Message\RequestInterface $request
166
-	 * @param integer $attempt
167
-	 * @return \Guzzle\Http\EntityBodyInterface|string|null
168
-	 * @throws \Exception
169
-	 * @throws \Guzzle\Http\Exception\ClientErrorResponseException
170
-	 * @throws \Guzzle\Http\Exception\BadResponseException
171
-	 */
172
-	private function sendRequest(RequestInterface $request, $attempt = 0) {
173
-		if ($attempt >= $this->options->get('max_attempt')) {
174
-			throw new \Exception("MAX attempt reached for " . $request->getUrl() . " with payload " . (string) $request);
175
-		}
176
-
177
-		try {
178
-			$response = $request->send();
179
-			if ($response->getStatusCode() == 200) {
180
-				return $response->getBody();
181
-			}
182
-			if ($response->getStatusCode() == 429) {
183
-				$this->renewAuthToken();
184
-			}
185
-
186
-			return $this->sendRequest($request, $attempt++);
187
-		} catch (ClientErrorResponseException $err) {
188
-			if ($err->getResponse()->getStatusCode() == 429) {
189
-				$this->renewAuthToken();
190
-				return $this->sendRequest($err->getRequest(), $attempt++);
191
-			}
192
-
193
-			throw $err;
194
-		} catch (\Exception $err) {
195
-			throw $err;
196
-		}
197
-	}
198
-
199
-	/**
200
-	 * renewing access_token
201
-	 *
202
-	 * @return self
203
-	 * @throws \Exception
204
-	 */
205
-	private function renewAuthToken() {
206
-		$token = (string) $this->sendRequest(
207
-			$this->httpClient->post(
208
-				self::AUTHENTICATION,
209
-				$this->options->get('default_headers'),
210
-				array(
211
-					"grant_type" => "client_credentials",
212
-					"client_id" => $this->clientId,
213
-					"client_secret" => $this->clientSecret,
214
-				)
215
-			)
216
-		);
217
-
218
-		$token = json_decode($token, true);
219
-
220
-		if (empty($token)) {
221
-			throw new \Exception("Access token request return empty response");
222
-		}
223
-
224
-		return $this->setAuthorizationHeader(
225
-			$token['access_token']
226
-		);
227
-	}
228
-
229
-	/**
230
-	 * set header for OAuth 2.0
231
-	 *
232
-	 * @param string $accessToken
233
-	 * @return self
234
-	 */
235
-	private function setAuthorizationHeader($accessToken) {
236
-		$this->accessToken = $accessToken;
237
-
238
-		$this->options->set(
239
-			'default_headers',
240
-			array_merge(
241
-				$this->options->get('default_headers'),
242
-				array(
243
-					"Authorization" => "Bearer " . $accessToken,
244
-				)
245
-			)
246
-		);
247
-
248
-		return $this;
249
-	}
250
-
251
-	/**
252
-	 * get Attachment Submission url Endpoint at rest API
253
-	 *
254
-	 * @param string $idArticle
255
-	 * @param string $field
256
-	 * @return string
257
-	 */
258
-	private function getAttachmentEndPoint($idArticle, $field) {
259
-		return $this->replaceEndPointId(
260
-			$idArticle,
261
-			$this->attachmentUrl[$field]
262
-		);
263
-	}
264
-
265
-	/**
266
-	 * get article endpoint for deleting api
267
-	 *
268
-	 * @param string $identifier
269
-	 * @return string
270
-	 */
271
-	private function getArticleWithIdEndPoint($identifier) {
272
-		return self::ARTICLE_ENDPOINT . "/$identifier";
273
-	}
274
-
275
-	/**
276
-	 * function that actually replace article_id inside endpoint pattern
277
-	 *
278
-	 * @param string $identifier
279
-	 * @param string $url
280
-	 * @return string
281
-	 */
282
-	private function replaceEndPointId($identifier, $url) {
283
-		return str_replace(
284
-			'{article_id}',
285
-			$identifier,
286
-			$url
287
-		);
288
-	}
289
-
290
-	/**
291
-	 * normalizing payload. not yet implemented totally, currently just bypass a toArray() function from collection.
292
-	 *
293
-	 * @param \One\Collection $collection
294
-	 * @return array
295
-	 */
296
-	private function normalizePayload(Collection $collection) {
297
-		return $collection->toArray();
298
-	}
299
-
300
-	/**
301
-	 * submitting article here, return new Object cloned from original
302
-	 *
303
-	 * @param \One\Model\Article $article
304
-	 * @return \One\Model\Article
305
-	 */
306
-	public function submitArticle(Article $article) {
307
-		$responseArticle = $this->post(
308
-			self::ARTICLE_ENDPOINT,
309
-			$this->normalizePayload(
310
-				$article->getCollection()
311
-			)
312
-		);
313
-
314
-		$responseArticle = json_decode($responseArticle, true);
315
-		$article->setId($responseArticle['data']['id']);
316
-
317
-		foreach (Article::getPossibleAttachment() as $field) {
318
-			if ($article->hasAttachment($field)) {
319
-				foreach ($article->getAttachmentByField($field) as $attachment) {
320
-					$this->submitAttachment(
321
-						$article->getId(),
322
-						$attachment,
323
-						$field
324
-					);
325
-				}
326
-			}
327
-		}
328
-
329
-		return $article;
330
-	}
331
-
332
-	/**
333
-	 * submit each attachment of an article here
334
-	 *
335
-	 * @param string $idArticle
336
-	 * @param \One\Model\Model $attachment
337
-	 * @param string $field
338
-	 * @return array
339
-	 */
340
-	public function submitAttachment($idArticle, Model $attachment, $field) {
341
-		return json_decode(
342
-			$this->post(
343
-				$this->getAttachmentEndPoint($idArticle, $field),
344
-				$this->normalizePayload(
345
-					$attachment->getCollection()
346
-				)
347
-			),
348
-			true
349
-		);
350
-	}
351
-
352
-	/**
353
-	 * get article from rest API
354
-	 *
355
-	 * @param string $idArticle
356
-	 * @return string json
357
-	 */
358
-	public function getArticle($idArticle) {
359
-		return $this->get(
360
-			self::ARTICLE_CHECK_ENDPOINT . "/$idArticle"
361
-		);
362
-	}
363
-
364
-	/**
365
-	 * get list article by publisher
366
-	 *
367
-	 * @return string json
368
-	 */
369
-	public function listArticle() {
370
-		return $this->get(
371
-			self::ARTICLE_ENDPOINT
372
-		);
373
-	}
374
-
375
-	/**
376
-	 * delete article based on id
377
-	 *
378
-	 * @param string $idArticle
379
-	 * @return string
380
-	 */
381
-	public function deleteArticle($idArticle) {
382
-		$articleOnRest = $this->getArticle($idArticle);
383
-
384
-		if (!empty($articleOnRest)) {
385
-			$articleOnRest = json_decode($articleOnRest, true);
386
-
387
-			if (isset($articleOnRest['data'])) {
388
-				foreach (Article::getDeleteableAttachment() as $field) {
389
-					if (isset($articleOnRest['data'][$field])) {
390
-						foreach ($articleOnRest['data'][$field] as $attachment) {
391
-							if (isset($attachment[$field . '_order'])) {
392
-								$this->deleteAttachment($idArticle, $field, $attachment[$field . '_order']);
393
-							}
394
-						}
395
-					}
396
-				}
397
-			}
398
-
399
-			return $this->delete(
400
-				$this->getArticleWithIdEndPoint($idArticle)
401
-			);
402
-		}
403
-	}
404
-
405
-	/**
406
-	 * delete attachment of an article
407
-	 *
408
-	 * @param string $idArticle
409
-	 * @param string $field
410
-	 * @param string $order
411
-	 * @return string
412
-	 */
413
-	public function deleteAttachment($idArticle, $field, $order) {
414
-		return $this->delete(
415
-			$this->getAttachmentEndPoint($idArticle, $field) . "/$order"
416
-		);
417
-	}
418
-
419
-	/**
420
-	 * get proxy
421
-	 *
422
-	 * @param string $path
423
-	 * @param \One\Collection|array $header
424
-	 * @param array $options
425
-	 * @return string
426
-	 */
427
-	final public function get($path, $header = array(), $options = array()) {
428
-		if (isset($this->logger) && !is_null($this->logger)) {
429
-			$this->logger->info("Hi, from post, path is " . $path);
430
-		}
431
-		return $this->requestGate(
432
-			'GET',
433
-			$path,
434
-			$header,
435
-			array(),
436
-			$options
437
-		);
438
-	}
439
-
440
-	/**
441
-	 * post proxy
442
-	 *
443
-	 * @param string $path
444
-	 * @param \One\Collection|array $body
445
-	 * @param \One\Collection|array $header
446
-	 * @param array $options
447
-	 * @return string
448
-	 */
449
-	final public function post($path, $body, $header = array(), $options = array()) {
450
-
451
-		if (isset($this->logger) && !is_null($this->logger)) {
452
-			$this->logger->info("Hi, from post " . $path);
453
-		}
454
-		return $this->requestGate(
455
-			'POST',
456
-			$path,
457
-			$header,
458
-			$body,
459
-			$options
460
-		);
461
-	}
462
-
463
-	/**
464
-	 * delete proxy
465
-	 *
466
-	 * @param string $path
467
-	 * @param \One\Collection|array $body
468
-	 * @param \One\Collection|array $header
469
-	 * @param array $options
470
-	 * @return string
471
-	 */
472
-	final public function delete($path, $body = array(), $header = array(), $options = array()) {
473
-		return $this->requestGate(
474
-			'DELETE',
475
-			$path,
476
-			$header,
477
-			$body,
478
-			$options
479
-		);
480
-	}
481
-
482
-	/**
483
-	 * @inheritDoc
484
-	 */
485
-	public function setLogger(LoggerInterface $logger) {
486
-		$this->logger = $logger;
487
-	}
19
+    const DEFAULT_MAX_ATTEMPT = 4;
20
+
21
+    const REST_SERVER = 'https://dev.one.co.id';
22
+    const AUTHENTICATION = '/oauth/token';
23
+    const ARTICLE_CHECK_ENDPOINT = '/api/article';
24
+    const ARTICLE_ENDPOINT = '/api/publisher/article';
25
+
26
+    /**
27
+     * attachment url destination
28
+     *
29
+     * @var array
30
+     */
31
+    private $attachmentUrl;
32
+
33
+    /**
34
+     * Logger variable, if set log activity to this obejct each time sending request and receiving response
35
+     *
36
+     * @var \Psr\Log\LoggerInterface
37
+     */
38
+    private $logger = null;
39
+
40
+    /**
41
+     * credentials props
42
+     *
43
+     * @var string $clientId
44
+     * @var string $clientSecret
45
+     */
46
+    private $clientId;
47
+    private $clientSecret;
48
+
49
+    /**
50
+     * Oauth access token response
51
+     *
52
+     * @var string $accessToken
53
+     */
54
+    private $accessToken = null;
55
+
56
+    /**
57
+     * publisher custom options
58
+     *
59
+     * @var \One\Collection $options
60
+     */
61
+    private $options;
62
+
63
+    /**
64
+     * http transaction Client
65
+     *
66
+     * @var \Guzzle\Http\Client
67
+     */
68
+    private $httpClient;
69
+
70
+    /**
71
+     * constructor
72
+     *
73
+     * @param string $clientId
74
+     * @param string $clientSecret
75
+     * @param array $options
76
+     */
77
+    public function __construct($clientId, $clientSecret, $options = array()) {
78
+        $this->clientId = $clientId;
79
+        $this->clientSecret = $clientSecret;
80
+
81
+        $this->assessOptions($options);
82
+
83
+        $this->attachmentUrl = array(
84
+            Article::ATTACHMENT_FIELD_GALLERY => self::ARTICLE_ENDPOINT . '/{article_id}/gallery',
85
+            Article::ATTACHMENT_FIELD_PAGE => self::ARTICLE_ENDPOINT . '/{article_id}/page',
86
+            Article::ATTACHMENT_FIELD_PHOTO => self::ARTICLE_ENDPOINT . '/{article_id}/photo',
87
+            Article::ATTACHMENT_FIELD_VIDEO => self::ARTICLE_ENDPOINT . '/{article_id}/video',
88
+        );
89
+    }
90
+
91
+    /**
92
+     * recycleToken from callback. If use external token storage could leveraged on this
93
+     *
94
+     * @param \Closure $tokenProducer
95
+     * @return self
96
+     */
97
+    public function recycleToken(\Closure $tokenProducer) {
98
+        return $this->setAuthorizationHeader($tokenProducer());
99
+    }
100
+
101
+    /**
102
+     * assessing and custom option
103
+     *
104
+     * @param array $options
105
+     * @return void
106
+     */
107
+    private function assessOptions($options) {
108
+        $defaultOptions = array(
109
+            'rest_server' => self::REST_SERVER,
110
+            'auth_url' => self::AUTHENTICATION,
111
+            'max_attempt' => self::DEFAULT_MAX_ATTEMPT,
112
+            'default_headers' => array(
113
+                "Accept" => "application/json",
114
+            ),
115
+        );
116
+
117
+        $this->options = new Collection(
118
+            array_merge(
119
+                $defaultOptions,
120
+                $options
121
+            )
122
+        );
123
+
124
+        if (isset($options['access_token'])) {
125
+            $this->setAuthorizationHeader($options['access_token']);
126
+        }
127
+
128
+        $this->httpClient = new Client(
129
+            $this->options->get('rest_server')
130
+        );
131
+    }
132
+
133
+    /**
134
+     * one gate menu for request creation.
135
+     *
136
+     * @param string $method
137
+     * @param string $path
138
+     * @param \One\Collection|array $header
139
+     * @param \One\Collection|array $body
140
+     * @param array $options
141
+     * @return string
142
+     */
143
+    private function requestGate($method, $path, $header = array(), $body = array(), $options = array()) {
144
+        if (empty($this->accessToken)) {
145
+            $this->renewAuthToken();
146
+        }
147
+
148
+        return (string) $this->sendRequest(
149
+            $this->httpClient->createRequest(
150
+                $method,
151
+                $path,
152
+                array_merge(
153
+                    $this->options->get('default_headers'),
154
+                    $header
155
+                ),
156
+                $body,
157
+                $options
158
+            )
159
+        );
160
+    }
161
+
162
+    /**
163
+     * actually send request created here, separated for easier attempt count and handling exception
164
+     *
165
+     * @param \Guzzle\Http\Message\RequestInterface $request
166
+     * @param integer $attempt
167
+     * @return \Guzzle\Http\EntityBodyInterface|string|null
168
+     * @throws \Exception
169
+     * @throws \Guzzle\Http\Exception\ClientErrorResponseException
170
+     * @throws \Guzzle\Http\Exception\BadResponseException
171
+     */
172
+    private function sendRequest(RequestInterface $request, $attempt = 0) {
173
+        if ($attempt >= $this->options->get('max_attempt')) {
174
+            throw new \Exception("MAX attempt reached for " . $request->getUrl() . " with payload " . (string) $request);
175
+        }
176
+
177
+        try {
178
+            $response = $request->send();
179
+            if ($response->getStatusCode() == 200) {
180
+                return $response->getBody();
181
+            }
182
+            if ($response->getStatusCode() == 429) {
183
+                $this->renewAuthToken();
184
+            }
185
+
186
+            return $this->sendRequest($request, $attempt++);
187
+        } catch (ClientErrorResponseException $err) {
188
+            if ($err->getResponse()->getStatusCode() == 429) {
189
+                $this->renewAuthToken();
190
+                return $this->sendRequest($err->getRequest(), $attempt++);
191
+            }
192
+
193
+            throw $err;
194
+        } catch (\Exception $err) {
195
+            throw $err;
196
+        }
197
+    }
198
+
199
+    /**
200
+     * renewing access_token
201
+     *
202
+     * @return self
203
+     * @throws \Exception
204
+     */
205
+    private function renewAuthToken() {
206
+        $token = (string) $this->sendRequest(
207
+            $this->httpClient->post(
208
+                self::AUTHENTICATION,
209
+                $this->options->get('default_headers'),
210
+                array(
211
+                    "grant_type" => "client_credentials",
212
+                    "client_id" => $this->clientId,
213
+                    "client_secret" => $this->clientSecret,
214
+                )
215
+            )
216
+        );
217
+
218
+        $token = json_decode($token, true);
219
+
220
+        if (empty($token)) {
221
+            throw new \Exception("Access token request return empty response");
222
+        }
223
+
224
+        return $this->setAuthorizationHeader(
225
+            $token['access_token']
226
+        );
227
+    }
228
+
229
+    /**
230
+     * set header for OAuth 2.0
231
+     *
232
+     * @param string $accessToken
233
+     * @return self
234
+     */
235
+    private function setAuthorizationHeader($accessToken) {
236
+        $this->accessToken = $accessToken;
237
+
238
+        $this->options->set(
239
+            'default_headers',
240
+            array_merge(
241
+                $this->options->get('default_headers'),
242
+                array(
243
+                    "Authorization" => "Bearer " . $accessToken,
244
+                )
245
+            )
246
+        );
247
+
248
+        return $this;
249
+    }
250
+
251
+    /**
252
+     * get Attachment Submission url Endpoint at rest API
253
+     *
254
+     * @param string $idArticle
255
+     * @param string $field
256
+     * @return string
257
+     */
258
+    private function getAttachmentEndPoint($idArticle, $field) {
259
+        return $this->replaceEndPointId(
260
+            $idArticle,
261
+            $this->attachmentUrl[$field]
262
+        );
263
+    }
264
+
265
+    /**
266
+     * get article endpoint for deleting api
267
+     *
268
+     * @param string $identifier
269
+     * @return string
270
+     */
271
+    private function getArticleWithIdEndPoint($identifier) {
272
+        return self::ARTICLE_ENDPOINT . "/$identifier";
273
+    }
274
+
275
+    /**
276
+     * function that actually replace article_id inside endpoint pattern
277
+     *
278
+     * @param string $identifier
279
+     * @param string $url
280
+     * @return string
281
+     */
282
+    private function replaceEndPointId($identifier, $url) {
283
+        return str_replace(
284
+            '{article_id}',
285
+            $identifier,
286
+            $url
287
+        );
288
+    }
289
+
290
+    /**
291
+     * normalizing payload. not yet implemented totally, currently just bypass a toArray() function from collection.
292
+     *
293
+     * @param \One\Collection $collection
294
+     * @return array
295
+     */
296
+    private function normalizePayload(Collection $collection) {
297
+        return $collection->toArray();
298
+    }
299
+
300
+    /**
301
+     * submitting article here, return new Object cloned from original
302
+     *
303
+     * @param \One\Model\Article $article
304
+     * @return \One\Model\Article
305
+     */
306
+    public function submitArticle(Article $article) {
307
+        $responseArticle = $this->post(
308
+            self::ARTICLE_ENDPOINT,
309
+            $this->normalizePayload(
310
+                $article->getCollection()
311
+            )
312
+        );
313
+
314
+        $responseArticle = json_decode($responseArticle, true);
315
+        $article->setId($responseArticle['data']['id']);
316
+
317
+        foreach (Article::getPossibleAttachment() as $field) {
318
+            if ($article->hasAttachment($field)) {
319
+                foreach ($article->getAttachmentByField($field) as $attachment) {
320
+                    $this->submitAttachment(
321
+                        $article->getId(),
322
+                        $attachment,
323
+                        $field
324
+                    );
325
+                }
326
+            }
327
+        }
328
+
329
+        return $article;
330
+    }
331
+
332
+    /**
333
+     * submit each attachment of an article here
334
+     *
335
+     * @param string $idArticle
336
+     * @param \One\Model\Model $attachment
337
+     * @param string $field
338
+     * @return array
339
+     */
340
+    public function submitAttachment($idArticle, Model $attachment, $field) {
341
+        return json_decode(
342
+            $this->post(
343
+                $this->getAttachmentEndPoint($idArticle, $field),
344
+                $this->normalizePayload(
345
+                    $attachment->getCollection()
346
+                )
347
+            ),
348
+            true
349
+        );
350
+    }
351
+
352
+    /**
353
+     * get article from rest API
354
+     *
355
+     * @param string $idArticle
356
+     * @return string json
357
+     */
358
+    public function getArticle($idArticle) {
359
+        return $this->get(
360
+            self::ARTICLE_CHECK_ENDPOINT . "/$idArticle"
361
+        );
362
+    }
363
+
364
+    /**
365
+     * get list article by publisher
366
+     *
367
+     * @return string json
368
+     */
369
+    public function listArticle() {
370
+        return $this->get(
371
+            self::ARTICLE_ENDPOINT
372
+        );
373
+    }
374
+
375
+    /**
376
+     * delete article based on id
377
+     *
378
+     * @param string $idArticle
379
+     * @return string
380
+     */
381
+    public function deleteArticle($idArticle) {
382
+        $articleOnRest = $this->getArticle($idArticle);
383
+
384
+        if (!empty($articleOnRest)) {
385
+            $articleOnRest = json_decode($articleOnRest, true);
386
+
387
+            if (isset($articleOnRest['data'])) {
388
+                foreach (Article::getDeleteableAttachment() as $field) {
389
+                    if (isset($articleOnRest['data'][$field])) {
390
+                        foreach ($articleOnRest['data'][$field] as $attachment) {
391
+                            if (isset($attachment[$field . '_order'])) {
392
+                                $this->deleteAttachment($idArticle, $field, $attachment[$field . '_order']);
393
+                            }
394
+                        }
395
+                    }
396
+                }
397
+            }
398
+
399
+            return $this->delete(
400
+                $this->getArticleWithIdEndPoint($idArticle)
401
+            );
402
+        }
403
+    }
404
+
405
+    /**
406
+     * delete attachment of an article
407
+     *
408
+     * @param string $idArticle
409
+     * @param string $field
410
+     * @param string $order
411
+     * @return string
412
+     */
413
+    public function deleteAttachment($idArticle, $field, $order) {
414
+        return $this->delete(
415
+            $this->getAttachmentEndPoint($idArticle, $field) . "/$order"
416
+        );
417
+    }
418
+
419
+    /**
420
+     * get proxy
421
+     *
422
+     * @param string $path
423
+     * @param \One\Collection|array $header
424
+     * @param array $options
425
+     * @return string
426
+     */
427
+    final public function get($path, $header = array(), $options = array()) {
428
+        if (isset($this->logger) && !is_null($this->logger)) {
429
+            $this->logger->info("Hi, from post, path is " . $path);
430
+        }
431
+        return $this->requestGate(
432
+            'GET',
433
+            $path,
434
+            $header,
435
+            array(),
436
+            $options
437
+        );
438
+    }
439
+
440
+    /**
441
+     * post proxy
442
+     *
443
+     * @param string $path
444
+     * @param \One\Collection|array $body
445
+     * @param \One\Collection|array $header
446
+     * @param array $options
447
+     * @return string
448
+     */
449
+    final public function post($path, $body, $header = array(), $options = array()) {
450
+
451
+        if (isset($this->logger) && !is_null($this->logger)) {
452
+            $this->logger->info("Hi, from post " . $path);
453
+        }
454
+        return $this->requestGate(
455
+            'POST',
456
+            $path,
457
+            $header,
458
+            $body,
459
+            $options
460
+        );
461
+    }
462
+
463
+    /**
464
+     * delete proxy
465
+     *
466
+     * @param string $path
467
+     * @param \One\Collection|array $body
468
+     * @param \One\Collection|array $header
469
+     * @param array $options
470
+     * @return string
471
+     */
472
+    final public function delete($path, $body = array(), $header = array(), $options = array()) {
473
+        return $this->requestGate(
474
+            'DELETE',
475
+            $path,
476
+            $header,
477
+            $body,
478
+            $options
479
+        );
480
+    }
481
+
482
+    /**
483
+     * @inheritDoc
484
+     */
485
+    public function setLogger(LoggerInterface $logger) {
486
+        $this->logger = $logger;
487
+    }
488 488
 
489 489
 }
Please login to merge, or discard this patch.
Braces   +44 added lines, -22 removed lines patch added patch discarded remove patch
@@ -15,7 +15,8 @@  discard block
 block discarded – undo
15 15
  * Publisher class
16 16
  * main class to be used that interfacing to the API
17 17
  */
18
-class Publisher implements LoggerAwareInterface {
18
+class Publisher implements LoggerAwareInterface
19
+{
19 20
 	const DEFAULT_MAX_ATTEMPT = 4;
20 21
 
21 22
 	const REST_SERVER = 'https://dev.one.co.id';
@@ -74,7 +75,8 @@  discard block
 block discarded – undo
74 75
 	 * @param string $clientSecret
75 76
 	 * @param array $options
76 77
 	 */
77
-	public function __construct($clientId, $clientSecret, $options = array()) {
78
+	public function __construct($clientId, $clientSecret, $options = array())
79
+	{
78 80
 		$this->clientId = $clientId;
79 81
 		$this->clientSecret = $clientSecret;
80 82
 
@@ -94,7 +96,8 @@  discard block
 block discarded – undo
94 96
 	 * @param \Closure $tokenProducer
95 97
 	 * @return self
96 98
 	 */
97
-	public function recycleToken(\Closure $tokenProducer) {
99
+	public function recycleToken(\Closure $tokenProducer)
100
+	{
98 101
 		return $this->setAuthorizationHeader($tokenProducer());
99 102
 	}
100 103
 
@@ -104,7 +107,8 @@  discard block
 block discarded – undo
104 107
 	 * @param array $options
105 108
 	 * @return void
106 109
 	 */
107
-	private function assessOptions($options) {
110
+	private function assessOptions($options)
111
+	{
108 112
 		$defaultOptions = array(
109 113
 			'rest_server' => self::REST_SERVER,
110 114
 			'auth_url' => self::AUTHENTICATION,
@@ -140,7 +144,8 @@  discard block
 block discarded – undo
140 144
 	 * @param array $options
141 145
 	 * @return string
142 146
 	 */
143
-	private function requestGate($method, $path, $header = array(), $body = array(), $options = array()) {
147
+	private function requestGate($method, $path, $header = array(), $body = array(), $options = array())
148
+	{
144 149
 		if (empty($this->accessToken)) {
145 150
 			$this->renewAuthToken();
146 151
 		}
@@ -169,7 +174,8 @@  discard block
 block discarded – undo
169 174
 	 * @throws \Guzzle\Http\Exception\ClientErrorResponseException
170 175
 	 * @throws \Guzzle\Http\Exception\BadResponseException
171 176
 	 */
172
-	private function sendRequest(RequestInterface $request, $attempt = 0) {
177
+	private function sendRequest(RequestInterface $request, $attempt = 0)
178
+	{
173 179
 		if ($attempt >= $this->options->get('max_attempt')) {
174 180
 			throw new \Exception("MAX attempt reached for " . $request->getUrl() . " with payload " . (string) $request);
175 181
 		}
@@ -202,7 +208,8 @@  discard block
 block discarded – undo
202 208
 	 * @return self
203 209
 	 * @throws \Exception
204 210
 	 */
205
-	private function renewAuthToken() {
211
+	private function renewAuthToken()
212
+	{
206 213
 		$token = (string) $this->sendRequest(
207 214
 			$this->httpClient->post(
208 215
 				self::AUTHENTICATION,
@@ -232,7 +239,8 @@  discard block
 block discarded – undo
232 239
 	 * @param string $accessToken
233 240
 	 * @return self
234 241
 	 */
235
-	private function setAuthorizationHeader($accessToken) {
242
+	private function setAuthorizationHeader($accessToken)
243
+	{
236 244
 		$this->accessToken = $accessToken;
237 245
 
238 246
 		$this->options->set(
@@ -255,7 +263,8 @@  discard block
 block discarded – undo
255 263
 	 * @param string $field
256 264
 	 * @return string
257 265
 	 */
258
-	private function getAttachmentEndPoint($idArticle, $field) {
266
+	private function getAttachmentEndPoint($idArticle, $field)
267
+	{
259 268
 		return $this->replaceEndPointId(
260 269
 			$idArticle,
261 270
 			$this->attachmentUrl[$field]
@@ -268,7 +277,8 @@  discard block
 block discarded – undo
268 277
 	 * @param string $identifier
269 278
 	 * @return string
270 279
 	 */
271
-	private function getArticleWithIdEndPoint($identifier) {
280
+	private function getArticleWithIdEndPoint($identifier)
281
+	{
272 282
 		return self::ARTICLE_ENDPOINT . "/$identifier";
273 283
 	}
274 284
 
@@ -279,7 +289,8 @@  discard block
 block discarded – undo
279 289
 	 * @param string $url
280 290
 	 * @return string
281 291
 	 */
282
-	private function replaceEndPointId($identifier, $url) {
292
+	private function replaceEndPointId($identifier, $url)
293
+	{
283 294
 		return str_replace(
284 295
 			'{article_id}',
285 296
 			$identifier,
@@ -293,7 +304,8 @@  discard block
 block discarded – undo
293 304
 	 * @param \One\Collection $collection
294 305
 	 * @return array
295 306
 	 */
296
-	private function normalizePayload(Collection $collection) {
307
+	private function normalizePayload(Collection $collection)
308
+	{
297 309
 		return $collection->toArray();
298 310
 	}
299 311
 
@@ -303,7 +315,8 @@  discard block
 block discarded – undo
303 315
 	 * @param \One\Model\Article $article
304 316
 	 * @return \One\Model\Article
305 317
 	 */
306
-	public function submitArticle(Article $article) {
318
+	public function submitArticle(Article $article)
319
+	{
307 320
 		$responseArticle = $this->post(
308 321
 			self::ARTICLE_ENDPOINT,
309 322
 			$this->normalizePayload(
@@ -337,7 +350,8 @@  discard block
 block discarded – undo
337 350
 	 * @param string $field
338 351
 	 * @return array
339 352
 	 */
340
-	public function submitAttachment($idArticle, Model $attachment, $field) {
353
+	public function submitAttachment($idArticle, Model $attachment, $field)
354
+	{
341 355
 		return json_decode(
342 356
 			$this->post(
343 357
 				$this->getAttachmentEndPoint($idArticle, $field),
@@ -355,7 +369,8 @@  discard block
 block discarded – undo
355 369
 	 * @param string $idArticle
356 370
 	 * @return string json
357 371
 	 */
358
-	public function getArticle($idArticle) {
372
+	public function getArticle($idArticle)
373
+	{
359 374
 		return $this->get(
360 375
 			self::ARTICLE_CHECK_ENDPOINT . "/$idArticle"
361 376
 		);
@@ -366,7 +381,8 @@  discard block
 block discarded – undo
366 381
 	 *
367 382
 	 * @return string json
368 383
 	 */
369
-	public function listArticle() {
384
+	public function listArticle()
385
+	{
370 386
 		return $this->get(
371 387
 			self::ARTICLE_ENDPOINT
372 388
 		);
@@ -378,7 +394,8 @@  discard block
 block discarded – undo
378 394
 	 * @param string $idArticle
379 395
 	 * @return string
380 396
 	 */
381
-	public function deleteArticle($idArticle) {
397
+	public function deleteArticle($idArticle)
398
+	{
382 399
 		$articleOnRest = $this->getArticle($idArticle);
383 400
 
384 401
 		if (!empty($articleOnRest)) {
@@ -410,7 +427,8 @@  discard block
 block discarded – undo
410 427
 	 * @param string $order
411 428
 	 * @return string
412 429
 	 */
413
-	public function deleteAttachment($idArticle, $field, $order) {
430
+	public function deleteAttachment($idArticle, $field, $order)
431
+	{
414 432
 		return $this->delete(
415 433
 			$this->getAttachmentEndPoint($idArticle, $field) . "/$order"
416 434
 		);
@@ -424,7 +442,8 @@  discard block
 block discarded – undo
424 442
 	 * @param array $options
425 443
 	 * @return string
426 444
 	 */
427
-	final public function get($path, $header = array(), $options = array()) {
445
+	final public function get($path, $header = array(), $options = array())
446
+	{
428 447
 		if (isset($this->logger) && !is_null($this->logger)) {
429 448
 			$this->logger->info("Hi, from post, path is " . $path);
430 449
 		}
@@ -446,7 +465,8 @@  discard block
 block discarded – undo
446 465
 	 * @param array $options
447 466
 	 * @return string
448 467
 	 */
449
-	final public function post($path, $body, $header = array(), $options = array()) {
468
+	final public function post($path, $body, $header = array(), $options = array())
469
+	{
450 470
 
451 471
 		if (isset($this->logger) && !is_null($this->logger)) {
452 472
 			$this->logger->info("Hi, from post " . $path);
@@ -469,7 +489,8 @@  discard block
 block discarded – undo
469 489
 	 * @param array $options
470 490
 	 * @return string
471 491
 	 */
472
-	final public function delete($path, $body = array(), $header = array(), $options = array()) {
492
+	final public function delete($path, $body = array(), $header = array(), $options = array())
493
+	{
473 494
 		return $this->requestGate(
474 495
 			'DELETE',
475 496
 			$path,
@@ -482,7 +503,8 @@  discard block
 block discarded – undo
482 503
 	/**
483 504
 	 * @inheritDoc
484 505
 	 */
485
-	public function setLogger(LoggerInterface $logger) {
506
+	public function setLogger(LoggerInterface $logger)
507
+	{
486 508
 		$this->logger = $logger;
487 509
 	}
488 510
 
Please login to merge, or discard this patch.
src/DummyLogger.php 2 patches
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -17,52 +17,52 @@
 block discarded – undo
17 17
  * @method void log($level, $message, array $context)
18 18
  */
19 19
 class DummyLogger implements LoggerInterface {
20
-	/**
21
-	 * {@inheritdoc}
22
-	 */
23
-	public function emergency($message, array $context = array()) {
24
-	}
20
+    /**
21
+     * {@inheritdoc}
22
+     */
23
+    public function emergency($message, array $context = array()) {
24
+    }
25 25
 
26
-	/**
27
-	 * {@inheritdoc}
28
-	 */
29
-	public function alert($message, array $context = array()) {
30
-	}
26
+    /**
27
+     * {@inheritdoc}
28
+     */
29
+    public function alert($message, array $context = array()) {
30
+    }
31 31
 
32
-	/**
33
-	 * {@inheritdoc}
34
-	 */
35
-	public function critical($message, array $context = array()) {
36
-	}
37
-	/**
38
-	 * {@inheritdoc}
39
-	 */
40
-	public function error($message, array $context = array()) {
41
-	}
42
-	/**
43
-	 * {@inheritdoc}
44
-	 */
45
-	public function warning($message, array $context = array()) {
46
-	}
47
-	/**
48
-	 * {@inheritdoc}
49
-	 */
50
-	public function notice($message, array $context = array()) {
51
-	}
52
-	/**
53
-	 * {@inheritdoc}
54
-	 */
55
-	public function info($message, array $context = array()) {
56
-		echo $message . "\n";
57
-	}
58
-	/**
59
-	 * {@inheritdoc}
60
-	 */
61
-	public function debug($message, array $context = array()) {
62
-	}
63
-	/**
64
-	 * {@inheritdoc}
65
-	 */
66
-	public function log($level, $message, array $context = array()) {
67
-	}
32
+    /**
33
+     * {@inheritdoc}
34
+     */
35
+    public function critical($message, array $context = array()) {
36
+    }
37
+    /**
38
+     * {@inheritdoc}
39
+     */
40
+    public function error($message, array $context = array()) {
41
+    }
42
+    /**
43
+     * {@inheritdoc}
44
+     */
45
+    public function warning($message, array $context = array()) {
46
+    }
47
+    /**
48
+     * {@inheritdoc}
49
+     */
50
+    public function notice($message, array $context = array()) {
51
+    }
52
+    /**
53
+     * {@inheritdoc}
54
+     */
55
+    public function info($message, array $context = array()) {
56
+        echo $message . "\n";
57
+    }
58
+    /**
59
+     * {@inheritdoc}
60
+     */
61
+    public function debug($message, array $context = array()) {
62
+    }
63
+    /**
64
+     * {@inheritdoc}
65
+     */
66
+    public function log($level, $message, array $context = array()) {
67
+    }
68 68
 }
Please login to merge, or discard this patch.
Braces   +20 added lines, -10 removed lines patch added patch discarded remove patch
@@ -16,53 +16,63 @@
 block discarded – undo
16 16
  * @method void debug($message, array $context)
17 17
  * @method void log($level, $message, array $context)
18 18
  */
19
-class DummyLogger implements LoggerInterface {
19
+class DummyLogger implements LoggerInterface
20
+{
20 21
 	/**
21 22
 	 * {@inheritdoc}
22 23
 	 */
23
-	public function emergency($message, array $context = array()) {
24
+	public function emergency($message, array $context = array())
25
+	{
24 26
 	}
25 27
 
26 28
 	/**
27 29
 	 * {@inheritdoc}
28 30
 	 */
29
-	public function alert($message, array $context = array()) {
31
+	public function alert($message, array $context = array())
32
+	{
30 33
 	}
31 34
 
32 35
 	/**
33 36
 	 * {@inheritdoc}
34 37
 	 */
35
-	public function critical($message, array $context = array()) {
38
+	public function critical($message, array $context = array())
39
+	{
36 40
 	}
37 41
 	/**
38 42
 	 * {@inheritdoc}
39 43
 	 */
40
-	public function error($message, array $context = array()) {
44
+	public function error($message, array $context = array())
45
+	{
41 46
 	}
42 47
 	/**
43 48
 	 * {@inheritdoc}
44 49
 	 */
45
-	public function warning($message, array $context = array()) {
50
+	public function warning($message, array $context = array())
51
+	{
46 52
 	}
47 53
 	/**
48 54
 	 * {@inheritdoc}
49 55
 	 */
50
-	public function notice($message, array $context = array()) {
56
+	public function notice($message, array $context = array())
57
+	{
51 58
 	}
52 59
 	/**
53 60
 	 * {@inheritdoc}
54 61
 	 */
55
-	public function info($message, array $context = array()) {
62
+	public function info($message, array $context = array())
63
+	{
56 64
 		echo $message . "\n";
57 65
 	}
58 66
 	/**
59 67
 	 * {@inheritdoc}
60 68
 	 */
61
-	public function debug($message, array $context = array()) {
69
+	public function debug($message, array $context = array())
70
+	{
62 71
 	}
63 72
 	/**
64 73
 	 * {@inheritdoc}
65 74
 	 */
66
-	public function log($level, $message, array $context = array()) {
75
+	public function log($level, $message, array $context = array())
76
+	{
67 77
 	}
68 78
 }
Please login to merge, or discard this patch.