Refactor config with constant instead of strings

This commit is contained in:
Maxime St-Pierre 2018-02-02 10:27:29 -05:00
parent 2836ba6d4b
commit ba40e13677
10 changed files with 39 additions and 26 deletions

View file

@ -85,13 +85,13 @@ class DeveloperActions extends Actions
Session::set(Session::KEY_DEVELOPER_RETURN_URL_SUCCESS, Request::getPostParam('returnUrl')); 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'); throw new Exception('no github client id');
} }
$gitHubParams = [ $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', 'redirect_uri' => Request::getHostAndProto() . '/quickstart/github/callback',
'scope' => 'user:email', 'scope' => 'user:email',
'allow_signup' => false 'allow_signup' => false
@ -112,8 +112,8 @@ class DeveloperActions extends Actions
{ {
$authResponseData = Curl::post('https://github.com/login/oauth/access_token', [ $authResponseData = Curl::post('https://github.com/login/oauth/access_token', [
'code' => $code, 'code' => $code,
'client_id' => Config::get('github_developer_credits_client_id'), 'client_id' => Config::get(Config::GITHUB_DEVELOPER_CREDITS_CLIENT_ID),
'client_secret' => Config::get('github_developer_credits_client_secret') 'client_secret' => Config::get(Config::GITHUB_DEVELOPER_CREDITS_CLIENT_SECRET)
], [ ], [
'headers' => ['Accept: application/json'], 'headers' => ['Accept: application/json'],
'json_response' => true 'json_response' => true

View file

@ -40,7 +40,7 @@ class OpsActions extends Actions
} }
$rawPost = file_get_contents('php://input'); $rawPost = file_get_contents('php://input');
$secret = Config::get('github_key'); $secret = Config::get(Config::GITHUB_KEY);
if ($hash !== hash_hmac($algo, $rawPost, $secret)) if ($hash !== hash_hmac($algo, $rawPost, $secret))
{ {
return NavActions::execute400(['error' => 'Hash does not match.']); return NavActions::execute400(['error' => 'Hash does not match.']);
@ -78,8 +78,8 @@ class OpsActions extends Actions
return NavActions::execute400(['error' => "Required params: log, name"]); return NavActions::execute400(['error' => "Required params: log, name"]);
} }
$awsKey = Config::get('aws_log_access_key'); $awsKey = Config::get(Config::AWS_LOG_ACCESS_KEY);
$awsSecret = Config::get('aws_log_secret_key'); $awsSecret = Config::get(Config::AWS_LOG_SECRET_KEY);
if (!$log || !$name) if (!$log || !$name)
{ {

View file

@ -1,13 +1,10 @@
<?php
$config = []; $config = [];
// $config[Config::GITHUB_KEY] = '';
// $config['github_key'] = ''; // $config[Config::GITHUB_DEVELOPER_CREDITS_CLIENT_ID] = '';
// $config['github_developer_credits_client_id'] = ''; // $config[Config::GITHUB_DEVELOPER_CREDITS_CLIENT_SECRET] = '';
// $config['github_developer_credits_client_secret'] = ''; // $config[Config::LBRY_API_SERVER] = '';
// $config['lbry_api_server'] = ''; // $config[Config::MAILCHIMP_KEY] = '';
// $config['mailchimp_key'] = ''; // $config[Config::ASANA_KEY] = '';
// $config['asana_key'] = ''; // $config[Config::AWS_LOG_ACCESS_KEY] = '';
// $config['aws_log_access_key'] = ''; // $config[Config::AWS_LOG_SECRET_KEY] = '';
// $config['aws_log_secret_key'] = '';
return $config; return $config;

View file

@ -80,7 +80,7 @@ class Asana
protected static function get($endpoint, array $data = [], $cache = true) protected static function get($endpoint, array $data = [], $cache = true)
{ {
$apiKey = Config::get('asana_key'); $apiKey = Config::get(Config::ASANA_KEY);
$options = [ $options = [
'headers' => ['Authorization: Bearer ' . $apiKey], 'headers' => ['Authorization: Bearer ' . $apiKey],

View file

@ -5,7 +5,7 @@ class LBRY
{ {
public static function getApiUrl($endpoint) public static function getApiUrl($endpoint)
{ {
return Config::get('lbry_api_server') . $endpoint; return Config::get(Config::LBRY_API_SERVER) . $endpoint;
} }
public static function getLBCtoUSDRate() public static function getLBCtoUSDRate()

View file

@ -51,7 +51,7 @@ class Mailgun
{ {
return Curl::doCurl($method, self::BASE_URL . $endpoint, $data, [ return Curl::doCurl($method, self::BASE_URL . $endpoint, $data, [
'headers' => [ 'headers' => [
'Authorization: Basic ' . base64_encode('api:' . Config::get('mailgun_api_key')) 'Authorization: Basic ' . base64_encode('api:' . Config::get(Config::MAILGUN_API_KEY))
], ],
'retry' => 3, 'retry' => 3,
]); ]);

View file

@ -45,7 +45,7 @@ class Salesforce
protected static function getApiUserPassword() 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) === ':') if ($userpw[0] === ':' || substr($userpw, -1) === ':')
{ {
throw new SalesforceException('Salesforce key and/or secret not configured correctly'); throw new SalesforceException('Salesforce key and/or secret not configured correctly');

View file

@ -10,7 +10,7 @@ class Slack
$e = Debug::exceptionToString($e); $e = Debug::exceptionToString($e);
} }
$slackErrorNotificationUrl = Config::get('slack_error_notification_url'); $slackErrorNotificationUrl = Config::get(Config::SLACK_ERROR_NOTIFICATION_URL);
if ($slackErrorNotificationUrl) if ($slackErrorNotificationUrl)
{ {
Curl::post($slackErrorNotificationUrl, ['text' => ($alert ? '<!channel> ' : '') . Request::getRelativeUri() . "\n" . $e], ['json_data' => true]); Curl::post($slackErrorNotificationUrl, ['text' => ($alert ? '<!channel> ' : '') . Request::getRelativeUri() . "\n" . $e], ['json_data' => true]);

View file

@ -4,6 +4,22 @@ class Config
{ {
const HELP_CONTACT_EMAIL = 'josh@lbry.io'; 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 $loaded = false;
protected static $data = []; protected static $data = [];

View file

@ -8,7 +8,7 @@ if (php_sapi_name() === 'cli-server' && is_file(__DIR__.preg_replace('#(\?.*)$#'
include __DIR__ . '/../bootstrap.php'; 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'); ini_set('display_errors', IS_PRODUCTION ? 'off' : 'on');
error_reporting(IS_PRODUCTION ? 0 : (E_ALL | E_STRICT)); error_reporting(IS_PRODUCTION ? 0 : (E_ALL | E_STRICT));