Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: php
php:
- "5.4"
- "5.3"
- "5.2"
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build Status](https://secure.travis-ci.org/aeroio/php-client.png)](http://travis-ci.org/aeroio/php-client)

PHP Client for Aero.cx
====================================

Expand Down
10 changes: 10 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<phpunit backupGlobals="false"
colors="true"
backupStaticAttributes="false"
syntaxCheck="false">
<testsuites>
<testsuite name="test">
<directory suffix=".php">test</directory>
</testsuite>
</testsuites>
</phpunit>
97 changes: 97 additions & 0 deletions src/AeroIO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* Aero.io API client for PHP
*
* @copyright Copyright 2012, aero.io (http://aero.io)
* @license The MIT License
*/

require_once 'src/ExceptionHandler.php';
require_once 'src/Connection.php';
require_once 'src/engines/Curl.php';

/**
* AeroIO class.
*
* Base class for the client.
*/
class AeroIO {

/**
* Type of the engine used for the request execution.
*
* @var object
*/
public static $engine;

/**
* Url address for the request.
*
* @var string
*/
public static $site;

/**
* Auth token for the user authentication.
*
* @var string
*/
public static $auth_token;

/**
* Sid for the user authentication.
*
* @var string
*/
public static $sid;

/**
* Id of the project to be handled.
*
* @var integer
*/
public static $project;

/**
* Set up the options for the request.
*
* @param array $options
* @return void
*/
public static function configure(Array $options = array()) {
foreach ($options as $key => $value) {
self::$$key = $value;
}

if (!self::$engine) self::$engine = new Aero_Curl();

self::setOptions();
}

/**
* Set exception handler for certain project.
*
* @return void
*/
public static function handleExceptions() {
Aero_ExceptionHandler::for_project(self::$project);
}

/**
* Set the options for the request.
*
* @return void
*/
protected static function setOptions() {
Aero_Connection::$site = self::$site;
Aero_Connection::$engine = self::$engine;
Aero_Connection::$credentials = array(
'auth_token' => self::$auth_token,
'sid' => self::$sid
);
}
}

?>

58 changes: 43 additions & 15 deletions src/Connection.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
<?php
require_once 'src/UrlBuilder.php';

/**
* Aero.io API client for PHP
*
* @copyright Copyright 2012, aero.io (http://aero.io)
* @license The MIT License
*/

/**
* Aero_Connection class.
*
* This class connects the components and executes them.
*/
class Aero_Connection {
public static function persist($resource, $type) {
$request = Aero_Request($type, $resource, $credentials);
//$url = UrlBuilder::assemble($resource);

//$params = array(
//'type' => $type,
//'url' => $url,
//'auth_token' => self::$credentials['auth_token'],
//'sid' => self::$credentials['sid'],
//'attributes' => $resource
//);
/**
* Base url of the application.
*
* @static string
*/
public static $site = 'https://aero.io/api/v1';

/**
* Credentials to be used for user authorization.
*
* @static array
*/
public static $credentials;

//$request = new Aero_Request($params);
/**
* Engine to be used for the request execution.
*
* @static object
*/
public static $engine;

/**
* Create a request object, to be used by the engine for execution and
* handle the response.
*
* @param object $resource
* @param string $type
* @return object
*/
public static function persist($resource, $type) {
$request = new Aero_Request($type, $resource, self::$credentials, self::$site);

$engine = new self::$engine();
$response = $engine->execute($request);

return Aero_Response::handle($response);
}

public static $engine;
public static $credentials;
}

?>
56 changes: 56 additions & 0 deletions src/ExceptionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/**
* Aero.io API client for PHP
*
* @copyright Copyright 2012, aero.io (http://aero.io)
* @license The MIT License
*/

require_once 'src/resources/Error.php';

/**
* Aero_ExceptionHandler class.
*
* Set up the handling of errors for certain project.
*/
class Aero_ExceptionHandler {

/**
* Id of the project to be handled.
*
* @var integer
*/
public static $project_id;

/**
* Save an error belonging to certain project.
*
* @param object $exception
* @return void
*/
public function handle($exception) {
$params = array(
'project_id' => self::$project_id,
'message' => $exception->getMessage(),
'resolved' => false
);

$error = new Aero_Error($params);
$error->save();
}

/**
* Set exception handler for certain project.
*
* @param integer $id
* @return void
*/
public static function for_project($id) {
self::$project_id = $id;

set_exception_handler(array(get_called_class(), "handle"));
}
}

?>
70 changes: 70 additions & 0 deletions src/Exceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't be lazy. Add comments :)


/**
* Aero.io API client for PHP
*
* @copyright Copyright 2012, aero.io (http://aero.io)
* @license The MIT License
*/

/**
* Client side exceptions.
*/
class ClientException extends Exception {}

/**
* Server side exceptions.
*/
class ServerException extends Exception {}

/**
* Exceptions, when the connection has timed out or something uncaught happened.
*/
class ConnectionException extends Exception {}

/**
* Exception, when new URI was assigned.
*/
class RedirectionException extends ConnectionException {}

/**
* Exception, when the request cannot be understood by the server.
*/
class BadRequestException extends ClientException {}

/**
* Exception, when user authentications is required.
*/
class UnauthorizedException extends ClientException {}

/**
* Exception, when the server is refusing to fulfill the request.
*/
class ForbiddenAccessException extends ClientException {}

/**
* Exception, when nothing mathing is found.
*/
class ResourceNotFoundException extends ClientException {}

/**
* Exception, when the method type is not allowed.
*/
class MethodNotAllowedException extends ClientException {}

/**
* Exception, when the request cannot be fulfiled due to a resource conflict.
*/
class ResourceConflictException extends ClientException {}

/**
* Exception, when the resource is no longer available.
*/
class ResourceGoneException extends ClientException {}

/**
* Exception, when there were semantic errors in the request.
*/
class ResourceInvalidException extends ClientException {}

?>
Loading