Refactoring base functions

Fetch functions, curl and it's parameters now are all in the FetchApi
file, while other Classes will receive them by Dependence Injection
This commit is contained in:
Bruno F. Fontes 2019-06-21 21:12:00 -03:00
parent 7664bf67a4
commit 460812852e
Signed by: brunofontes
GPG Key ID: EE3447CE80048495
4 changed files with 160 additions and 77 deletions

18
src/BaseApi.php Normal file
View File

@ -0,0 +1,18 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @link https://github.com/brunofontes
*/
namespace BrunoFontes\Memsource;
class BaseApi
{
protected $fetchApi;
public function __construct(\BrunoFontes\Memsource\FetchApi $fetchApi)
{
$this->fetchApi = $fetchApi;
}
}

View File

@ -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;

109
src/FetchApi.php Normal file
View File

@ -0,0 +1,109 @@
<?php
/**
* A very compact and simple Memsource API library
*
* @author Bruno Fontes <developer@brunofontes.net>
* @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;
}
}

View File

@ -1,102 +1,58 @@
<?php
/**
* A very compact and simple Memsource API library
*
*
* @author Bruno Fontes <developer@brunofontes.net>
* @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;
}
}
}