<?php
namespace ModulesAbstract\ElectronicInvoice\Communicator; class AbstractCommunicator { protected $logger = null; public function __construct() { $this->logger = new \Engine\Logger\Dummy(); } public function getLogger() { return $this->logger; } public function setLogger($logger) { $this->logger = $logger; return $this; } protected function createHttpTransferObj() { $transfer = new \App\Utils\HttpTransfer\StandardTransferWithErrorHandling(); $transfer->setHeaders([ 'User-Agent: StandardTransferWithErrorHandling', 'Accept-Language: en', ]); ini_set("default_socket_timeout", 1.5*60); return $transfer; } protected function json_decode($jsonStr) { $json = \json_decode($jsonStr); $json_last_error_str = null; switch (json_last_error()) { case JSON_ERROR_NONE: break; case JSON_ERROR_DEPTH: $json_last_error_str = 'JSON_ERROR_DEPTH - Maximum stack depth exceeded'; break; case JSON_ERROR_STATE_MISMATCH: $json_last_error_str = 'JSON_ERROR_STATE_MISMATCH - Underflow or the modes mismatch'; break; case JSON_ERROR_CTRL_CHAR: $json_last_error_str = 'JSON_ERROR_CTRL_CHAR - Unexpected control character found'; break; case JSON_ERROR_SYNTAX: $json_last_error_str = 'JSON_ERROR_SYNTAX - Syntax error, malformed JSON'; break; case JSON_ERROR_UTF8: $json_last_error_str = 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded'; break; default: $json_last_error_str = 'unknown'; break; } if ($json_last_error_str) { throw new \Exception\Net\Base(sprintf("[%s] Invalid JSON response. Error: %s", __METHOD__, $json_last_error_str)); } return $json; } protected function buildLogContextFromStandardTransferWithErrorHandling(array $logContext, \App\Utils\HttpTransfer\StandardTransferWithErrorHandling $transfer) { $lastRequest = $transfer->getLastRequest(); unset($lastRequest['lastResponse']['processedHeaders']); $logContext['getLastRequest'] = new \Engine\Logger\Context\StandardTransferLastRequest($lastRequest); return $logContext; } } 