Skip to content

Commit

Permalink
Merge pull request #31 from DivineOmega/analysis-qMPM73
Browse files Browse the repository at this point in the history
Applied fixes from StyleCI
  • Loading branch information
DivineOmega committed May 27, 2016
2 parents 9e0b00a + 6b24ab2 commit d7023fd
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 89 deletions.
2 changes: 1 addition & 1 deletion src/CachetInstance.php
Expand Up @@ -68,7 +68,7 @@ public function ping()

public function isWorking()
{
return ($this->ping() == 'Pong!');
return $this->ping() == 'Pong!';
}

public function getAllComponents($sort = null, $order = null)
Expand Down
27 changes: 15 additions & 12 deletions src/Client/ApiResponse.php
@@ -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;
}
}
107 changes: 54 additions & 53 deletions src/Client/ApiV1Client.php
@@ -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');
}
}
21 changes: 11 additions & 10 deletions src/Client/IApiClient.php
@@ -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);
}
4 changes: 2 additions & 2 deletions src/Exceptions/CachetApiException.php
@@ -1,7 +1,7 @@
<?php

namespace DivineOmega\CachetPHP\Exceptions;

class CachetApiException extends \Exception
{

}
}
2 changes: 1 addition & 1 deletion src/Factories/MetricPointFactory.php
Expand Up @@ -10,7 +10,7 @@ abstract class MetricPointFactory
public static function getAll(CachetInstance $cachetInstance, $metric, $sort = null, $order = null)
{
$response = $cachetInstance->client()->request('metrics/'.$metric->id.'/points',
['sort' => $sort, 'order' => $order, ]);
['sort' => $sort, 'order' => $order]);

$toReturn = [];

Expand Down
20 changes: 10 additions & 10 deletions src/Models/ModelBase.php
@@ -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;
}
}
}

0 comments on commit d7023fd

Please sign in to comment.