<?php



use Idearia\Logger;



abstract class BaseTransport {



	private $api = false;

	private $transport = false;



	public function __construct($transport) {

		$this->api = $transport;

		$this->transport = get_class($transport);

	}



	protected function processRequest($method, $data = []) {



		try {



			if ( is_array($method) ) {

				Logger::debug( "Transport {$this->transport}: Request " . implode("->", $method) . " DATA=" . $this->objToLog($data) );



				$result = $this->api->{$method[0]}->{$method[1]}(...$data);

			} else {

				Logger::debug( "Transport {$this->transport}: Request {$method} DATA=" . $this->objToLog($data) );



				$result = $this->api->{$method}(...$data);

			}



			if ( $result = $this->getResult($result) ) {

				Logger::debug( "Transport {$this->transport}: Request RESULT=" . $this->objToLog($result) );



				return $result;

			} else {

				Logger::warning( "Transport {$this->transport}: Request has no result" );



				return false;

			}		

		} catch (Throwable $e) {

		

			$errorData = [

				"message" => $e->getMessage(),

				"class" => $e::class,

				"request" => $method,

				"data" => $data,

			];

			Logger::error( "Transport {$this->transport}: Request ERROR=" . $this->objToLog($errorData) );

		

			return false;

		}

	}



	private function simpleRequest($url, $headers = [], $getData = [], $postData = []) {

		try {



			$params = [];

			foreach ($getData as $key => $value) {

				$params[] = urlencode($key) . "=" . urlencode($value);

			}

			$targetUrl = "{$url}" . (strpos($url, "?") ? "&" : "?") . implode("&", $params);



			$ch = curl_init();

			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

			curl_setopt($ch, CURLOPT_URL, $targetUrl);

			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

			curl_setopt($ch, CURLINFO_HEADER_OUT, true);



			if ($postData) {

				curl_setopt($ch, CURLOPT_POST, true);

				curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData) );

				$headers[] = "Content-type: application/json; charset=UTF-8";

			}

			

			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);



			Logger::debug( "Transport CURL: simpleRequest {$targetUrl} DATA=" . $this->objToLog($postData) );

			$result = curl_exec($ch); 

			curl_close($ch);



			if ( $result = $this->getResult($result) ) {

				Logger::debug( "Transport CURL: Request RESULT=" . $this->objToLog($result) );



				return $result;

			} else {

				Logger::debug( "Transport CURL: Request has no result" );



				return false;

			}

		} catch (Throwable $e) {

		

			$errorData = [

				"message" => $e->getMessage(),

				"class" => $e::class,

				"request" => $targetUrl,

				"headers" => $headers,

				"data" => $postData,

			];

			Logger::error( "Transport CURL: simpleRequest ERROR=" . $this->objToLog($errorData) );

		

			return false;

		}

	}

	public function simpleGet($url, $getData = [], $headers = []) {

		return $this->simpleRequest($url, $headers, $getData);

	}

	public function simplePost($url, $postData, $headers = []) {

		return $this->simpleRequest($url, $headers, [], $postData, $headers);

	}





	protected function objToLog($object) {

		return json_encode($object, JSON_UNESCAPED_UNICODE);

	}

	protected function objToArray($object) {

		return json_decode(json_encode($object, JSON_UNESCAPED_UNICODE), 1);

	}



	abstract protected function getResult($result);





}

