You are here

public function WebformTestHandlerRemotePostClient::request in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/modules/webform_test_handler_remote_post/src/WebformTestHandlerRemotePostClient.php \Drupal\webform_test_handler_remote_post\WebformTestHandlerRemotePostClient::request()

File

tests/modules/webform_test_handler_remote_post/src/WebformTestHandlerRemotePostClient.php, line 19

Class

WebformTestHandlerRemotePostClient
Extend Guzzle client so that we can override remote posts.

Namespace

Drupal\webform_test_handler_remote_post

Code

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();

  // Handle 404 errors.
  switch ($response_type) {

    // 404 Not Found.
    case 404:
      return new Response(404, [], 'File not found');

    // 405 Method Not Allowed.
    case 405:
      return new Response(405, [], 'Method Not Allowed');

    // 401 Unauthorized.
    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));

    // 500 Internal Server Error.
    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));

    // 200 OK.
    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));
  }
}