Skip to content

Commit

Permalink
final
Browse files Browse the repository at this point in the history
  • Loading branch information
ymigval committed Aug 26, 2023
1 parent 8402b83 commit 4d247d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
31 changes: 18 additions & 13 deletions src/IndexNowApiKeyManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ymigval\LaravelIndexnow;

use Exception;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Ymigval\LaravelIndexnow\Exceptions\InvalidKeyException;
Expand All @@ -21,22 +22,26 @@ class IndexNowApiKeyManager
* Get the IndexNow API key.
*
* @return string
* @throws KeyFileDoesNotExistException | InvalidKeyException
*/
public static function getApiKey()
{
if (File::exists(static::$apiKeyFilePath) == false) {
throw new KeyFileDoesNotExistException();
try {
if (File::exists(static::$apiKeyFilePath) == false) {
throw new KeyFileDoesNotExistException();
}

$apiKey = File::get(static::$apiKeyFilePath);
$apiKeyLength = strlen($apiKey);

if ($apiKeyLength < 8 || $apiKeyLength > 128) {
throw new InvalidKeyException();
}

return $apiKey;
} catch (Exception $e) {
// If you don't have an API Key, generate a new one.
return IndexNowApiKeyManager::generateNewApiKey();
}

$apiKey = File::get(static::$apiKeyFilePath);
$apiKeyLength = strlen($apiKey);

if ($apiKeyLength < 8 || $apiKeyLength > 128) {
throw new InvalidKeyException();
}

return $apiKey;
}

/**
Expand All @@ -52,4 +57,4 @@ public static function generateNewApiKey(): string

return $apiKey;
}
}
}
10 changes: 1 addition & 9 deletions src/IndexNowService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Ymigval\LaravelIndexnow\Exceptions\ExcessUrlsException;
use Ymigval\LaravelIndexnow\Exceptions\InvalidKeyException;
use Ymigval\LaravelIndexnow\Exceptions\KeyFileDoesNotExistException;
use Ymigval\LaravelIndexnow\Exceptions\MixedException;
use Ymigval\LaravelIndexnow\Exceptions\NonAbsoluteUrlException;
use Ymigval\LaravelIndexnow\Exceptions\SearchEngineUnknownException;
Expand Down Expand Up @@ -66,16 +64,10 @@ public function getSearchEngine(): string
* Get the IndexNow API key.
*
* @return string
* @throws KeyFileDoesNotExistException | InvalidKeyException
*/
public function getKey(): string
{
// If you don't have an API Key, generate a new one.
try {
return IndexNowApiKeyManager::getApiKey();
} catch (Exception $e) {
return IndexNowApiKeyManager::generateNewApiKey();
}
return IndexNowApiKeyManager::getApiKey();
}

/**
Expand Down

0 comments on commit 4d247d1

Please sign in to comment.