1
0
mirror of synced 2025-02-08 19:19:24 +03:00
woocommerce-module/retailcrm/include/api/class-wc-retailcrm-proxy.php

83 lines
2.8 KiB
PHP
Raw Normal View History

2017-06-07 16:29:57 +03:00
<?php
/**
* RetailCRM Integration.
2017-06-07 16:29:57 +03:00
*
* @package WC_Retailcrm_Proxy
* @category Integration
* @author RetailCRM
2017-06-07 16:29:57 +03:00
*/
if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) :
/**
* Class WC_Retailcrm_Proxy
*/
class WC_Retailcrm_Proxy
{
public function __construct($api_url, $api_key, $api_vers = 'v4')
2017-06-07 16:29:57 +03:00
{
$this->logger = new WC_Logger();
if ( ! class_exists( 'WC_Retailcrm_Client_V3' ) ) {
include_once( __DIR__ . '/class-wc-retailcrm-client-v3.php' );
}
if ( ! class_exists( 'WC_Retailcrm_Client_V4' ) ) {
include_once( __DIR__ . '/class-wc-retailcrm-client-v4.php' );
}
if ( ! class_exists( 'WC_Retailcrm_Client_V5' ) ) {
include_once( __DIR__ . '/class-wc-retailcrm-client-v5.php' );
2017-06-07 16:29:57 +03:00
}
if ($api_url && $api_key) {
switch ($api_vers) {
case 'v3':
$this->retailcrm = new WC_Retailcrm_Client_V3($api_url, $api_key);
break;
case 'v4':
$this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key);
break;
case 'v5':
$this->retailcrm = new WC_Retailcrm_Client_V5($api_url, $api_key);
break;
}
2017-06-07 16:29:57 +03:00
}
}
public function __call($method, $arguments)
{
try {
$response = call_user_func_array(array($this->retailcrm, $method), $arguments);
if ($response->isSuccessful()) {
$result = ' Ok';
} else {
$result = sprintf(
$method ." : Error: [HTTP-code %s] %s",
$response->getStatusCode(),
$response->getErrorMsg()
);
if (isset($response['errors'])) {
foreach ($response['errors'] as $error) {
$result .= " $error";
}
}
}
$this->logger->add('retailcrm', sprintf("[%s] %s", $method, $result));
} catch (WC_Retailcrm_Exception_Curl $exception) {
$this->logger->add('retailcrm', sprintf("[%s] %s - %s", $method, $exception->getMessage(), $result));
} catch (WC_Retailcrm_Exception_Json $exception) {
$this->logger->add('retailcrm', sprintf("[%s] %s - %s", $method, $exception->getMessage(), $result));
} catch (InvalidArgumentException $exception) {
$this->logger->add('retailcrm', sprintf("[%s] %s - %s", $method, $exception->getMessage(), $result));
}
return $response;
}
}
endif;