fix(CS): Formatted Code

Signed-off-by: Stuart McCulloch Anderson's avatarStuart McCulloch Anderson <stuart@nxfifteen.me.uk>
parent 1a933768
......@@ -10,7 +10,7 @@
namespace Core\Analytics;
require_once( dirname( __FILE__ ) . "/../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../autoloader.php");
use Core\Core;
use Exception;
......@@ -30,7 +30,8 @@ use Raven_ErrorHandler;
* @copyright 2017 Stuart McCulloch Anderson
* @license https://nxfifteen.me.uk/api/license/mit/ MIT
*/
class ErrorRecording {
class ErrorRecording
{
/**
* @var Raven_Client
......@@ -50,48 +51,23 @@ class ErrorRecording {
*
* @param Core $appClass
*/
public function __construct( $appClass ) {
if ( defined( 'SENTRY_DSN' ) ) {
public function __construct($appClass)
{
if (defined('SENTRY_DSN')) {
Raven_Autoloader::register();
$this->appClass = $appClass;
}
}
/**
* @return Raven_Client
*/
public function getSentryClient() {
if ( defined( 'SENTRY_DSN' ) ) {
if ( is_null( $this->sentryClient ) ) {
$this->sentryClient = ( new Raven_Client( SENTRY_DSN ) )
->setAppPath( __DIR__ )
->setRelease( $this->appClass->getSetting( "version", "0.0.0.1", true ) )
->setEnvironment( $this->appClass->getSetting( "environment", "development", false ) )
->setPrefixes( [ __DIR__ ] )
->install();
$this->sentryClient->user_context( [
'id' => sha1( gethostbyname( gethostname() ) . gethostname() . $this->appClass->getSetting( "ownerFuid",
"Unknown", false ) ),
'username' => $this->appClass->getSetting( "ownerFuid", "Unknown", false ),
'ip_address' => gethostbyname( gethostname() )
] );
}
return $this->sentryClient;
} else {
return null;
}
}
/**
* @return Raven_ErrorHandler
*/
public function getSentryErrorHandler() {
if ( defined( 'SENTRY_DSN' ) ) {
if ( is_null( $this->sentryErrorHandler ) ) {
$this->sentryErrorHandler = new Raven_ErrorHandler( $this->getSentryClient() );
public function getSentryErrorHandler()
{
if (defined('SENTRY_DSN')) {
if (is_null($this->sentryErrorHandler)) {
$this->sentryErrorHandler = new Raven_ErrorHandler($this->getSentryClient());
$this->sentryErrorHandler->registerExceptionHandler();
$this->sentryErrorHandler->registerErrorHandler();
$this->sentryErrorHandler->registerShutdownFunction();
......@@ -104,42 +80,49 @@ class ErrorRecording {
}
/**
* Log an exception to sentry
*
* @param Exception $exception The Exception object.
* @param array $data Additional attributes to pass with this event (see Sentry docs).
* @param null $logger
* @param null $vars
*
* @return int|null
* @return Raven_Client
*/
public function captureException( $exception, $data = null, $logger = null, $vars = null ) {
if ( defined( 'SENTRY_DSN' ) ) {
nxr( 0, "### Exception Recorded ###" );
public function getSentryClient()
{
if (defined('SENTRY_DSN')) {
if (is_null($this->sentryClient)) {
$this->sentryClient = (new Raven_Client(SENTRY_DSN))
->setAppPath(__DIR__)
->setRelease($this->appClass->getSetting("version", "0.0.0.1", true))
->setEnvironment($this->appClass->getSetting("environment", "development", false))
->setPrefixes([__DIR__])
->install();
return $this->getSentryClient()->captureException( $exception, $data, $logger, $vars );
$this->sentryClient->user_context([
'id' => sha1(gethostbyname(gethostname()) . gethostname() . $this->appClass->getSetting("ownerFuid",
"Unknown", false)),
'username' => $this->appClass->getSetting("ownerFuid", "Unknown", false),
'ip_address' => gethostbyname(gethostname())
]);
}
return $this->sentryClient;
} else {
return null;
}
}
/**
* Log a message to sentry
* Log an exception to sentry
*
* @param string $message The message (primary description) for the event.
* @param array $params params to use when formatting the message.
* @param array $data Additional attributes to pass with this event (see Sentry docs).
* @param bool $stack
* @param null $vars
* @param Exception $exception The Exception object.
* @param array $data Additional attributes to pass with this event (see Sentry docs).
* @param null $logger
* @param null $vars
*
* @return int|null
*/
public function captureMessage( $message, $params = [], $data = [], $stack = false, $vars = null ) {
nxr( 0, "[ERROR] $message" );
if ( defined( 'SENTRY_DSN' ) ) {
nxr( 0, "### Message Recorded ###" );
public function captureException($exception, $data = null, $logger = null, $vars = null)
{
if (defined('SENTRY_DSN')) {
nxr(0, "### Exception Recorded ###");
return $this->getSentryClient()->captureMessage( $message, $params, $data, $stack, $vars );
return $this->getSentryClient()->captureException($exception, $data, $logger, $vars);
} else {
return null;
}
......@@ -151,30 +134,54 @@ class ErrorRecording {
*
* @return int|null
*/
public function postDatabaseQuery( $medoo, $parameters ) {
if ( defined( 'SENTRY_DSN' ) ) {
public function postDatabaseQuery($medoo, $parameters)
{
if (defined('SENTRY_DSN')) {
$medoo_error = $medoo->error();
if ( $medoo_error[ 0 ] != 0000 ) {
if ($medoo_error[0] != 0000) {
$medoo_info = $medoo->info();
return $this->captureMessage( $medoo_error[ 2 ], [ 'database' ], [
return $this->captureMessage($medoo_error[2], ['database'], [
'level' => 'error',
'extra' => [
'method' => $parameters[ 'METHOD' ],
'method_line' => $parameters[ 'LINE' ],
'sql_server' => $medoo_info[ 'server' ],
'sql_client' => $medoo_info[ 'client' ],
'sql_driver' => $medoo_info[ 'driver' ],
'sql_version' => $medoo_info[ 'version' ],
'sql_connection' => $medoo_info[ 'connection' ],
'method' => $parameters['METHOD'],
'method_line' => $parameters['LINE'],
'sql_server' => $medoo_info['server'],
'sql_client' => $medoo_info['client'],
'sql_driver' => $medoo_info['driver'],
'sql_version' => $medoo_info['version'],
'sql_connection' => $medoo_info['connection'],
'sql_last_query' => $medoo->last(),
'php_version' => phpversion(),
'core_version' => $this->appClass->getSetting( "version", "0.0.0.1", true )
'php_version' => phpversion(),
'core_version' => $this->appClass->getSetting("version", "0.0.0.1", true)
]
] );
]);
}
}
return null;
}
/**
* Log a message to sentry
*
* @param string $message The message (primary description) for the event.
* @param array $params params to use when formatting the message.
* @param array $data Additional attributes to pass with this event (see Sentry docs).
* @param bool $stack
* @param null $vars
*
* @return int|null
*/
public function captureMessage($message, $params = [], $data = [], $stack = false, $vars = null)
{
nxr(0, "[ERROR] $message");
if (defined('SENTRY_DSN')) {
nxr(0, "### Message Recorded ###");
return $this->getSentryClient()->captureMessage($message, $params, $data, $stack, $vars);
} else {
return null;
}
}
}
......@@ -10,7 +10,7 @@
namespace Core\Analytics;
require_once( dirname( __FILE__ ) . "/../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../autoloader.php");
/**
* tracking
......@@ -23,7 +23,8 @@ require_once( dirname( __FILE__ ) . "/../../autoloader.php" );
* @copyright 2017 Stuart McCulloch Anderson
* @license https://nxfifteen.me.uk/api/license/mit/ MIT
*/
class UserAnalytics {
class UserAnalytics
{
/**
* @var \PiwikTracker
......@@ -40,41 +41,42 @@ class UserAnalytics {
*
* @codeCoverageIgnore
*
* @param int $trackingId Id site to be tracked
* @param string $api_url "http://example.org/piwik/" or "http://piwik.example.org/"
* @param int $trackingId Id site to be tracked
* @param string $api_url "http://example.org/piwik/" or "http://piwik.example.org/"
* If set, will overwrite PiwikTracker::$URL
* @param null $server_name
* @param null $request_uri
* @param null $server_name
* @param null $request_uri
*/
public function __construct( $trackingId, $api_url, $server_name = null, $request_uri = null ) {
if ( is_null( $server_name ) ) {
$server_name = $_SERVER[ 'SERVER_NAME' ];
public function __construct($trackingId, $api_url, $server_name = null, $request_uri = null)
{
if (is_null($server_name)) {
$server_name = $_SERVER['SERVER_NAME'];
}
if ( is_null( $request_uri ) ) {
$request_uri = $_SERVER[ 'REQUEST_URI' ];
if (is_null($request_uri)) {
$request_uri = $_SERVER['REQUEST_URI'];
}
$this->setSiteId( $trackingId );
$this->setSiteId($trackingId);
$this->PiwikTracker = new \PiwikTracker( $this->getSiteId(), $api_url );
$this->PiwikTracker = new \PiwikTracker($this->getSiteId(), $api_url);
if ( array_key_exists( "HTTPS", $_SERVER ) && $_SERVER[ "HTTPS" ] == "on" ) {
if (array_key_exists("HTTPS", $_SERVER) && $_SERVER["HTTPS"] == "on") {
$protocol = "https://";
} else {
$protocol = "http://";
}
$this->PiwikTracker->setUrl( $protocol . $server_name . $request_uri );
$this->PiwikTracker->setUrl($protocol . $server_name . $request_uri);
//Sets the Browser language.
if ( array_key_exists( "HTTP_ACCEPT_LANGUAGE", $_SERVER ) ) {
$lang = $_SERVER[ 'HTTP_ACCEPT_LANGUAGE' ];
$lang = explode( ',', $lang );
$this->PiwikTracker->setBrowserLanguage( $lang[ 0 ] );
if (array_key_exists("HTTP_ACCEPT_LANGUAGE", $_SERVER)) {
$lang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$lang = explode(',', $lang);
$this->PiwikTracker->setBrowserLanguage($lang[0]);
}
//Sets the user agent, used to detect OS and browser.
if ( array_key_exists( "HTTP_USER_AGENT", $_SERVER ) ) {
$this->PiwikTracker->setUserAgent( $_SERVER[ 'HTTP_USER_AGENT' ] );
if (array_key_exists("HTTP_USER_AGENT", $_SERVER)) {
$this->PiwikTracker->setUserAgent($_SERVER['HTTP_USER_AGENT']);
}
}
......@@ -83,7 +85,8 @@ class UserAnalytics {
*
* @param string $siteId
*/
private function setSiteId( $siteId ) {
private function setSiteId($siteId)
{
$this->siteId = $siteId;
}
......@@ -91,7 +94,8 @@ class UserAnalytics {
* @codeCoverageIgnore
* @return string
*/
private function getSiteId() {
private function getSiteId()
{
return $this->siteId;
}
......@@ -102,23 +106,25 @@ class UserAnalytics {
*
* @return mixed Response string or true if using bulk requests.
*/
public function endEvent( $documentTitle ) {
return $this->PiwikTracker->doTrackPageView( $documentTitle );
public function endEvent($documentTitle)
{
return $this->PiwikTracker->doTrackPageView($documentTitle);
}
/**
* Tracks an event
*
* @param string $category The Event Category (Videos, Music, Games...)
* @param string $action The Event's Action (Play, Pause, Duration, Add Playlist, Downloaded,
* @param string $category The Event Category (Videos, Music, Games...)
* @param string $action The Event's Action (Play, Pause, Duration, Add Playlist, Downloaded,
* Clicked...)
* @param string|bool $name (optional) The Event's object Name (a particular Movie name, or Song name, or
* @param string|bool $name (optional) The Event's object Name (a particular Movie name, or Song name, or
* File name...)
* @param float|bool $value (optional) The Event's value
* @param float|bool $value (optional) The Event's value
*
* @return mixed Response string or true if using bulk requests.
*/
public function track( $category, $action, $name = false, $value = false ) {
return $this->PiwikTracker->doTrackEvent( $category, $action, $name, $value );
public function track($category, $action, $name = false, $value = false)
{
return $this->PiwikTracker->doTrackEvent($category, $action, $name, $value);
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
......@@ -12,7 +12,7 @@ namespace Core\Rewards;
use Core\Core;
require_once( dirname( __FILE__ ) . "/../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../autoloader.php");
/**
* Modules
......@@ -41,7 +41,8 @@ class Delivery
* @param $AppClass
* @param $UserID
*/
public function __construct($AppClass, $UserID) {
public function __construct($AppClass, $UserID)
{
$this->setAppClass($AppClass);
$this->setUserID($UserID);
}
......@@ -51,7 +52,8 @@ class Delivery
* @param string $state
* @param string $rewardKey
*/
public function deliver($recordReward, $state, $rewardKey) {
public function deliver($recordReward, $state, $rewardKey)
{
$this->recordDevlivery($recordReward, $state, $rewardKey);
}
......@@ -60,7 +62,8 @@ class Delivery
* @param $state
* @param $rewardKey
*/
protected function recordDevlivery($recordReward, $state, $rewardKey) {
protected function recordDevlivery($recordReward, $state, $rewardKey)
{
$db_prefix = $this->getAppClass()->getSetting("db_prefix", null, false);
if (!array_key_exists("rmid", $recordReward)) {
......
......@@ -12,7 +12,7 @@ namespace Core\Rewards\Delivery;
use Core\Rewards\Delivery;
require_once( dirname( __FILE__ ) . "/../../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../../autoloader.php");
/**
* Modules
......@@ -31,7 +31,8 @@ class Minecraft extends Delivery
* @param string $state
* @param string $rewardKey
*/
public function deliver($recordReward, $state, $rewardKey) {
public function deliver($recordReward, $state, $rewardKey)
{
$minecraftUsername = $this->getAppClass()->getUserSetting($this->getUserID(), "minecraft_username", null);
if (!is_null($minecraftUsername) && !is_numeric($minecraftUsername)) {
......
......@@ -12,7 +12,7 @@ namespace Core\Rewards\Delivery;
use Core\Rewards\Delivery;
require_once( dirname( __FILE__ ) . "/../../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../../autoloader.php");
/**
* Modules
......@@ -30,7 +30,8 @@ class Wordpress extends Delivery
* @param string $state
* @param string $rewardKey
*/
public function deliver($recordReward, $state, $rewardKey) {
public function deliver($recordReward, $state, $rewardKey)
{
$user_wp_id = $this->getAppClass()->getUserSetting($this->getUserID(), "wp_user_id");
if (is_null($user_wp_id)) {
nxr(0, "User doesnt have a WP ID");
......
......@@ -12,7 +12,7 @@ namespace Core\Rewards\Delivery;
use Core\Rewards\Delivery;
require_once( dirname( __FILE__ ) . "/../../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../../autoloader.php");
/**
* Modules
......@@ -30,7 +30,8 @@ class Xp extends Delivery
* @param string $state
* @param string $rewardKey
*/
public function deliver($xp, $state, $rewardKey) {
public function deliver($xp, $state, $rewardKey)
{
$db_prefix = $this->getAppClass()->getSetting("db_prefix", null, false);
if (!$this->getAppClass()->getDatabase()->has($db_prefix . "users_xp", ['fuid' => $this->getUserID()])) {
$this->getAppClass()->getDatabase()->insert($db_prefix . "users_xp", ["xp" => 0, "fuid" => $this->getUserID()]);
......
......@@ -13,7 +13,7 @@ namespace Core\Rewards;
use Core\Core;
use DateTime;
require_once( dirname( __FILE__ ) . "/../../autoloader.php" );
require_once(dirname(__FILE__) . "/../../autoloader.php");
/**
* Modules
......@@ -31,19 +31,15 @@ class Modules
* @var array
*/
protected $eventDetails;
private $createNewAwards = false;
/**
* @var String
*/
protected $UserID;
/**
* @var Rewards
*/
protected $RewardsClass;
private $createNewAwards = false;
/**
* @var Core
*/
......@@ -54,61 +50,13 @@ class Modules
* @param $AppClass
* @param $UserID
*/
public function __construct($AppClass, $UserID) {
public function __construct($AppClass, $UserID)
{
$this->setAppClass($AppClass);
$this->setUserID($UserID);
$this->setRewardsClass(new Rewards($AppClass, $UserID));
}
/**
* @return Rewards
*/
protected function getRewardsClass()
{
return $this->RewardsClass;
}
/**
* @param Rewards $RewardsClass
*/
protected function setRewardsClass($RewardsClass)
{
$this->RewardsClass = $RewardsClass;
}
/**
* @todo Consider test case
* @return String
*/
protected function getUserID()
{
return $this->UserID;
}
/**
* @todo Consider test case
*
* @param String $UserID
*/
protected function setUserID($UserID)
{
$this->UserID = $UserID;
}
/**
* @param Core $AppClass
*/
protected function setAppClass($AppClass) {
$this->AppClass = $AppClass;
}
/**
* @return Core
*/
protected function getAppClass() {
return $this->AppClass;
}
/**
* @return array|string|object
*/
......@@ -124,7 +72,8 @@ class Modules
* @param null $rewardKey
* @return bool
*/
protected function checkDB($cat, $event, $score, $rewardKey = null) {
protected function checkDB($cat, $event, $score, $rewardKey = null)
{
$db_prefix = $this->getAppClass()->getSetting("db_prefix", null, false);
if (is_null($rewardKey)) {
......@@ -207,7 +156,40 @@ class Modules
return false;
}
protected function cleanupQueue() {
/**
* @return Core
*/
protected function getAppClass()
{
return $this->AppClass;
}
/**
* @param Core $AppClass
*/
protected function setAppClass($AppClass)
{
$this->AppClass = $AppClass;
}
/**
* @return Rewards
*/
protected function getRewardsClass()
{
return $this->RewardsClass;
}
/**
* @param Rewards $RewardsClass
*/
protected function setRewardsClass($RewardsClass)
{
$this->RewardsClass = $RewardsClass;
}
protected function cleanupQueue()
{
$prefix = $this->getAppClass()->getSetting("db_prefix", null, false);
$this->getAppClass()->getDatabase()->delete($prefix . "reward_queue",
["AND" => ["fuid" => $this->getUserID(), "state" => "delivered", "date[<]" => date('Y-m-d', strtotime(' -14 days'))]]);
......@@ -217,4 +199,23 @@ class Modules
]);
}
/**
* @todo Consider test case
* @return String
*/
protected function getUserID()
{
return $this->UserID;
}
/**
* @todo Consider test case
*
* @param String $UserID
*/
protected function setUserID($UserID)
{
$this->UserID = $UserID;
}
}
\ No newline at end of file
......@@ -32,7 +32,8 @@ class DefaultModule extends Modules
/**
* @param $eventDetails
*/
public function trigger($eventDetails) {
public function trigger($eventDetails)
{
$this->setEventDetails($eventDetails);
$eventDetails = $this->getEventDetails();
......
......@@ -11,7 +11,6 @@
namespace Core\Rewards\Modules;
use Core\Rewards\Modules;
use DateTime;
require_once(dirname(__FILE__) . "/../Modules.php");
require_once(dirname(__FILE__) . "/../../../autoloader.php");
......
......@@ -33,7 +33,8 @@ class FitbitLoggedActivity extends Modules
/**
* @param $eventDetails
*/
public function trigger($eventDetails) {
public function trigger($eventDetails)
{
$this->setEventDetails($eventDetails);
$activity = $this->getEventDetails();
......
......@@ -11,7 +11,6 @@
namespace Core\Rewards\Modules;
use Core\Rewards\Modules;
use DateTime;
require_once(dirname(__FILE__) . "/../Modules.php");
require_once(dirname(__FILE__) . "/../../../autoloader.php");
......@@ -33,7 +32,8 @@ class FitbitStreak extends Modules
/**
* @param $eventDetails
*/
public function trigger($eventDetails) {
public function trigger($eventDetails)
{
$this->setEventDetails($eventDetails);
$eventDetails = $this->getEventDetails();
......
......@@ -33,7 +33,8 @@ class FitbitTracker extends Modules
/**
* @param $eventDetails
*/
public function trigger($eventDetails) {
public function trigger($eventDetails)
{
$this->setEventDetails($eventDetails);
$eventDetails = $this->getEventDetails();
......@@ -79,10 +80,10 @@ class FitbitTracker extends Modules
$rewardKey = sha1($yesterday . $eventDetails['trigger'] . $hundredth);
if (!$this->getRewardsClass()->alreadyAwarded(sha1($rewardKey . "db"))) {
$this->checkDB("hundredth", $eventDetails['trigger'], $hundredth, sha1($rewardKey."db"));
$this->checkDB("hundredth", $eventDetails['trigger'], $hundredth, sha1($rewardKey . "db"));
}
if (!$this->getRewardsClass()->alreadyAwarded($rewardKey)) {
$xp = round(($eventDetails['value'] - 5000) / 2 , 0);
$xp = round(($eventDetails['value'] - 5000) / 2, 0);
if ($xp > 0) {
$this->getRewardsClass()->giveUserXp($xp, $rewardKey);
$this->getRewardsClass()->notifyUser("fa fa-git", "bg-success", "Step Mark", "You took " . $recordedValue . " steps yesterday", $xp . "XP", '+24 hours');
......@@ -106,6 +107,17 @@ class FitbitTracker extends Modules
];
}
/**
* @param string $goal
* @param string $value
*
* @return bool
*/
private function crushedGoal($goal, $value)
{
return $this->reachedGoal($goal, $value, 2);
}
/**
* @param string $goal
* @param int $value
......@@ -152,15 +164,4 @@ class FitbitTracker extends Modules
{
return $this->reachedGoal($goal, $value, 1.5);
}
/**
* @param string $goal