Test Setup Failed
Branch master (24c563)
by Daniel
02:09
created

GmailConnection::setBothAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Dacastro4\LaravelGmail;
4
5
use Google_Client;
6
use Google_Service_Gmail;
7
use Illuminate\Container\Container;
8
use Illuminate\Config\Repository as Config;
9
use Illuminate\Support\Facades\Storage;
10
use Illuminate\Support\Facades\Request;
11
12
class GmailConnection extends Google_Client
13
{
14
15
	protected $emailAddress;
16
	protected $refreshToken;
17
	protected $app;
18
	protected $accessToken;
19
	protected $token;
20
	private $config;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
21
22
	public function __construct( $config = null )
23
	{
24
		$this->app = Container::getInstance();
25
		$this->config = $config;
26
		parent::__construct( $this->getConfigs() );
27
		$this->setScopes( $this->getUserScopes() );
28
		$this->setAccessType( 'offline' );
29
		if ( $this->check() ) {
30
			$this->refreshTokenIfNeeded();
31
		}
32
	}
33
34
	public function getAccessToken()
35
	{
36
		$token = parent::getAccessToken() ?: $this->config();
37
38
		return $token;
39
	}
40
41
	/**
42
	 * @return array|string
43
	 * @throws \Exception
44
	 */
45
	public function makeToken()
46
	{
47
		if ( ! $this->check() ) {
48
			$request = Request::capture();
49
			$code = $request->input( 'code', null );
50
			if ( $code ) {
51
				$accessToken = $this->fetchAccessTokenWithAuthCode( $code );
52
				$me = $this->getProfile();
53
				if ( $me ) {
54
					$this->emailAddress = $me->emailAddress;
55
				}
56
				$this->setBothAccessToken( $accessToken );
57
58
				return $accessToken;
59
			} else {
60
				throw new \Exception( 'No access token' );
61
			}
62
		} else {
63
			return $this->getAccessToken();
64
		}
65
	}
66
67
	public function setToken( $token )
68
	{
69
		$this->setAccessToken( $token );
70
	}
71
72
	public function check()
73
	{
74
		return ! $this->isAccessTokenExpired();
75
	}
76
77
	public function isAccessTokenExpired()
78
	{
79
		$token = parent::getAccessToken() ?: $this->config();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getAccessToken() instead of isAccessTokenExpired()). Are you sure this is correct? If so, you might want to change this to $this->getAccessToken().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
80
		if ( $token ) {
81
			$this->setAccessToken( $token );
82
		}
83
84
		return parent::isAccessTokenExpired();
85
	}
86
87
	public function logout()
88
	{
89
		$this->revokeToken();
90
	}
91
92
93
	private function refreshTokenIfNeeded()
94
	{
95
		if ( $this->isAccessTokenExpired() ) {
96
			$this->fetchAccessTokenWithRefreshToken( $this->getRefreshToken() );
97
			$token = $this->getAccessToken();
98
			$this->setAccessToken( $token );
99
		}
100
	}
101
102
	/**
103
	 * @return \Google_Service_Gmail_Profile
104
	 */
105
	public function getProfile()
106
	{
107
		$service = new Google_Service_Gmail( $this );
108
109
		return $service->users->getProfile( 'me' );
110
	}
111
112
	public function setAccessToken( $token )
113
	{
114
		parent::setAccessToken( $token );
115
	}
116
117
	public function setBothAccessToken( $token )
118
	{
119
		parent::setAccessToken( $token );
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (setAccessToken() instead of setBothAccessToken()). Are you sure this is correct? If so, you might want to change this to $this->setAccessToken().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
120
		$this->saveAccessToken($token);
121
	}
122
123
	/**
124
	 * Save the credentials in a file
125
	 *
126
	 * @param array $config
127
	 */
128
	public function saveAccessToken( array $config )
129
	{
130
		$fileName = $this->getFileName();
131
		$file = "gmail/tokens/$fileName.json";
132
133
		if ( Storage::exists( $file ) ) {
134
			Storage::delete( storage_path($file) );
135
		}
136
137
		$config[ 'email' ] = $this->emailAddress;
138
139
		Storage::put( $file, json_encode( $config ) );
140
	}
141
142
	/**
143
	 * Delete the credentials in a file
144
	 */
145
	public function deleteAccessToken()
146
	{
147
		$fileName = $this->getFileName();
148
		$file = "gmail/tokens/$fileName.json";
149
150
		if ( Storage::exists( $file ) ) {
151
			Storage::delete( $file );
152
		}
153
154
		Storage::put( $file, json_encode( [] ) );
155
	}
156
157
	/**
158
	 * @return array
159
	 */
160
	public function getConfigs()
161
	{
162
		return [
163
			'client_secret' => $this->config['gmail.client_secret'],
164
			'client_id'     => $this->config['gmail.client_id'],
165
			'redirect_uri'  => url( $this->config['gmail.redirect_url'] ),
166
		];
167
	}
168
169
	public function config( $string = null, $email = null )
170
	{
171
		$email = $email ?: $this->emailAddress;
172
		$fileName = $this->getFileName($email);
173
		$file = "gmail/tokens/{$fileName}.json";
174
175
		if ( Storage::exists( $file ) ) {
176
			$config = json_decode(
177
				Storage::get( $file ),
178
				true
179
			);
180
181
			if ( $string ) {
182
				if ( isset( $config[ $string ] ) ) {
183
					return $config[ $string ];
184
				}
185
			} else {
186
				return $config;
187
			}
188
189
		}
190
191
		return null;
192
	}
193
194
	private function getFileName($email = null)
0 ignored issues
show
Unused Code introduced by
The parameter $email is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
195
	{
196
		//TODO Make the replacer function
197
		return $this->config['gmail.credentials_file_name'];
198
	}
199
200
	private function getUserScopes()
201
	{
202
		return [
203
			Google_Service_Gmail::GMAIL_READONLY,
204
			Google_Service_Gmail::GMAIL_MODIFY,
205
		];
206
	}
207
208
}