Skip to content

Commit

Permalink
Merge pull request #54 from redbitcz/fix-unavailable-api
Browse files Browse the repository at this point in the history
Handle API Exception
  • Loading branch information
jakubboucek authored Jan 8, 2021
2 parents c6cd440 + cf6eeda commit 3a4ce3e
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function __construct( Plugin $loader ) {

/**
* Get products from simple shop via API
* TODO: Show message if loading pf products failed
*/
public function wp_ajax_load_simple_shop_products() {
$this->update_simpleshop_products_cache();
Expand All @@ -52,7 +53,6 @@ public function wp_ajax_load_simple_shop_products() {
* Return products. If you need force refresh products from API, call `update_simpleshop_products_cache()` before
*
* @return array
* @throws VyfakturujAPIException
*/
public function get_simpleshop_products() {
$products = $this->get_simpleshop_products_cache();
Expand Down Expand Up @@ -90,23 +90,24 @@ protected function get_simpleshop_products_cache() {

/**
* Update Products cache from Vyfakturuj API
*
* @throws VyfakturujAPIException
*/
public function update_simpleshop_products_cache() {
$products = $this->load_simpleshop_products();

$cacheKey = $this->loader->get_cache_user_key();
$cachedTime = time();

$cache = [
$cacheKey => array_merge(
$products,
[ self::PRODUCTS_CACHE_FIELD => $cachedTime ]
)
];

update_option( 'ssc_cache_products', $cache );
try {
$products = $this->load_simpleshop_products();
$cacheKey = $this->loader->get_cache_user_key();
$cachedTime = time();

$cache = [
$cacheKey => array_merge(
$products,
[ self::PRODUCTS_CACHE_FIELD => $cachedTime ]
)
];

return update_option( 'ssc_cache_products', $cache );
} catch ( VyfakturujAPIException $e ) {
return new \WP_Error( 'api-error', $e->getMessage() );
}
}


Expand Down

0 comments on commit 3a4ce3e

Please sign in to comment.