forked from homer6/sfAltumoPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
russpoutine
committed
Jun 1, 2012
1 parent
cd85798
commit 7ca2ed1
Showing
9 changed files
with
1,291 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<?php | ||
|
||
|
||
namespace Geckoboard; | ||
|
||
|
||
class Request extends \sfWebRequest { | ||
|
||
|
||
/** | ||
* @var \Altumo\Http\IncomingHttpRequest | ||
*/ | ||
protected $incoming_http_request = null; | ||
|
||
|
||
/** | ||
* Class constructor. | ||
* | ||
* @see initialize() | ||
*/ | ||
public function __construct( \sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array() ){ | ||
|
||
$this->initialize( $dispatcher, $parameters, $attributes, $options ); | ||
|
||
$this->setIncomingHttpRequest( new \Altumo\Http\IncomingHttpRequest() ); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Setter for the incoming_http_request field | ||
* | ||
* @param \Altumo\Http\IncomingHttpRequest $incoming_http_request | ||
*/ | ||
protected function setIncomingHttpRequest( $incoming_http_request ){ | ||
|
||
$this->incoming_http_request = $incoming_http_request; | ||
|
||
} | ||
|
||
|
||
/** | ||
* Authenticates user by API key | ||
* | ||
* @param sfActions $action | ||
* | ||
* @return void | ||
* | ||
* @throws \Exception if validation fails | ||
*/ | ||
public function authenticate(){ | ||
|
||
//require SSL | ||
if ( ! $this->isSecure() ) { | ||
throw new \Exception( "Please make a SSL request" ); | ||
} | ||
|
||
//authenticate via the API key, if provided | ||
$api_key = $this->getHttpRequestHeader( 'Authorization', null ); | ||
|
||
// check if authorization string is set | ||
if( is_null($api_key) ){ | ||
// if no authorization string set, | ||
|
||
// fail | ||
throw new \Exception( "Please provide an API key" ); | ||
|
||
} else { | ||
// if authorization string set | ||
|
||
// extract api key from authorization string | ||
if( ! preg_match('/\\s*Basic\\s+(.*?)\\s*$/im', $api_key, $regs) ){ | ||
// if format not recognized, | ||
|
||
// fail | ||
throw new \Exception('Unknown or inactive API user.'); | ||
|
||
} else { | ||
// if api key extracted, | ||
|
||
$api_key = $regs[1]; | ||
|
||
$api_key = base64_decode($api_key); | ||
if ( ! $api_key) throw new \Exception("Unknown or inactive API user"); | ||
|
||
$api_key = $this->trimGeckoboardAuthorization($api_key); | ||
|
||
// find user by api key | ||
$user = \UserQuery::create() | ||
->filterByApiKey( $api_key ) | ||
->filterByActive( true ) | ||
->findOne() | ||
; | ||
|
||
// if user not found | ||
if( ! $user ){ | ||
|
||
// fail | ||
throw new \Exception('Unknown or inactive API user.'); | ||
} | ||
|
||
// get sf guard user | ||
$sf_guard_user = $user->getsfGuardUser(); | ||
|
||
// check if sf guard user is active | ||
if( $sf_guard_user->getIsActive() ){ | ||
// if sf guard user active, | ||
|
||
// sign in | ||
\sfContext::getInstance()->getUser()->signIn( $sf_guard_user, false ); | ||
|
||
} else { | ||
// if sf guard user is not active, | ||
|
||
// fail | ||
throw new \Exception('Unknown or inactive API user.'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Gets a specified HTTP Request header. | ||
* Return null or $default, if not found. | ||
* | ||
* @param string $name | ||
* @param mixed $default | ||
* | ||
* @return string | ||
*/ | ||
protected function getHttpRequestHeader( $name, $default = null ){ | ||
|
||
return $this->getIncomingHttpRequest()->getHeader( $name, $default ); | ||
|
||
} | ||
|
||
|
||
/** | ||
* Getter for the incoming_http_request field on this ApiRequest. | ||
* | ||
* @return \Altumo\Http\IncomingHttpRequest | ||
*/ | ||
protected function getIncomingHttpRequest(){ | ||
|
||
return $this->incoming_http_request; | ||
|
||
} | ||
|
||
|
||
/** | ||
* Geckoboard will send input as [base64 encoded api key]:X, | ||
* this method returns the api key part | ||
* | ||
* @param string $value | ||
* | ||
* @return string | ||
*/ | ||
protected function trimGeckoboardAuthorization( $value ) | ||
{ | ||
return str_replace(':X', '', $value); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
|
||
namespace Geckoboard; | ||
|
||
|
||
class Response extends \sfWebResponse | ||
{ | ||
|
||
public function getContent() | ||
{ | ||
return json_encode( $this->content ); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
|
||
namespace Geckoboard\Widget; | ||
|
||
|
||
abstract class AbstractWidget { | ||
|
||
|
||
/** | ||
* Returns data to be sent to the response | ||
* | ||
* @return \stdClass | ||
*/ | ||
protected function getContent() | ||
{ | ||
return new \stdClass(); | ||
} | ||
|
||
|
||
/** | ||
* Export data to JSON | ||
* | ||
* @return string | ||
*/ | ||
public function toJSON() | ||
{ | ||
return json_encode( $this->getContent() ); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
|
||
namespace Geckoboard\Widget; | ||
|
||
|
||
class FunnelWidget extends \Geckoboard\Widget\AbstractWidget | ||
{ | ||
|
||
protected $items = array(); | ||
protected $is_reverse = false; | ||
protected $is_percentage_enabled = false; | ||
|
||
|
||
/** | ||
* Adds an item | ||
* | ||
* @param double $value | ||
* @param string $label | ||
* | ||
* @throws \Exception if $value doesn't validate | ||
* @throws \Exception if $label doesn't validate | ||
* | ||
* @return \Geckoboard\Widget\FunnelWidget | ||
*/ | ||
public function addItem( $value, $label ) | ||
{ | ||
$value = \Altumo\Validation\Numerics::assertDouble( $value ); | ||
|
||
$label = \Altumo\Validation\Strings::assertString( $label ); | ||
|
||
$item = new \stdClass(); | ||
$item->value = $value; | ||
$item->label = $label; | ||
|
||
$this->items []= $item; | ||
|
||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns an array of items as \stdClass objects | ||
* | ||
* @return \stdClass[] | ||
*/ | ||
protected function getItems() | ||
{ | ||
return $this->items; | ||
} | ||
|
||
|
||
/** | ||
* Determines whether (true) the widget should be rendered as reverse, | ||
* i.e. with red at the top and green at the bottom, or (false) with | ||
* green at the top and red at the bottom. | ||
* | ||
* @param bool $reverse | ||
* | ||
* @throws \Exception if value doesn't validate | ||
* | ||
* @return \Geckoboard\Widget\FunnelWidget | ||
*/ | ||
public function setReverse( $reverse = true ) | ||
{ | ||
$this->is_reverse = \Altumo\Validation\Booleans::assertLooseBoolean( $reverse ); | ||
|
||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* Returns true if the widget should be rendered as reverse, | ||
* i.e. with red at the top and green at the bottom, or false | ||
* if with green at the top and red at the bottom. | ||
* | ||
* @return bool | ||
*/ | ||
protected function isReverse() | ||
{ | ||
return $this->is_reverse; | ||
} | ||
|
||
|
||
/** | ||
* Determines whether or not percentages should be shown in the widget | ||
* | ||
* @param bool $enabled | ||
* | ||
* @return \Geckoboard\Widget\FunnelWidget | ||
* | ||
* @throws \Exception if value doesn't validate | ||
*/ | ||
public function setPercentageEnabled( $enabled = true ) | ||
{ | ||
$this->is_percentage_enabled = \Altumo\Validation\Booleans::assertLooseBoolean( $enabled ); | ||
|
||
return $this; | ||
} | ||
|
||
|
||
/** | ||
* @return bool | ||
*/ | ||
protected function isPercentageEnabled() | ||
{ | ||
return $this->is_percentage_enabled; | ||
} | ||
|
||
|
||
/** | ||
* (non-PHPdoc) | ||
* @see Geckoboard\Widget.AbstractWidget::getContent() | ||
*/ | ||
public function getContent() | ||
{ | ||
$content = new \stdClass(); | ||
|
||
// determine whether red should be at bottom (reverse) or top (not) | ||
if ($this->isReverse()) $content->type = 'reverse'; | ||
|
||
// determine whether percentages should be shown | ||
$content->percentage = $this->isPercentageEnabled() ? 'show' : 'hide'; | ||
|
||
$content->item = array(); | ||
foreach( $this->getItems() as $item ) $content->item []= $item; | ||
|
||
return $content; | ||
} | ||
|
||
} |
Oops, something went wrong.