Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from DivineOmega/analysis-qMPM73
Applied fixes from StyleCI
- Loading branch information
Showing
7 changed files
with
94 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,23 @@ | ||
<?php | ||
|
||
namespace DivineOmega\CachetPHP\Client; | ||
|
||
class ApiResponse | ||
{ | ||
private $data; | ||
private $data; | ||
|
||
function __construct(array $data) | ||
{ | ||
$this->data = $data; | ||
} | ||
public function __construct(array $data) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
function getData(){ | ||
return isset($this->data['data']) ? $this->data['data'] : null; | ||
} | ||
public function getData() | ||
{ | ||
return isset($this->data['data']) ? $this->data['data'] : null; | ||
} | ||
|
||
function getMeta(){ | ||
return isset($this->data['meta']) ? $this->data['meta'] : null; | ||
} | ||
} | ||
public function getMeta() | ||
{ | ||
return isset($this->data['meta']) ? $this->data['meta'] : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,62 @@ | ||
<?php | ||
|
||
namespace DivineOmega\CachetPHP\Client; | ||
|
||
use DivineOmega\CachetPHP\Exceptions\CachetApiException; | ||
use GuzzleHttp\Client; | ||
|
||
class ApiV1Client implements IApiClient | ||
{ | ||
private $apiToken; | ||
private $guzzleClient; | ||
|
||
function __construct($apiUrl, $apiToken) | ||
{ | ||
$this->apiToken = $apiToken; | ||
|
||
$this->guzzleClient = new Client([ | ||
'base_uri' => $apiUrl.'/v1/', | ||
'timeout' => 3.0, | ||
]); | ||
} | ||
|
||
|
||
private function getAuthHeaders() | ||
{ | ||
$authHeaderKey = 'X-Cachet-Token'; | ||
$authHeaderValue = $this->apiToken; | ||
|
||
return [ | ||
$authHeaderKey => $authHeaderValue, | ||
]; | ||
} | ||
|
||
function request($url, $data = null, $method = 'GET', $authorisationRequired = true){ | ||
$options = []; | ||
|
||
if($authorisationRequired){ | ||
$options['headers'] = $this->getAuthHeaders(); | ||
} | ||
|
||
if($data) { | ||
if ($method != 'GET') { | ||
$options['json'] = $data; | ||
} else { | ||
$options['query'] = $data; | ||
} | ||
} | ||
$response = $this->guzzleClient->request($method, $url, $options); | ||
|
||
if ($response->getStatusCode() != 200) { | ||
throw new CachetApiException('cachet.php: Bad response. Code: '.$response->getStatusCode()); | ||
} | ||
|
||
$data = json_decode($response->getBody(), true); | ||
|
||
if (!$data) { | ||
throw new CachetApiException('cachet.php: Could not decode JSON from '.$url); | ||
} | ||
|
||
return new ApiResponse($data, 'data'); | ||
} | ||
} | ||
private $apiToken; | ||
private $guzzleClient; | ||
|
||
public function __construct($apiUrl, $apiToken) | ||
{ | ||
$this->apiToken = $apiToken; | ||
|
||
$this->guzzleClient = new Client([ | ||
'base_uri' => $apiUrl.'/v1/', | ||
'timeout' => 3.0, | ||
]); | ||
} | ||
|
||
private function getAuthHeaders() | ||
{ | ||
$authHeaderKey = 'X-Cachet-Token'; | ||
$authHeaderValue = $this->apiToken; | ||
|
||
return [ | ||
$authHeaderKey => $authHeaderValue, | ||
]; | ||
} | ||
|
||
public function request($url, $data = null, $method = 'GET', $authorisationRequired = true) | ||
{ | ||
$options = []; | ||
|
||
if ($authorisationRequired) { | ||
$options['headers'] = $this->getAuthHeaders(); | ||
} | ||
|
||
if ($data) { | ||
if ($method != 'GET') { | ||
$options['json'] = $data; | ||
} else { | ||
$options['query'] = $data; | ||
} | ||
} | ||
$response = $this->guzzleClient->request($method, $url, $options); | ||
|
||
if ($response->getStatusCode() != 200) { | ||
throw new CachetApiException('cachet.php: Bad response. Code: '.$response->getStatusCode()); | ||
} | ||
|
||
$data = json_decode($response->getBody(), true); | ||
|
||
if (!$data) { | ||
throw new CachetApiException('cachet.php: Could not decode JSON from '.$url); | ||
} | ||
|
||
return new ApiResponse($data, 'data'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
<?php | ||
namespace DivineOmega\CachetPHP\Client; | ||
|
||
namespace DivineOmega\CachetPHP\Client; | ||
|
||
interface IApiClient | ||
{ | ||
/** | ||
* @param string $url | ||
* @param mixed $data | ||
* @param string $method | ||
* @param bool $authorisationRequired | ||
* @return ApiResponse | ||
*/ | ||
function request($url, $data = null, $method = 'GET', $authorisationRequired = true); | ||
} | ||
/** | ||
* @param string $url | ||
* @param mixed $data | ||
* @param string $method | ||
* @param bool $authorisationRequired | ||
* | ||
* @return ApiResponse | ||
*/ | ||
public function request($url, $data = null, $method = 'GET', $authorisationRequired = true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?php | ||
|
||
namespace DivineOmega\CachetPHP\Exceptions; | ||
|
||
class CachetApiException extends \Exception | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
<?php | ||
namespace DivineOmega\CachetPHP\Objects; | ||
|
||
namespace DivineOmega\CachetPHP\Objects; | ||
|
||
use DivineOmega\CachetPHP\CachetInstance; | ||
|
||
abstract class ModelBase | ||
{ | ||
protected $cachetInstance = null; | ||
protected $cachetInstance = null; | ||
|
||
public function __construct($row, CachetInstance $cachetInstance = null) | ||
{ | ||
$this->cachetInstance = $cachetInstance; | ||
public function __construct($row, CachetInstance $cachetInstance = null) | ||
{ | ||
$this->cachetInstance = $cachetInstance; | ||
|
||
foreach ($row as $key => $value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
} | ||
foreach ($row as $key => $value) { | ||
$this->$key = $value; | ||
} | ||
} | ||
} |