Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
GoogleClient
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
3 / 3
9
100.00% covered (success)
100.00%
1 / 1
 getCredentialsFromJsonFile
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 addScope
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5namespace pvc\cloud\google;
6
7use Google_Client;
8use Google_Service_Drive;
9use pvc\cloud\google\err\GoogleAuthenticationException;
10use pvc\cloud\google\err\InvalidOrUnauthorizedScopeException;
11use pvc\err\pvc\file\FileDoesNotExistException;
12use pvc\err\pvc\file\FileNotReadableException;
13use ReflectionClass;
14use Throwable;
15
16class 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}