Files
piratepoet/tests/unit/Services/BridgeTestMockAPI.php
Alexey Stoletniy 98d6f55a6e Tweak Sending Service key validation after a code review [MAILPOET-743]
* Abstract key state to unbound it from the API response codes
* Rename SendingServiceKeyCheck task for clarity
* Add a setter for the API key in the Bridge API class
* Make some smaller fixes
2017-01-27 16:22:11 +03:00

47 lines
988 B
PHP

<?php
namespace MailPoet\Services\Bridge;
use Carbon\Carbon;
if(!defined('ABSPATH')) exit;
class MockAPI {
public $api_key;
function __construct($api_key) {
$this->setKey($api_key);
}
function checkKey() {
// if key begins with these codes, return them
$regex = '/^(401|402|503)/';
$code = preg_match($regex, $this->api_key, $m) ? $m[1] : 200;
return $this->processResponse($code);
}
function setKey($api_key) {
$this->api_key = $api_key;
}
private function processResponse($code) {
switch($code) {
case 200:
$body = array('subscriber_limit' => 10000);
break;
case 402:
$body = array(
'subscriber_limit' => 10000,
'expire_at' => Carbon::createFromTimestamp(current_time('timestamp'))
->addMonth()->format('c')
);
break;
case 401:
default:
$body = null;
break;
}
return array('code' => $code, 'data' => $body);
}
}