Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| GoogleClient | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
9 | |
100.00% |
1 / 1 |
| getCredentialsFromJsonFile | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| addScope | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace pvc\cloud\google; |
| 6 | |
| 7 | use Google_Client; |
| 8 | use Google_Service_Drive; |
| 9 | use pvc\cloud\google\err\GoogleAuthenticationException; |
| 10 | use pvc\cloud\google\err\InvalidOrUnauthorizedScopeException; |
| 11 | use pvc\err\pvc\file\FileDoesNotExistException; |
| 12 | use pvc\err\pvc\file\FileNotReadableException; |
| 13 | use ReflectionClass; |
| 14 | use Throwable; |
| 15 | |
| 16 | class GoogleClient extends Google_Client |
| 17 | { |
| 18 | public Google_Client $client; |
| 19 | |
| 20 | /** |
| 21 | * @param string $credentialsFile |
| 22 | * @return array<string> |
| 23 | * @throws FileDoesNotExistException |
| 24 | * @throws FileNotReadableException |
| 25 | */ |
| 26 | public static function getCredentialsFromJsonFile(string $credentialsFile): array |
| 27 | { |
| 28 | if (!file_exists($credentialsFile)) { |
| 29 | throw new FileDoesNotExistException($credentialsFile); |
| 30 | } |
| 31 | if (!is_readable($credentialsFile)) { |
| 32 | throw new FileNotReadableException($credentialsFile); |
| 33 | } |
| 34 | $fileContents = file_get_contents($credentialsFile); |
| 35 | |
| 36 | /** |
| 37 | * assertion to help the static type checker |
| 38 | * json_decode will only fail of the depth of the json is too deep. The odds of this are very, very small |
| 39 | * in this context and throwing a custom error or exception would not really add any value anyway. |
| 40 | */ |
| 41 | assert($fileContents !== false); |
| 42 | /** @var array<string> $credentials */ |
| 43 | $credentials = json_decode($fileContents, true); |
| 44 | return $credentials; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @param array<string> $credentials |
| 49 | * @throws GoogleAuthenticationException |
| 50 | */ |
| 51 | public function __construct(array $credentials) |
| 52 | { |
| 53 | try { |
| 54 | parent::__construct(['credentials' => $credentials]); |
| 55 | } catch (Throwable $e) { |
| 56 | throw new GoogleAuthenticationException($e); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param array<string>|string $scope_or_scopes |
| 62 | * @return void |
| 63 | * |
| 64 | * valid scopes are defined as constants in vendor\google\apiclient-services\src\Drive\Drive.php. The class is |
| 65 | * aliased and these constants can be referred to as Google_Service_Drive::(constant name). For example, |
| 66 | * Google_Service_Drive::DRIVE. |
| 67 | * |
| 68 | * The precise definitions of these scopes can be found at |
| 69 | * https://developers.google.com/drive/api/guides/api-specific-auth |
| 70 | */ |
| 71 | public function addScope($scope_or_scopes): void |
| 72 | { |
| 73 | $reflection = new ReflectionClass(Google_Service_Drive::class); |
| 74 | $constants = $reflection->getConstants(); |
| 75 | $scopes = (is_string($scope_or_scopes)) ? [$scope_or_scopes] : $scope_or_scopes; |
| 76 | |
| 77 | foreach ($scopes as $scope) { |
| 78 | if (!in_array($scope, $constants)) { |
| 79 | throw new InvalidOrUnauthorizedScopeException($scope); |
| 80 | } else { |
| 81 | parent::addScope($scope); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |