TestBroker.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Mailgun\Tests\Mock\Connection;
  3. use Mailgun\Connection\RestClient;
  4. class TestBroker extends RestClient
  5. {
  6. private $apiKey;
  7. protected $apiEndpoint;
  8. public function __construct($apiKey = null, $apiEndpoint = "api.mailgun.net", $apiVersion = "v2")
  9. {
  10. $this->apiKey = $apiKey;
  11. $this->apiEndpoint = $apiEndpoint;
  12. }
  13. public function post($endpointUrl, $postData = array(), $files = array())
  14. {
  15. return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
  16. }
  17. public function get($endpointUrl, $queryString = array())
  18. {
  19. return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
  20. }
  21. public function delete($endpointUrl)
  22. {
  23. return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
  24. }
  25. public function put($endpointUrl, $queryString)
  26. {
  27. return $this->responseHandler($endpointUrl, $httpResponseCode = 200);
  28. }
  29. public function responseHandler($endpointUrl, $httpResponseCode = 200)
  30. {
  31. if ($httpResponseCode === 200) {
  32. $result = new \stdClass();
  33. $result->http_response_body = new \stdClass();
  34. $jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}');
  35. foreach ($jsonResponseData as $key => $value) {
  36. $result->http_response_body->$key = $value;
  37. }
  38. } elseif ($httpStatusCode == 400) {
  39. throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
  40. } elseif ($httpStatusCode == 401) {
  41. throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS);
  42. } elseif ($httpStatusCode == 401) {
  43. throw new GenericHTTPError(EXCEPTION_INVALID_CREDENTIALS);
  44. } elseif ($httpStatusCode == 404) {
  45. throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT);
  46. } else {
  47. throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR);
  48. return false;
  49. }
  50. $result->http_response_code = $httpResponseCode;
  51. $result->http_endpoint_url = $endpointUrl;
  52. return $result;
  53. }
  54. }