Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
15.56% |
7 / 45 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| GoogleTranslate | |
15.56% |
7 / 45 |
|
0.00% |
0 / 6 |
70.22 | |
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 | |
23.33% |
7 / 30 |
|
0.00% |
0 / 1 |
11.21 | |||
| 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 Google\Client as GoogleClient; |
| 8 | use GuzzleHttp\Client; |
| 9 | use GuzzleHttp\Exception\GuzzleException; |
| 10 | use Illuminate\Support\Facades\Cache; |
| 11 | use Psr\Http\Message\ResponseInterface; |
| 12 | |
| 13 | class GoogleTranslate |
| 14 | { |
| 15 | private $client; |
| 16 | |
| 17 | private $headers; |
| 18 | |
| 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 | * |
| 24 | * @throws Exception |
| 25 | */ |
| 26 | public function __construct($accessToken) |
| 27 | { |
| 28 | if (! $accessToken) { |
| 29 | throw new Exception('Access token is required. gcloud auth print-access-token'); |
| 30 | } |
| 31 | $this->client = new Client; |
| 32 | $this->headers = $this->headers($accessToken); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Sets request headers |
| 37 | * |
| 38 | * @return string[] |
| 39 | */ |
| 40 | public function headers($accessToken): array |
| 41 | { |
| 42 | return [ |
| 43 | 'Authorization' => "Bearer $accessToken", |
| 44 | 'Content-Type' => 'application/json', |
| 45 | ]; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Post generated AI prompt response to google translate |
| 50 | * |
| 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 | * @throws GuzzleException |
| 63 | */ |
| 64 | public function translateGeneratedAIPromptResponse(array $postParams): ResponseInterface |
| 65 | { |
| 66 | return $this->postAiGeneratedResponse($postParams); |
| 67 | } |
| 68 | |
| 69 | public static function getGoogleTranslateAccessToken() |
| 70 | { |
| 71 | $access_token = Cache::get('google_translate_access_token'); |
| 72 | if ($access_token) { |
| 73 | return $access_token; |
| 74 | } |
| 75 | |
| 76 | try { |
| 77 | $googleCredentials = SecretsAWS::getSecretValue(); |
| 78 | $privateKey = str_replace('\\n', "\n", $googleCredentials['GOOGLE_TRANSLATE_PRIVATE_KEY']); |
| 79 | |
| 80 | $client = new GoogleClient; |
| 81 | |
| 82 | $credentials = [ |
| 83 | 'type' => 'service_account', |
| 84 | 'project_id' => $googleCredentials['GOOGLE_TRANSLATE_PROJECT_ID'], |
| 85 | 'private_key_id' => $googleCredentials['GOOGLE_TRANSLATE_PRIVATE_KEY_ID'], |
| 86 | 'private_key' => $privateKey, |
| 87 | 'client_email' => $googleCredentials['GOOGLE_TRANSLATE_CLIENT_EMAIL'], |
| 88 | 'client_id' => $googleCredentials['GOOGLE_TRANSLATE_CLIENT_ID'], |
| 89 | 'auth_uri' => 'https://accounts.google.com/o/oauth2/auth', |
| 90 | 'token_uri' => 'https://oauth2.googleapis.com/token', |
| 91 | 'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs', |
| 92 | 'client_x509_cert_url' => $googleCredentials['GOOGLE_TRANSLATE_X509_CERT_URL'], |
| 93 | ]; |
| 94 | |
| 95 | $client->setAuthConfig($credentials); |
| 96 | $client->setApplicationName('FlyMSG'); |
| 97 | $client->setScopes(['https://www.googleapis.com/auth/cloud-platform']); |
| 98 | |
| 99 | if ($client->isAccessTokenExpired()) { |
| 100 | $client->fetchAccessTokenWithAssertion(); |
| 101 | } |
| 102 | |
| 103 | $access_token = $client->getAccessToken(); |
| 104 | $access_token = $access_token['access_token']; |
| 105 | |
| 106 | // Cache for 40 minutes (2400 seconds) |
| 107 | Cache::put('google_translate_access_token', $access_token, 2400); |
| 108 | |
| 109 | return $access_token; |
| 110 | } catch (Exception $e) { |
| 111 | FlyMSGLogger::logError('FlyTranslate', $e); |
| 112 | |
| 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 | |
| 124 | return GoogleTranslate::getGoogleTranslateAccessToken(); |
| 125 | } |
| 126 | } |