From 8ef2abcbebd46bd8c69d4945e4f3712f5b7f98b4 Mon Sep 17 00:00:00 2001 From: mht Date: Fri, 29 Apr 2016 13:51:48 +0200 Subject: [PATCH] #98 Allow Using DotEnv for ApiKeys In order to be able to store configuration for a project without touching the code inside vendor we added the dotenv dependency and fetch the keys from the ENV if existing. The configuration constants can still be used. --- SEOstats/Config/ApiKeys.php | 20 +++++++++++++++++++- SEOstats/Services/Google.php | 4 ++-- SEOstats/Services/Mozscape.php | 10 +++++----- SEOstats/Services/Sistrix.php | 6 +++--- composer.json | 3 ++- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/SEOstats/Config/ApiKeys.php b/SEOstats/Config/ApiKeys.php index b5cd27d8..b0d5e1cd 100644 --- a/SEOstats/Config/ApiKeys.php +++ b/SEOstats/Config/ApiKeys.php @@ -15,7 +15,7 @@ * Client API keys * @package SEOstats */ -interface ApiKeys +class ApiKeys { // To acquire an API key, visit Google's APIs Console here: // https://code.google.com/apis/console @@ -32,4 +32,22 @@ interface ApiKeys // To acquire a SISTRIX API key, visit: // http://www.sistrix.de const SISTRIX_API_ACCESS_KEY = ''; + + public static function getGoogleSimpleApiAccessKey() { + return env('GOOGLE_SIMPLE_API_ACCESS_KEY', self::GOOGLE_SIMPLE_API_ACCESS_KEY); + } + + public static function getMozscapeAccessId() { + return env('MOZSCAPE_ACCESS_ID', self::MOZSCAPE_ACCESS_ID); + } + + public static function getMozscapeSecretKey() + { + return env('MOZSCAPE_SECRET_KEY', self::MOZSCAPE_SECRET_KEY); + } + + public static function getSistrixApiAccessKey() + { + return env('SISTRIX_API_ACCESS_KEY', self::SISTRIX_API_ACCESS_KEY); + } } diff --git a/SEOstats/Services/Google.php b/SEOstats/Services/Google.php index fcdc4bcc..c3ec9686 100644 --- a/SEOstats/Services/Google.php +++ b/SEOstats/Services/Google.php @@ -88,7 +88,7 @@ public static function getSearchResultsTotal($url = false) public static function getPagespeedAnalysis($url = false, $strategy = 'desktop') { - if ('' == Config\ApiKeys::GOOGLE_SIMPLE_API_ACCESS_KEY) { + if ('' == Config\ApiKeys::getGoogleSimpleApiAccessKey()) { throw new E('In order to use the PageSpeed API, you must obtain and set an API key first (see SEOstats\Config\ApiKeys.php).'); exit(0); @@ -96,7 +96,7 @@ public static function getPagespeedAnalysis($url = false, $strategy = 'desktop') $url = parent::getUrl($url); $url = sprintf(Config\Services::GOOGLE_PAGESPEED_URL, - $url, $strategy, Config\ApiKeys::GOOGLE_SIMPLE_API_ACCESS_KEY); + $url, $strategy, Config\ApiKeys::getGoogleSimpleApiAccessKey()); $ret = static::_getPage($url); diff --git a/SEOstats/Services/Mozscape.php b/SEOstats/Services/Mozscape.php index 9a7d4dc5..5e49042a 100644 --- a/SEOstats/Services/Mozscape.php +++ b/SEOstats/Services/Mozscape.php @@ -78,8 +78,8 @@ public static function getMozRankRaw($url = false) */ public static function getCols($cols, $url = false) { - if ('' == Config\ApiKeys::MOZSCAPE_ACCESS_ID || - '' == Config\ApiKeys::MOZSCAPE_SECRET_KEY) { + if ('' == Config\ApiKeys::getMozscapeAccessId() || + '' == Config\ApiKeys::getMozscapeSecretKey()) { throw new E('In order to use the Mozscape API, you must obtain and set an API key first (see SEOstats\Config\ApiKeys.php).'); exit(0); @@ -90,7 +90,7 @@ public static function getCols($cols, $url = false) $apiEndpoint = sprintf(Config\Services::MOZSCAPE_API_URL, urlencode(Helper\Url::parseHost(parent::getUrl($url))), $cols, - Config\ApiKeys::MOZSCAPE_ACCESS_ID, + Config\ApiKeys::getMozscapeAccessId(), $expires, urlencode(self::_getUrlSafeSignature($expires)) ); @@ -104,8 +104,8 @@ public static function getCols($cols, $url = false) private static function _getUrlSafeSignature($expires) { - $data = Config\ApiKeys::MOZSCAPE_ACCESS_ID . "\n{$expires}"; - $sig = self::_hmacsha1($data, Config\ApiKeys::MOZSCAPE_SECRET_KEY); + $data = Config\ApiKeys::getMozscapeAccessId() . "\n{$expires}"; + $sig = self::_hmacsha1($data, Config\ApiKeys::getMozscapeSecretKey()); return base64_encode($sig); } diff --git a/SEOstats/Services/Sistrix.php b/SEOstats/Services/Sistrix.php index e6f2d5ae..3d7d5086 100644 --- a/SEOstats/Services/Sistrix.php +++ b/SEOstats/Services/Sistrix.php @@ -69,7 +69,7 @@ public static function getVisibilityIndexByApi($url = false, $db = false) $domain = static::getDomainFromUrl($url); $database = static::getValidDatabase($db); - $dataUrl = sprintf(Config\Services::SISTRIX_API_VI_URL, Config\ApiKeys::SISTRIX_API_ACCESS_KEY, urlencode($domain), $database); + $dataUrl = sprintf(Config\Services::SISTRIX_API_VI_URL, Config\ApiKeys::getSistrixApiAccessKey(), urlencode($domain), $database); $json = static::_getPage($dataUrl); @@ -88,7 +88,7 @@ public static function getApiCredits() { self::guardApiKey(); - $dataUrl = sprintf(Config\Services::SISTRIX_API_CREDITS_URL, Config\ApiKeys::SISTRIX_API_ACCESS_KEY); + $dataUrl = sprintf(Config\Services::SISTRIX_API_CREDITS_URL, Config\ApiKeys::getSistrixApiAccessKey()); $json = static::_getPage($dataUrl); if(empty($json)) { @@ -117,7 +117,7 @@ protected static function guardApiKey() protected static function hasApiKey() { - if ('' == Config\ApiKeys::SISTRIX_API_ACCESS_KEY) { + if ('' == Config\ApiKeys::getSistrixApiAccessKey()) { return false; } diff --git a/composer.json b/composer.json index d9cfb953..b0c37e63 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ "require" : { "php" : ">=5.3", "ext-curl" : "*", - "ext-json" : "*" + "ext-json" : "*", + "vlucas/phpdotenv" : "*" }, "require-dev" : { "squizlabs/php_codesniffer" : "~1",