Files
mtdb_movie/app/Services/Settings/Validators/TmdbApiKeyValidator.php
maher 703f50a09d
Some checks failed
Build / run (push) Has been cancelled
first commit
2025-10-29 11:42:25 +01:00

45 lines
1.1 KiB
PHP
Executable File

<?php
namespace App\Services\Settings\Validators;
use App\Services\Data\Tmdb\TmdbApi;
use Common\Settings\Validators\SettingsValidator;
use GuzzleHttp\Exception\ClientException;
use Illuminate\Support\Arr;
class TmdbApiKeyValidator implements SettingsValidator
{
public const KEYS = ['tmdb_api_key'];
public function fails($values)
{
if ($apiKey = Arr::get($values, 'tmdb_api_key')) {
config(['services.tmdb.key' => $apiKey]);
}
try {
app(TmdbApi::class)->browse();
} catch (ClientException $e) {
$errResponse = json_decode(
$e
->getResponse()
->getBody()
->getContents(),
true,
512,
JSON_THROW_ON_ERROR,
);
return $this->getMessage($errResponse);
}
}
/**
* @param array $errResponse
* @return array
*/
private function getMessage($errResponse)
{
return ['tmdb_api_key' => 'This Themoviedb api key is not valid.'];
}
}