Skip to content

Commit

Permalink
Refactoring/improvements on top of recent cleanup/abstraction work
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Hall committed May 27, 2016
1 parent d7023fd commit f66a57e
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 50 deletions.
18 changes: 10 additions & 8 deletions examples.php
Expand Up @@ -2,9 +2,18 @@

require_once 'vendor/autoload.php';

use \DivineOmega\CachetPHP\Client\ApiV1Client;
use \DivineOmega\CachetPHP\Factories\CachetInstanceFactory;

$cachetInstance = CachetInstanceFactory::create('https://demo.cachethq.io/api/v1/', '9yMHsdioQosnyVK4iCVR');
$client = new ApiV1Client('https://demo.cachethq.io/api', '9yMHsdioQosnyVK4iCVR');
$cachetInstance = CachetInstanceFactory::create($client);

// Check if Cachet instance is working correctly
if ($cachetInstance->isWorking()) {
echo "\n";
echo '*** Cachet instance working fine! ***';
echo "\n";
}

// Add component
echo "\n";
Expand All @@ -17,13 +26,6 @@
echo $component->id.' - '.$component->name.' - '.$component->description.' - '.$component->status;
echo "\n";

// Check if Cachet instance is working correctly
if ($cachetInstance->isWorking()) {
echo "\n";
echo '*** Cachet instance working fine! ***';
echo "\n";
}

// Get components
$components = $cachetInstance->getAllComponents();

Expand Down
34 changes: 11 additions & 23 deletions src/CachetInstance.php
Expand Up @@ -10,24 +10,11 @@

class CachetInstance
{
public $guzzleClient;
private $client;

private $baseUrl;
private $apiToken;

public function __construct($baseUrl, $apiToken)
public function __construct($client)
{
if (!$baseUrl) {
throw new \Exception('You must specify the base URL for your Cachet instance.');
}

$this->baseUrl = $baseUrl;
$this->apiToken = $apiToken;

$this->guzzleClient = new Client([
'base_uri' => $baseUrl,
'timeout' => 3.0,
]);
$this->client = $client;
}

public function getApiToken()
Expand All @@ -45,22 +32,23 @@ public function getAuthHeaders()
];
}

public function client()
{
return $this->client;
}

public function ping()
{
$response = $this->guzzleClient->get('ping');
$response = $this->client->request('ping', 'GET');

if ($response->getStatusCode() != 200) {
throw new \Exception('cachet.php: Bad response.');
}

$data = json_decode($response->getBody());
$data = $response->getData();

if (!$data) {
throw new \Exception('cachet.php: Could not decode JSON from '.$url);
}

if (isset($data->data)) {
$data = $data->data;
throw new \Exception('cachet.php: No data received from ping.');
}

return $data;
Expand Down
9 changes: 8 additions & 1 deletion src/Client/ApiResponse.php
Expand Up @@ -5,10 +5,12 @@
class ApiResponse
{
private $data;
private $statusCode;

public function __construct(array $data)
public function __construct(array $data, $statusCode)
{
$this->data = $data;
$this->statusCode = $statusCode;
}

public function getData()
Expand All @@ -20,4 +22,9 @@ public function getMeta()
{
return isset($this->data['meta']) ? $this->data['meta'] : null;
}

public function getStatusCode()
{
return $this->statusCode;
}
}
4 changes: 3 additions & 1 deletion src/Client/ApiV1Client.php
Expand Up @@ -57,6 +57,8 @@ public function request($url, $data = null, $method = 'GET', $authorisationRequi
throw new CachetApiException('cachet.php: Could not decode JSON from '.$url);
}

return new ApiResponse($data, 'data');
$statusCode = $response->getStatusCode();

return new ApiResponse($data, $statusCode);
}
}
16 changes: 8 additions & 8 deletions src/Factories/CachetElementFactory.php
Expand Up @@ -3,10 +3,10 @@
namespace DivineOmega\CachetPHP\Factories;

