diff --git a/src/BaseApi.php b/src/BaseApi.php new file mode 100644 index 0000000..2cbe7c4 --- /dev/null +++ b/src/BaseApi.php @@ -0,0 +1,18 @@ + + * @link https://github.com/brunofontes + */ +namespace BrunoFontes\Memsource; + +class BaseApi +{ + protected $fetchApi; + + public function __construct(\BrunoFontes\Memsource\FetchApi $fetchApi) + { + $this->fetchApi = $fetchApi; + } +} diff --git a/src/BilingualFile.php b/src/BilingualFile.php index d5e5f8f..170a7e8 100644 --- a/src/BilingualFile.php +++ b/src/BilingualFile.php @@ -7,7 +7,7 @@ */ namespace BrunoFontes\Memsource; -class BilingualFile extends \BrunoFontes\Memsource +class BilingualFile extends \BrunoFontes\Memsource\BaseApi { private $_url = '/bilingualFiles'; @@ -35,7 +35,7 @@ class BilingualFile extends \BrunoFontes\Memsource for ($i = 0; $i < count($groupedJobUids); $i++) { $apiReadyArray = $this->_convertUidArrayToApiRequest($groupedJobUids[$i]); $filenames[$i] = count($groupedJobUids) > 1?"{$i}_{$filename}":$filename; - $filecontent = $this->fetch('jsonPost', $url, $apiReadyArray); + $filecontent = $this->fetchApi->fetch('jsonPost', $url, $apiReadyArray); $this->_saveIntoFile($filenames[$i], $filecontent); } return $filenames; diff --git a/src/FetchApi.php b/src/FetchApi.php new file mode 100644 index 0000000..39a1350 --- /dev/null +++ b/src/FetchApi.php @@ -0,0 +1,109 @@ + + * @link https://github.com/brunofontes + */ +namespace BrunoFontes\Memsource; + +class FetchApi +{ + protected $base_url; + protected $token; + + /** + * BaseAPI needs at least the Memsource Token to use it's API + * + * @param string $token + * @param string $memsourceBaseUrl [Optional] A non-standard Memsource URL base for the API to work + */ + public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web') + { + $this->base_url = $memsourceBaseUrl; + $this->token = $token; + } + + /** + * Fetch API data using curl + * + * @param string $method Should be 'get', 'post', 'jsonPost' or 'download' + * @param string $url The api url + * @param array $parameters Array ['key' => 'value'] of get or post fields or structured array for json requests + * @param string $filename [optional] Specified file in which the download request will be saved + * + * @return void + */ + public function fetch(string $method, string $url, array $parameters, $filename = '') : string + { + $setopt = []; + switch ($method) { + case 'get': + $parameters = http_build_query($parameters); + $url = $url . ($parameters ? '?'.$parameters : ''); + break; + case 'post': + $parameters = http_build_query($parameters); + $setopt = $this->getPostParam($parameters); + break; + case 'jsonPost': + $setopt = $this->getJsonPostParam($parameters); + break; + case 'download': + if (empty($filename)) { + throw new Exception('You need to specify a filename to download a file.', 1); + } + $setopt = $this->getDownloadFileParam($filename) + + $this->getJsonPostParam($parameters); + break; + } + return $this->curl($url, $setopt); + } + + private function getDownloadFileParam(string $filename) + { + return [ + CURLOPT_FILE => $file, + ]; + } + + private function getJsonPostParam(array $postFields) + { + return [ + CURLOPT_HTTPHEADER => ['Content-type: application/json'], + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => json_encode($postFields) + ]; + } + + private function getPostParam(string $postFields) + { + return [ + CURLOPT_POST => true, + CURLOPT_POSTFIELDS => $postFields + ]; + } + + protected function curl(string $url, array $curl_extra_setopt = []) + { + if (empty($url)) { + throw new Exception('URL not defined', 1); + } + + $header = $this->token ? ["Authorization: Bearer {$this->token}"] : []; + $header = array_merge($header, $curl_extra_setopt[CURLOPT_HTTPHEADER]??[]); + $curl_setopt = [ + CURLOPT_URL => $this->base_url . $url, + CURLOPT_HTTPHEADER => $header, + CURLOPT_SSL_VERIFYPEER => false, + CURLOPT_TIMEOUT => 500, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_RETURNTRANSFER => true + ]; + $curl = curl_init(); + curl_setopt_array($curl, ($curl_setopt + $curl_extra_setopt)); + $response = curl_exec($curl); + curl_close($curl); + return $response; + } +} diff --git a/src/Memsource.php b/src/Memsource.php index f94a74d..b6f4e36 100644 --- a/src/Memsource.php +++ b/src/Memsource.php @@ -1,102 +1,58 @@ * @link https://github.com/brunofontes */ + namespace BrunoFontes; +/** + * Memsource API class + * + * Instructions: https://github.com/brunofontes/Memsource-API/ + * Memsource API details: https://cloud.memsource.com/web/docs/api + */ class Memsource { private $_oauth; - protected $base_url; - protected $token; + private $_bilingualFile; + private $_project; + private $_fetchApi; - public function __construct(string $memsourceBaseUrl = 'https://cloud.memsource.com/web') + public function __construct(string $token = null, string $memsourceBaseUrl = 'https://cloud.memsource.com/web') { - $this->base_url = $memsourceBaseUrl; + $this->_fetchApi = new \BrunoFontes\Memsource\FetchApi($token, $memsourceBaseUrl); } - public function oauth() + /** + * Memsource Oauth functions + * + * @return \BrunoFontes\Memsource\Oauth + */ + public function oauth(): \BrunoFontes\Memsource\Oauth { return $this->_oauth ?? $this->_oauth = new \BrunoFontes\Memsource\oauth(); } /** - * Fetch API data using curl + * Memsource API Project related functions * - * @param string $method Should be 'get', 'post', 'jsonPost' or 'download' - * @param string $url The api url - * @param array $parameters Array ['key' => 'value'] of get or post fields or structured array for json requests - * @param string $filename [optional] Specified file in which the download request will be saved - * - * @return void + * @return \BrunoFontes\Memsource\Project */ - protected function fetch(string $method, string $url, array $parameters, $filename = '') + public function project(): \BrunoFontes\Memsource\Project { - switch ($method) { - case 'get': - $parameters = http_build_query($parameters); - break; - case 'post': - $parameters = http_build_query($parameters); - $setopt = $this->getPostParam($parameters); - break; - case 'jsonPost': - $setopt = $this->getJsonPostParam($parameters); - break; - case 'download': - if (empty($filename)) { - throw new Exception("You need to specify a filename to download a file.", 1); - } - $setopt = $this->getDownloadFileParam($filename) - + $this->getJsonPostParam($parameters); - break; - } - return $this->curl($url, $setopt); + return $this->_project ?? $this->_project = new \BrunoFontes\Memsource\Project($this->_fetchApi); } - private function getDownloadFileParam(string $filename) + /** + * Memsource API BilingualFile related functions + * + * @return \BrunoFontes\Memsource\BilingualFile + */ + public function bilingualFile(): \BrunoFontes\Memsource\BilingualFile { - $file = fopen($filename, 'w+'); - return [ - CURLOPT_FILE => $file, - CURLOPT_FOLLOWLOCATION => true, - ]; + return $this->_bilingualFile ?? $this->_bilingualFile = new \BrunoFontes\Memsource\BilingualFile($this->_fetchApi); } - - private function getJsonPostParam(array $postFields) - { - return [ - CURLOPT_HTTPHEADER => ['Content-type: application/json'], - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => json_encode($postFields) - ]; - } - - private function getPostParam(string $postFields) - { - return [ - CURLOPT_POST => true, - CURLOPT_POSTFIELDS => $postFields - ]; - } - - protected function curl(string $url, array $curl_extra_setopt=[]) - { - $header = $this->token ? ["Authorization: Bearer {$this->token}"] : []; - $curl_setopt = [ - CURLOPT_URL => $this->base_url . $url, - CURLOPT_HTTPHEADER => ($header + $curl_extra_setopt[CURLOPT_HTTPHEADER]), - CURLOPT_SSL_VERIFYPEER => false, - CURLOPT_TIMEOUT => 500, - CURLOPT_FOLLOWLOCATION => true, - ]; - $curl = curl_init(); - curl_setopt_array($curl, ($curl_setopt + $curl_extra_setopt)); - $response = curl_exec($curl); - curl_close($curl); - return $response; - } -} \ No newline at end of file +}