Passed
Push — master ( 67acca...c6ae30 )
by Daniel
01:45
created

GmailConnection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dacastro4\LaravelGmail;
4
5
use Dacastro4\LaravelGmail\Traits\Configurable;
6
use Google_Client;
7
use Google_Service_Gmail;
8
use Illuminate\Container\Container;
9
use Illuminate\Support\Facades\Request;
10
use Illuminate\Support\Facades\Storage;
11
12
class GmailConnection extends Google_Client
13
{
14
15
	use Configurable;
16
17
	protected $emailAddress;
18
	protected $refreshToken;
19
	protected $app;
20
	protected $accessToken;
21
	protected $token;
22
	private $configuration;
23
24
	public function __construct( $config = null )
25
	{
26
		$this->app = Container::getInstance();
27
28
		$this->configuration = $config;
29
30
		parent::__construct( $this->getConfigs() );
31
32
		$this->configApi();
33
34
		if ( $this->check() ) {
35
			$this->refreshTokenIfNeeded();
36
		}
37
	}
38
39
	public function getAccessToken()
40
	{
41
		$token = parent::getAccessToken() ?: $this->config();
42
43
		return $token;
44
	}
45
46
	/**
47
	 * @return array|string
48
	 * @throws \Exception
49
	 */
50
	public function makeToken()
51
	{
52
		if ( ! $this->check() ) {
53
			$request = Request::capture();
54
			$code = (string) $request->input( 'code', null );
55
			if ( !is_null($code) && !empty($code) ) {
56
				$accessToken = $this->fetchAccessTokenWithAuthCode( $code );
57
				$me = $this->getProfile();
58
				if ( $me && $me instanceof \Google_Service_Gmail_Profile ) {
0 ignored issues
show
introduced by
The condition $me && $me instanceof Go...e_Service_Gmail_Profile can never be true.
Loading history...
59
					$this->emailAddress = $me->emailAddress;
60
				}
61
				$this->setBothAccessToken( $accessToken );
62
63
				return $accessToken;
64
			} else {
65
				throw new \Exception( 'No access token' );
66
			}
67
		} else {
68
			return $this->getAccessToken();
69
		}
70
	}
71
72
	public function setToken( $token )
73
	{
74
		$this->setAccessToken( $token );
75
	}
76
77
	/**
78
	 * Check
79
	 *
80
	 * @return bool
81
	 */
82
	public function check()
83
	{
84
		return ! $this->isAccessTokenExpired();
85
	}
86
87
	/**
88
	 * Check if token exists and is expired
89
	 * Throws an AuthException when the auth file its empty or with the wrong token
90
	 *
91
	 *
92
	 * @return bool
93
	 */
94
	public function isAccessTokenExpired()
95
	{
96
		$token = $this->getToken();
97
98
		if ( $token ) {
99
			$this->setAccessToken( $token );
100
		}
101
102
		return parent::isAccessTokenExpired();
103
	}
104
105
	/**
106
	 * Revokes user's permission and logs them out
107
	 */
108
	public function logout()
109
	{
110
		$this->revokeToken();
111
	}
112
113
	public function getToken()
114
	{
115
		return parent::getAccessToken() ?: $this->config();
116
	}
117
118
	/**
119
	 * Refresh the auth token if needed
120
	 */
121
	private function refreshTokenIfNeeded()
122
	{
123
		if ( $this->isAccessTokenExpired() ) {
124
			$this->fetchAccessTokenWithRefreshToken( $this->getRefreshToken() );
125
			$token = $this->getAccessToken();
126
			$this->setAccessToken( $token );
127
		}
128
	}
129
130
	/**
131
	 * Gets user profile from Gmail
132
	 *
133
	 * @return \Google_Service_Gmail_Profile
134
	 */
135
	public function getProfile()
136
	{
137
		$service = new Google_Service_Gmail( $this );
138
139
		return $service->users->getProfile( 'me' );
140
	}
141
142
	/**
143
	 * @param array|string $token
144
	 */
145
	public function setAccessToken( $token )
146
	{
147
		parent::setAccessToken( $token );
148
	}
149
150
	/**
151
	 * @param $token
152
	 */
153
	public function setBothAccessToken( $token )
154
	{
155
		parent::setAccessToken( $token );
156
		$this->saveAccessToken( $token );
157
	}
158
159
	/**
160
	 * Save the credentials in a file
161
	 *
162
	 * @param array $config
163
	 */
164
	public function saveAccessToken( array $config )
165
	{
166
		$fileName = $this->getFileName();
167
		$file = "gmail/tokens/$fileName.json";
168
169
		if ( Storage::exists( $file ) ) {
170
			Storage::delete( storage_path( $file ) );
171
		}
172
173
		$config[ 'email' ] = $this->emailAddress;
174
175
		Storage::put( $file, json_encode( $config ) );
176
	}
177
178
	/**
179
	 * Delete the credentials in a file
180
	 */
181
	public function deleteAccessToken()
182
	{
183
		$fileName = $this->getFileName();
184
		$file = "gmail/tokens/$fileName.json";
185
186
		if ( Storage::exists( $file ) ) {
187
			Storage::delete( $file );
188
		}
189
190
		Storage::put( $file, json_encode( [] ) );
191
	}
192
193
}