use DivineOmega\CachetPHP\CachetInstance;
use DivineOmega\CachetPHP\Objects\Component;
use DivineOmega\CachetPHP\Objects\Incident;
use DivineOmega\CachetPHP\Objects\Metric;
use DivineOmega\CachetPHP\Objects\Subscriber;
use DivineOmega\CachetPHP\Models\Component;
use DivineOmega\CachetPHP\Models\Incident;
use DivineOmega\CachetPHP\Models\Metric;
use DivineOmega\CachetPHP\Models\Subscriber;

abstract class CachetElementFactory
{
Expand Down Expand Up @@ -54,19 +54,19 @@ public static function create(CachetInstance $cachetInstance, $type, $data)
switch ($type) {

case 'components':
$toReturn = new Component($cachetInstance, $row);
$toReturn = new Component($row, $cachetInstance);
break;

case 'incidents':
$toReturn = new Incident($cachetInstance, $row);
$toReturn = new Incident($row, $cachetInstance);
break;

case 'metrics':
$toReturn = new Metric($cachetInstance, $row);
$toReturn = new Metric($row, $cachetInstance);
break;

case 'subscribers':
$toReturn = new Subscriber($cachetInstance, $row);
$toReturn = new Subscriber($row, $cachetInstance);
break;

default:
Expand Down
5 changes: 3 additions & 2 deletions src/Factories/CachetInstanceFactory.php
Expand Up @@ -3,11 +3,12 @@
namespace DivineOmega\CachetPHP\Factories;

use DivineOmega\CachetPHP\CachetInstance;
use DivineOmega\CachetPHP\Client\IApiClient;

abstract class CachetInstanceFactory
{
public static function create($baseUrl, $apiToken)
public static function create(IApiClient $client)
{
return new CachetInstance($baseUrl, $apiToken);
return new CachetInstance($client);
}
}
2 changes: 1 addition & 1 deletion src/Factories/MetricPointFactory.php
Expand Up @@ -3,7 +3,7 @@
namespace DivineOmega\CachetPHP\Factories;

use DivineOmega\CachetPHP\CachetInstance;
use DivineOmega\CachetPHP\Objects\MetricPoint;
use DivineOmega\CachetPHP\Models\MetricPoint;

abstract class MetricPointFactory
{
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Component.php
@@ -1,6 +1,6 @@
<?php

namespace DivineOmega\CachetPHP\Objects;
namespace DivineOmega\CachetPHP\Models;

class Component extends ModelBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Incident.php
@@ -1,6 +1,6 @@
<?php

namespace DivineOmega\CachetPHP\Objects;
namespace DivineOmega\CachetPHP\Models;

class Incident extends ModelBase
{
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Metric.php
@@ -1,6 +1,6 @@
<?php

namespace DivineOmega\CachetPHP\Objects;
namespace DivineOmega\CachetPHP\Models;

use DivineOmega\CachetPHP\Factories\MetricPointFactory;

Expand Down
2 changes: 1 addition & 1 deletion src/Models/MetricPoint.php
@@ -1,6 +1,6 @@
<?php

namespace DivineOmega\CachetPHP\Objects;
namespace DivineOmega\CachetPHP\Models;

use DivineOmega\CachetPHP\CachetInstance;

Expand Down
2 changes: 1 addition & 1 deletion src/Models/ModelBase.php
@@ -1,6 +1,6 @@
<?php

namespace DivineOmega\CachetPHP\Objects;
namespace DivineOmega\CachetPHP\Models;

use DivineOmega\CachetPHP\CachetInstance;

Expand Down
2 changes: 1 addition & 1 deletion src/Models/Subscriber.php
@@ -1,6 +1,6 @@
<?php

namespace DivineOmega\CachetPHP\Objects;
namespace DivineOmega\CachetPHP\Models;

class Subscriber extends ModelBase
{
Expand Down

0 comments on commit f66a57e

Please sign in to comment.