View source
<?php
namespace Drupal\webform_test_handler_remote_post;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Random;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
class WebformTestHandlerRemotePostClient extends Client {
public function request($method, $uri = '', array $options = []) {
if (strpos($uri, 'http://webform-test-handler-remote-post/') === FALSE) {
return parent::request($method, $uri, $options);
}
if ($method === 'get') {
parse_str(parse_url($uri, PHP_URL_QUERY), $params);
}
else {
$params = isset($options['json']) ? $options['json'] : $options['form_params'];
}
$response_type = isset($params['response_type']) ? $params['response_type'] : 200;
$operation = ltrim(parse_url($uri, PHP_URL_PATH), '/');
$random = new Random();
switch ($response_type) {
case 404:
return new Response(404, [], 'File not found');
case 405:
return new Response(405, [], 'Method Not Allowed');
case 401:
$status = 401;
$headers = [
'Content-Type' => [
'application/json',
],
];
$json = [
'method' => $method,
'status' => 'unauthorized',
'message' => (string) new FormattableMarkup('Unauthorized to process @type request.', [
'@type' => $operation,
]),
'options' => $options,
];
return new Response($status, $headers, Json::encode($json));
case 500:
$status = 500;
$headers = [
'Content-Type' => [
'application/json',
],
];
$json = [
'method' => $method,
'status' => 'fail',
'message' => (string) new FormattableMarkup('Failed to process @type request.', [
'@type' => $operation,
]),
'options' => $options,
];
return new Response($status, $headers, Json::encode($json));
case 201:
$status = 201;
$headers = [
'Content-Type' => [
'application/json',
],
];
$json = [
'method' => $method,
'status' => 'success',
'message' => (string) new FormattableMarkup('Process @type request.', [
'@type' => $operation,
]),
'options' => $options,
'confirmation_number' => $random
->name(20, TRUE),
];
return new Response($status, $headers, Json::encode($json));
case 200:
default:
$status = 200;
$headers = [
'Content-Type' => [
'application/json',
],
];
$json = [
'method' => $method,
'status' => 'success',
'message' => (string) new FormattableMarkup('Processed @type request.', [
'@type' => $operation,
]),
'options' => $options,
'confirmation_number' => $random
->name(20, TRUE),
];
return new Response($status, $headers, Json::encode($json));
}
}
}