2013-07-25 03:25:24 +04:00
|
|
|
<?php
|
2014-05-13 17:25:26 +04:00
|
|
|
namespace Mailgun\Tests\Mock\Connection;
|
2013-07-25 03:25:24 +04:00
|
|
|
|
2015-04-18 22:20:26 +03:00
|
|
|
use Mailgun\Connection\Exceptions\GenericHTTPError;
|
|
|
|
use Mailgun\Connection\Exceptions\InvalidCredentials;
|
|
|
|
use Mailgun\Connection\Exceptions\MissingEndpoint;
|
2013-08-08 03:22:19 +04:00
|
|
|
use Mailgun\Connection\RestClient;
|
2015-04-18 22:20:26 +03:00
|
|
|
use Mailgun\Messages\Exceptions\MissingRequiredMIMEParameters;
|
2013-07-25 03:25:24 +04:00
|
|
|
|
2014-05-14 17:06:05 +04:00
|
|
|
class TestBroker extends RestClient
|
|
|
|
{
|
|
|
|
private $apiKey;
|
|
|
|
|
|
|
|
protected $apiEndpoint;
|
|
|
|
|
2015-10-03 21:01:58 +03:00
|
|
|
public function __construct($apiKey = null, $apiHost = "api.mailgun.net", $apiVersion = "v3")
|
2014-05-14 17:06:05 +04:00
|
|
|
{
|
|
|
|
$this->apiKey = $apiKey;
|
2015-10-03 21:01:58 +03:00
|
|
|
$this->apiEndpoint = $apiHost;
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
2016-07-19 16:06:49 +03:00
|
|
|
public function post($endpointUrl, array $postData = array(), $files = array())
|
2014-05-14 17:06:05 +04:00
|
|
|
{
|
2015-04-18 22:20:26 +03:00
|
|
|
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get($endpointUrl, $queryString = array())
|
|
|
|
{
|
2015-04-18 22:20:26 +03:00
|
|
|
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function delete($endpointUrl)
|
|
|
|
{
|
2015-04-18 22:20:26 +03:00
|
|
|
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function put($endpointUrl, $queryString)
|
|
|
|
{
|
2015-04-18 22:20:26 +03:00
|
|
|
return $this->testResponseHandler($endpointUrl, $httpResponseCode = 200);
|
2014-05-14 17:06:05 +04:00
|
|
|
}
|
|
|
|
|
2015-04-18 22:20:26 +03:00
|
|
|
public function testResponseHandler($endpointUrl, $httpResponseCode = 200)
|
2014-05-14 17:06:05 +04:00
|
|
|
{
|
|
|
|
if ($httpResponseCode === 200) {
|
|
|
|
$result = new \stdClass();
|
|
|
|
$result->http_response_body = new \stdClass();
|
|
|
|
$jsonResponseData = json_decode('{"message": "Some JSON Response Data", "id": "1234"}');
|
|
|
|
foreach ($jsonResponseData as $key => $value) {
|
|
|
|
$result->http_response_body->$key = $value;
|
|
|
|
}
|
2015-04-18 22:20:26 +03:00
|
|
|
} elseif ($httpResponseCode == 400) {
|
2014-05-14 17:06:05 +04:00
|
|
|
throw new MissingRequiredMIMEParameters(EXCEPTION_MISSING_REQUIRED_MIME_PARAMETERS);
|
2015-04-18 22:20:26 +03:00
|
|
|
} elseif ($httpResponseCode == 401) {
|
2014-05-14 17:06:05 +04:00
|
|
|
throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS);
|
2015-04-18 22:20:26 +03:00
|
|
|
} elseif ($httpResponseCode == 401) {
|
2014-05-14 17:06:05 +04:00
|
|
|
throw new GenericHTTPError(EXCEPTION_INVALID_CREDENTIALS);
|
2015-04-18 22:20:26 +03:00
|
|
|
} elseif ($httpResponseCode == 404) {
|
2014-05-14 17:06:05 +04:00
|
|
|
throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT);
|
|
|
|
} else {
|
|
|
|
throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$result->http_response_code = $httpResponseCode;
|
|
|
|
$result->http_endpoint_url = $endpointUrl;
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2013-07-26 02:42:06 +04:00
|
|
|
|
2013-07-25 03:25:24 +04:00
|
|
|
|
|
|
|
}
|