Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 45 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| GoogleTranslate | |
0.00% |
0 / 45 |
|
0.00% |
0 / 6 |
110 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| headers | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| postAiGeneratedResponse | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| translateGeneratedAIPromptResponse | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getGoogleTranslateAccessToken | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
20 | |||
| refreshGoogleTranslateAccessToken | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Services\FlyMsgAI; |
| 4 | |
| 5 | use App\Helpers\FlyMSGLogger; |
| 6 | use Exception; |
| 7 | use GuzzleHttp\Client; |
| 8 | use Illuminate\Support\Facades\Artisan; |
| 9 | use Psr\Http\Message\ResponseInterface; |
| 10 | use GuzzleHttp\Exception\GuzzleException; |
| 11 | use Illuminate\Support\Facades\Cache; |
| 12 | use Google\Client as GoogleClient; |
| 13 | use Illuminate\Support\Facades\Log; |
| 14 | |
| 15 | class GoogleTranslate |
| 16 | { |
| 17 | private $client; |
| 18 | private $headers; |
| 19 | private $baseURL = "https://translate.googleapis.com/v3beta1/projects/project-romeo/locations/global:detectLanguage"; |
| 20 | |
| 21 | /** |
| 22 | * Setup Google Translate client headers for translate requests |
| 23 | * @throws Exception |
| 24 | */ |
| 25 | public function __construct($accessToken) |
| 26 | { |
| 27 | if (!$accessToken) { |
| 28 | throw new Exception("Access token is required. gcloud auth print-access-token"); |
| 29 | } |
| 30 | $this->client = new Client(); |
| 31 | $this->headers = $this->headers($accessToken); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Sets request headers |
| 36 | * @param $accessToken |
| 37 | * @return string[] |
| 38 | */ |
| 39 | public function headers($accessToken): array |
| 40 | { |
| 41 | return [ |
| 42 | "Authorization" => "Bearer $accessToken", |
| 43 | "Content-Type" => "application/json", |
| 44 | ]; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Post generated AI prompt response to google translate |
| 49 | * @param array $data |
| 50 | * @return ResponseInterface |
| 51 | * @throws GuzzleException |
| 52 | */ |
| 53 | private function postAiGeneratedResponse(array $data = []): ResponseInterface |
| 54 | { |
| 55 | return $this->client->post($this->baseURL, [ |
| 56 | 'headers' => $this->headers, |
| 57 | 'json' => $data, |
| 58 | ]); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @param array $postParams |
| 63 | * @return ResponseInterface |
| 64 | * @throws GuzzleException |
| 65 | */ |
| 66 | public function translateGeneratedAIPromptResponse(array $postParams): ResponseInterface |
| 67 | { |
| 68 | return $this->postAiGeneratedResponse($postParams); |
| 69 | } |
| 70 | |
| 71 | public static function getGoogleTranslateAccessToken() |
| 72 | { |
| 73 | $access_token = Cache::get("google_translate_access_token"); |
| 74 | if ($access_token) { |
| 75 | return $access_token; |
| 76 | } |
| 77 | |
| 78 | try { |
| 79 | $googleCredentials = SecretsAWS::getSecretValue(); |
| 80 | $privateKey = str_replace("\\n", "\n", $googleCredentials['GOOGLE_TRANSLATE_PRIVATE_KEY']); |
| 81 | |
| 82 | $client = new GoogleClient(); |
| 83 | |
| 84 | $credentials = [ |
| 85 | 'type' => 'service_account', |
| 86 | 'project_id' => $googleCredentials['GOOGLE_TRANSLATE_PROJECT_ID'], |
| 87 | 'private_key_id' => $googleCredentials['GOOGLE_TRANSLATE_PRIVATE_KEY_ID'], |
| 88 | 'private_key' => $privateKey, |
| 89 | 'client_email' => $googleCredentials['GOOGLE_TRANSLATE_CLIENT_EMAIL'], |
| 90 | 'client_id' => $googleCredentials['GOOGLE_TRANSLATE_CLIENT_ID'], |
| 91 | 'auth_uri' => 'https://accounts.google.com/o/oauth2/auth', |
| 92 | 'token_uri' => 'https://oauth2.googleapis.com/token', |
| 93 | 'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs', |
| 94 | 'client_x509_cert_url' => $googleCredentials['GOOGLE_TRANSLATE_X509_CERT_URL'], |
| 95 | ]; |
| 96 | |
| 97 | $client->setAuthConfig($credentials); |
| 98 | $client->setApplicationName("FlyMSG"); |
| 99 | $client->setScopes(['https://www.googleapis.com/auth/cloud-platform']); |
| 100 | |
| 101 | if ($client->isAccessTokenExpired()) { |
| 102 | $client->fetchAccessTokenWithAssertion(); |
| 103 | } |
| 104 | |
| 105 | $access_token = $client->getAccessToken(); |
| 106 | $access_token = $access_token['access_token']; |
| 107 | |
| 108 | Cache::put("google_translate_access_token", $access_token, GeminiAPI::FOURTY_MINUTES); |
| 109 | |
| 110 | return $access_token; |
| 111 | } catch (Exception $e) { |
| 112 | FlyMSGLogger::logError('FlyTranslate', $e); |
| 113 | return null; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Refreshes access token for Google Translate api |
| 119 | */ |
| 120 | public static function refreshGoogleTranslateAccessToken() |
| 121 | { |
| 122 | Cache::forget("google_translate_access_token"); |
| 123 | return GoogleTranslate::getGoogleTranslateAccessToken(); |
| 124 | } |
| 125 | } |