Skip to content

Commit

Permalink
Merge pull request eyecatchup#131 from iMi-digital/feature-use-dotenv
Browse files Browse the repository at this point in the history
eyecatchup#98 Allow Using DotEnv for ApiKeys
  • Loading branch information
eyecatchup authored Jul 15, 2016
2 parents 6b2a27a + 8ef2abc commit bd3117f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
20 changes: 19 additions & 1 deletion SEOstats/Config/ApiKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
4 changes: 2 additions & 2 deletions SEOstats/Services/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ 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);
}

$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);

Expand Down
10 changes: 5 additions & 5 deletions SEOstats/Services/Mozscape.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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))
);
Expand All @@ -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);
}
Expand Down
6 changes: 3 additions & 3 deletions SEOstats/Services/Sistrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"require" : {
"php" : ">=5.3",
"ext-curl" : "*",
"ext-json" : "*"
"ext-json" : "*",
"vlucas/phpdotenv" : "*"
},
"require-dev" : {
"squizlabs/php_codesniffer" : "~1",
Expand Down

0 comments on commit bd3117f

Please sign in to comment.