* @copyright 2016-2017 GFI Informatique, 2016-2017 TNT * @license https://opensource.org/licenses/MIT MIT License */ require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Debug.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_Logger.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_JsonRPCClient.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_ServiceCache.php'; require_once _PS_MODULE_DIR_.'tntofficiel/libraries/TNTOfficiel_PasswordManager.php'; final class TNTOfficiel_RelayPointsHelper { /** @var Singleton */ private static $_instance = null; /** @var TNTOfficiel_Logger */ private $objLogger; /** * Constructor. */ public function __construct() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $this->objLogger = new TNTOfficiel_Logger(); } /** * Creates a singleton. * * @param void * * @return Singleton */ public static function getInstance() { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); if (is_null(TNTOfficiel_RelayPointsHelper::$_instance)) { TNTOfficiel_RelayPointsHelper::$_instance = new TNTOfficiel_RelayPointsHelper(); } return TNTOfficiel_RelayPointsHelper::$_instance; } /** * Get the relay points for the given postcode/city from the middleware or from the cache. * * @param $postcode * @param $city * @param $idShop * * @return array */ public function getRelayPoints($postcode, $city, $idShop) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $objCache = Cache::getInstance(); // Get params without timestamp $arrRequestData = $this->_getParams($postcode, $idShop, $city); $strCacheKey = 'middleware::getRelayPoints_'.md5(serialize($arrRequestData)); // Retrieve from cache if exist. if ($objCache->exists($strCacheKey)) { return Tools::jsonDecode($objCache->get($strCacheKey), true); } // JSON RPC Request to Middleware. $client = new TNTOfficiel_JsonRPCClient(Configuration::get('TNT_CARRIER_MIDDLEWARE_URL')); try { $arrResponse = $client->execute('getRelayPoints', $arrRequestData); $this->objLogger->logMessageTnt('getRelayPoints', null, 'JSON', true, 'SUCCESS'); } catch (Exception $objException) { $strStatus = ($objException->getCode() == 500) ? 'FATAL' : 'ERROR'; $strMsg = $objException->getMessage(); $this->objLogger->logMessageTnt('getRelayPoints', $strMsg, 'JSON', false, $strStatus); return array(); } // If no RelayPoints if ($arrResponse['relay_points'] === false) { $arrResponse['relay_points'] = array(); } // Store in cache until midnight. $objCache->set($strCacheKey, Tools::jsonEncode($arrResponse['relay_points']), TNTOfficiel_ServiceCache::getSecondsUntilMidnight()); return $arrResponse['relay_points']; } /** * Get the repositories for the given postcode from the middleware or from the cache. * * @param $postcode * @param $idShop * * @return array */ public function getRepositories($postcode, $idShop) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $objCache = Cache::getInstance(); // Get params without timestamp $arrRequestData = $this->_getParams($postcode, $idShop); $strCacheKey = 'middleware::getRepositories_'.md5(serialize($arrRequestData)); // Retrieve from cache if exist. if ($objCache->exists($strCacheKey)) { return Tools::jsonDecode($objCache->get($strCacheKey), true); } // JSON RPC Request to Middleware. $client = new TNTOfficiel_JsonRPCClient(Configuration::get('TNT_CARRIER_MIDDLEWARE_URL')); try { $arrResponse = $client->execute('getRepositories', $arrRequestData); $this->objLogger->logMessageTnt('getRepositories', null, 'JSON', true, 'SUCCESS'); } catch (Exception $objException) { $strStatus = ($objException->getCode() == 500) ? 'FATAL' : 'ERROR'; $strMsg = $objException->getMessage(); $this->objLogger->logMessageTnt('getRepository', $strMsg, 'JSON', false, $strStatus); return array(); } // If no Repositories. if ($arrResponse['repositories'] === false) { $arrResponse['repositories'] = array(); } // Store in cache until midnight. $objCache->set($strCacheKey, Tools::jsonEncode($arrResponse['repositories']), TNTOfficiel_ServiceCache::getSecondsUntilMidnight()); return $arrResponse['repositories']; } /** * Get the params needed by the middleware. * * @param $postcode * @param $idShop * @param null $city * * @return array */ private function _getParams($postcode, $idShop, $city = null) { TNTOfficiel_Debug::log(array('msg' => '>>', 'file' => __FILE__, 'line' => __LINE__)); $arrData = array( 'store' => $idShop, 'merchant' => array( 'identity' => Configuration::get('TNT_CARRIER_USERNAME'), 'password' => TNTOfficiel_PasswordManager::getHash(), 'merchant_number' => Configuration::get('TNT_CARRIER_ACCOUNT'), ), 'postcode' => $postcode, 'city' => $city, ); //add the city if not null if ($city != null) { $arrData['city'] = $city; } return $arrData; } }