Skip to content

Commit

Permalink
Improve performance by performing fewer API calls and storing issuers…
Browse files Browse the repository at this point in the history
… and methods in transient cache
  • Loading branch information
Willem Stuursma committed Jun 18, 2014
1 parent 291ba1d commit 892ead9
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 21 deletions.
2 changes: 1 addition & 1 deletion MPM/lib
27 changes: 18 additions & 9 deletions MPM/mpm_gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@

class MPM_Gateway extends WC_Payment_Gateway
{
/**
* Does this payment method have any extra fields? Only iDEAL has extra fields.
*
* @var bool
*/
public $has_fields = FALSE;

protected $_data = null;

/**
* @var Mollie_API_Object_Issuer[]
*/
protected $issuers = array();

public function __construct()
{
// Register this method with MPM_Settings
/** @var MPM_Settings $mpm */
global $mpm;
$methods = $mpm->get_methods();
if ($mpm->count >= count($methods))
Expand All @@ -48,15 +61,11 @@ public function __construct()
$this->title = __($this->_data->description, 'MPM'); // translate visual title

// Define issuers (if any)
$issuers = $mpm->get_api()->issuers->all();
$this->has_fields = FALSE;
$this->issuers = array();
foreach ($issuers as $issuer)
{
if ($issuer->method === $this->id) {
$this->has_fields = TRUE;
$this->issuers[] = $issuer;
}
$this->issuers = $mpm->get_issuers($this->id);

if (!empty($this->issuers))
{
$this->has_fields = TRUE;
}

// Assign image
Expand Down
87 changes: 76 additions & 11 deletions MPM/mpm_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class MPM_Settings extends WC_Settings_API
{
// Keep track of payment methods
public $count = 0;
protected $methods = array();

/** @var $api Mollie_API_Client|null */
protected $api = null;
Expand Down Expand Up @@ -410,27 +409,93 @@ public function get_api()
}

/**
* Retrieve the Mollie methods
* @var NULL|Mollie_API_Object_Method[]
*/
private static $_methods;

/**
* Retrieve the enabled payment methods from the Mollie API. Caches these for about a minute.
*
* @return array|Mollie_API_Object_List|Mollie_API_Object_Method[]
*/
public function get_methods()
{
// If we have them, give them
if (!empty($this->methods))
// Retrieve the methods or fail with error
try
{
return $this->methods;
if (empty(self::$_methods))
{
$cached = @unserialize(get_transient('mpm_api_methods'));

if ($cached instanceof Mollie_API_Object_List)
{
self::$_methods = $cached;
}
else
{
self::$_methods = $this->get_api()->methods->all();
set_transient('mpm_api_methods', self::$_methods, MINUTE_IN_SECONDS);
}
}

return self::$_methods;
}
// If we don't even have the api yet, get it
if (is_null($this->api))
catch (Mollie_API_Exception $e)
{
$this->api = $this->get_api();
$this->errors[] = __('Payment error:', 'MPM') . $e->getMessage();
$this->display_errors();
return array();
}
}

// Retrieve the methods or fail with error
/**
* @var NULL|Mollie_API_Object_Issuer[]
*/
private static $_issuers;

/**
* Get the issuers from the Mollie API. Caches these in Wordpress cache for about a day.
*
* @param string|NULL $method Filter issuers by method
* @return Mollie_API_Object_Issuer[]
*/
public function get_issuers ($method = NULL)
{
// Retrieve the issuers or fail with error
try
{
$this->methods = $this->api->methods->all();
return $this->methods;
if (empty(self::$_issuers))
{
$cached = @unserialize(get_transient('mpm_api_issuers'));

if ($cached instanceof Mollie_API_Object_List)
{
self::$_issuers = $cached;
}
else
{
self::$_issuers = $this->get_api()->issuers->all();
set_transient('mpm_api_issuers', self::$_issuers, DAY_IN_SECONDS);
}
}

// Filter issuers by method
if ($method !== NULL)
{
$issuers = array();

foreach(self::$_issuers AS $issuer)
{
if ($issuer->method === $method)
{
$issuers[] = $issuer;
}
}

return $issuers;
}

return self::$_issuers;
}
catch (Mollie_API_Exception $e)
{
Expand Down

0 comments on commit 892ead9

Please sign in to comment.