Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

Google   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 39
dl 0
loc 113
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedirectUri() 0 3 1
A refreshToken() 0 6 1
A getClient() 0 16 2
A getAccessToken() 0 17 3
1
<?php
2
/**
3
 * Basic Google OAuth provider - file.
4
 *
5
 * @copyright YetiForce S.A.
6
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
7
 * @author Radosław Skrzypczak <[email protected]>
8
 */
9
10
namespace App\Integrations\OAuth;
11
12
/**
13
 * Basic Google OAuth provider - class.
14
 */
15
class Google extends AbstractProvider
16
{
17
	/**
18
	 * OAuth provider label.
19
	 *
20
	 * @var string
21
	 */
22
	protected $label = 'Google';
23
24
	/** @var string Icon for authorization button */
25
	protected $icon = 'fab fa-google';
26
27
	/**
28
	 * List of scopes that will be used for authentication.
29
	 *
30
	 * @var array
31
	 *
32
	 * @see https://developers.google.com/identity/protocols/googlescopes
33
	 */
34
	protected $scopes;
35
	protected $scopesForAction = ['MailAccount' => ['https://mail.google.com/']];
36
37
	/**
38
	 * @var string If set, this will be sent to google as the "access_type" parameter.
39
	 *
40
	 * @see https://developers.google.com/identity/protocols/OpenIDConnect#authenticationuriparameters
41
	 */
42
	protected $accessType = 'offline';
43
44
	/**
45
	 * @var string The client ID string that you obtain from the API Console.
46
	 *
47
	 * @see https://developers.google.com/identity/protocols/oauth2/openid-connect#getcredentials
48
	 */
49
	protected $clientId;
50
51
	/** @var string Secret known only to the application and the authorization server */
52
	protected $clientSecret;
53
54
	/**
55
	 * Determines where the response is sent.
56
	 * The value of this parameter must exactly match one of the authorized redirect values that you set in the API Console.
57
	 *
58
	 * @var string
59
	 */
60
	protected $redirectUri;
61
	/**
62
	 * Undocumented variable.
63
	 *
64
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
65
	 */
66
	private $client;
67
68
	protected $refreshToken;
69
	protected $accessToken;
70
	protected $expireTime;
71
72
	public function getClient(array $options = [])
73
	{
74
		if (!$this->client) {
75
			$options = array_merge([
76
				'clientId' => $this->clientId,
77
				'clientSecret' => $this->clientSecret,
78
				'redirectUri' => $this->getRedirectUri(),
79
				// 	//'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts
80
				'accessType' => $this->accessType,
81
				'scopes' => $this->scopes,
82
				// 'prompt' => 'consent', // to alweys return refresh_token
83
			], $options);
84
			$this->client = new \League\OAuth2\Client\Provider\Google($options);
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Provider\Google was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
85
		}
86
87
		return $this->client;
88
	}
89
90
	public function getRedirectUri(): string
91
	{
92
		return $this->redirectUri;
93
	}
94
95
	/**
96
	 * Requests an access token using a specified grant and option set.
97
	 *
98
	 * @param mixed $grant
99
	 * @param array $options
100
	 *
101
	 * @return string
102
	 */
103
	public function getAccessToken($grant, array $options = [])
104
	{
105
		$this->token = null;
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
106
		try {
107
			$token = $this->getClient()->getAccessToken($grant, $options);
108
			$this->accessToken = $token->getToken();
109
			$this->expireTime = $token->getExpires();
110
			if ($token->getRefreshToken()) {
111
				$this->refreshToken = $token->getRefreshToken();
112
			}
113
			$this->token = $token;
114
		} catch (\Throwable $th) {
115
			\App\Log::error($th->getMessage());
116
			throw $th;
117
		}
118
119
		return $token->getToken();
120
	}
121
122
	public function refreshToken()
123
	{
124
		$grant = new \League\OAuth2\Client\Grant\RefreshToken();
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Grant\RefreshToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
		$this->getAccessToken($grant, ['refresh_token' => $this->getRefreshToken()]);
126
127
		return $this;
128
	}
129
}
130