From b36b3fbc27b0c47883880794add97ce25e9c8f5d Mon Sep 17 00:00:00 2001 From: Maxime St-Pierre Date: Fri, 2 Feb 2018 10:27:29 -0500 Subject: [PATCH] Refactor config with constant instead of strings --- controller/action/DeveloperActions.class.php | 8 ++++---- controller/action/OpsActions.class.php | 6 +++--- data/config.php.example | 19 ++++++++----------- lib/thirdparty/Asana.class.php | 2 +- lib/thirdparty/LBRY.class.php | 4 ++-- lib/thirdparty/Mailgun.class.php | 2 +- lib/thirdparty/Salesforce.class.php | 4 ++-- lib/thirdparty/Slack.class.php | 2 +- lib/tools/Config.class.php | 16 ++++++++++++++++ web/index.php | 2 +- 10 files changed, 39 insertions(+), 26 deletions(-) diff --git a/controller/action/DeveloperActions.class.php b/controller/action/DeveloperActions.class.php index aba841b1..547c688b 100644 --- a/controller/action/DeveloperActions.class.php +++ b/controller/action/DeveloperActions.class.php @@ -85,13 +85,13 @@ class DeveloperActions extends Actions Session::set(Session::KEY_DEVELOPER_RETURN_URL_SUCCESS, Request::getPostParam('returnUrl')); } - if (!Config::get('github_developer_credits_client_id')) + if (!Config::get(Config::GITHUB_DEVELOPER_CREDITS_CLIENT_ID)) { throw new Exception('no github client id'); } $gitHubParams = [ - 'client_id' => Config::get('github_developer_credits_client_id'), + 'client_id' => Config::get(Config::GITHUB_DEVELOPER_CREDITS_CLIENT_ID), 'redirect_uri' => Request::getHostAndProto() . '/quickstart/github/callback', 'scope' => 'user:email', 'allow_signup' => false @@ -112,8 +112,8 @@ class DeveloperActions extends Actions { $authResponseData = Curl::post('https://github.com/login/oauth/access_token', [ 'code' => $code, - 'client_id' => Config::get('github_developer_credits_client_id'), - 'client_secret' => Config::get('github_developer_credits_client_secret') + 'client_id' => Config::get(Config::GITHUB_DEVELOPER_CREDITS_CLIENT_ID), + 'client_secret' => Config::get(Config::GITHUB_DEVELOPER_CREDITS_CLIENT_SECRET) ], [ 'headers' => ['Accept: application/json'], 'json_response' => true diff --git a/controller/action/OpsActions.class.php b/controller/action/OpsActions.class.php index 5214d10e..08e5580f 100644 --- a/controller/action/OpsActions.class.php +++ b/controller/action/OpsActions.class.php @@ -40,7 +40,7 @@ class OpsActions extends Actions } $rawPost = file_get_contents('php://input'); - $secret = Config::get('github_key'); + $secret = Config::get(Config::GITHUB_KEY); if ($hash !== hash_hmac($algo, $rawPost, $secret)) { return NavActions::execute400(['error' => 'Hash does not match.']); @@ -78,8 +78,8 @@ class OpsActions extends Actions return NavActions::execute400(['error' => "Required params: log, name"]); } - $awsKey = Config::get('aws_log_access_key'); - $awsSecret = Config::get('aws_log_secret_key'); + $awsKey = Config::get(Config::AWS_LOG_ACCESS_KEY); + $awsSecret = Config::get(Config::AWS_LOG_SECRET_KEY); if (!$log || !$name) { diff --git a/data/config.php.example b/data/config.php.example index 9f6987a8..b918085d 100644 --- a/data/config.php.example +++ b/data/config.php.example @@ -1,13 +1,10 @@ - ['Authorization: Bearer ' . $apiKey], diff --git a/lib/thirdparty/LBRY.class.php b/lib/thirdparty/LBRY.class.php index 6232b092..a3d1325d 100644 --- a/lib/thirdparty/LBRY.class.php +++ b/lib/thirdparty/LBRY.class.php @@ -5,7 +5,7 @@ class LBRY { public static function getApiUrl($endpoint) { - return Config::get('lbry_api_server') . $endpoint; + return Config::get(Config::LBRY_API_SERVER) . $endpoint; } public static function getLBCtoUSDRate() @@ -29,4 +29,4 @@ class LBRY { return Curl::post(static::getApiUrl('/list/unsubscribe'), ['email' => $email], ['json_response' => true]); } -} \ No newline at end of file +} diff --git a/lib/thirdparty/Mailgun.class.php b/lib/thirdparty/Mailgun.class.php index 1fb79693..cf898cda 100644 --- a/lib/thirdparty/Mailgun.class.php +++ b/lib/thirdparty/Mailgun.class.php @@ -51,7 +51,7 @@ class Mailgun { return Curl::doCurl($method, self::BASE_URL . $endpoint, $data, [ 'headers' => [ - 'Authorization: Basic ' . base64_encode('api:' . Config::get('mailgun_api_key')) + 'Authorization: Basic ' . base64_encode('api:' . Config::get(Config::MAILGUN_API_KEY)) ], 'retry' => 3, ]); diff --git a/lib/thirdparty/Salesforce.class.php b/lib/thirdparty/Salesforce.class.php index e8ffb0db..fdb5f714 100644 --- a/lib/thirdparty/Salesforce.class.php +++ b/lib/thirdparty/Salesforce.class.php @@ -45,7 +45,7 @@ class Salesforce protected static function getApiUserPassword() { - $userpw = Config::get('salesforce_key') . ':' . Config::get('salesforce_secret'); + $userpw = Config::get(Config::SALESFORCE_KEY) . ':' . Config::get(Config::SALESFORCE_SECRET); if ($userpw[0] === ':' || substr($userpw, -1) === ':') { throw new SalesforceException('Salesforce key and/or secret not configured correctly'); @@ -77,4 +77,4 @@ class Salesforce class SalesforceException extends Exception { -} \ No newline at end of file +} diff --git a/lib/thirdparty/Slack.class.php b/lib/thirdparty/Slack.class.php index dd43e56b..616b4983 100644 --- a/lib/thirdparty/Slack.class.php +++ b/lib/thirdparty/Slack.class.php @@ -10,7 +10,7 @@ class Slack $e = Debug::exceptionToString($e); } - $slackErrorNotificationUrl = Config::get('slack_error_notification_url'); + $slackErrorNotificationUrl = Config::get(Config::SLACK_ERROR_NOTIFICATION_URL); if ($slackErrorNotificationUrl) { Curl::post($slackErrorNotificationUrl, ['text' => ($alert ? ' ' : '') . Request::getRelativeUri() . "\n" . $e], ['json_data' => true]); diff --git a/lib/tools/Config.class.php b/lib/tools/Config.class.php index 76cb6c90..32dfed23 100644 --- a/lib/tools/Config.class.php +++ b/lib/tools/Config.class.php @@ -4,6 +4,22 @@ class Config { const HELP_CONTACT_EMAIL = 'josh@lbry.io'; + //Constant to help with managing strings + const IS_PROD = "is_prod"; + const GITHUB_KEY = "github_key"; + const GITHUB_DEVELOPER_CREDITS_CLIENT_ID = "github_developer_credits_client_id"; + const GITHUB_DEVELOPER_CREDITS_CLIENT_SECRET = "github_developer_credits_client_secret"; + const LBRY_API_SERVER = "lbry_api_server"; + const MAILCHIMP_KEY = "mailchimp_key"; + const ASANA_KEY = "asana_key"; + const AWS_LOG_ACCESS_KEY = "aws_log_access_key"; + const AWS_LOG_SECRET_KEY = "aws_log_secret_key"; + const MAILGUN_API_KEY = "mailgun_api_key"; + const SALESFORCE_KEY = "salesforce_key"; + const SALESFORCE_SECRET = "salesforce_secret"; + const SLACK_ERROR_NOTIFICATION_URL = "slack_error_notification_url"; + + protected static $loaded = false; protected static $data = []; diff --git a/web/index.php b/web/index.php index 6212d6ee..118d9395 100644 --- a/web/index.php +++ b/web/index.php @@ -8,7 +8,7 @@ if (php_sapi_name() === 'cli-server' && is_file(__DIR__.preg_replace('#(\?.*)$#' include __DIR__ . '/../bootstrap.php'; -define('IS_PRODUCTION', Config::get('is_prod') === "yes"); +define('IS_PRODUCTION', Config::get(Config::IS_PROD) === true); ini_set('display_errors', IS_PRODUCTION ? 'off' : 'on'); error_reporting(IS_PRODUCTION ? 0 : (E_ALL | E_STRICT));