Completed
Push — master ( 2d3dcc...9dbdcf )
by Daniel
04:02
created

GmailConnection::refreshTokenIfNeeded()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
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\File;
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 $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
		$this->refreshTokenIfNeeded();
30
	}
31
32
	public function getAccessToken()
33
	{
34
		$token = parent::getAccessToken() ?: $this->config();
35
		return $token;
36
	}
37
38
	/**
39
	 * @return array|string
40
	 * @throws \Exception
41
	 */
42
	public function makeToken()
43
	{
44
		if ( ! $this->check() ) {
45
			$request = Request::capture();
46
			$code = $request->input( 'code', null );
47
			if ( $code ) {
48
				$accessToken = $this->fetchAccessTokenWithAuthCode( $code );
49
				$me = $this->getProfile();
50
				if ( $me ) {
51
					$this->emailAddress = $me->emailAddress;
52
				}
53
				$this->setAccessToken($accessToken);
54
				return $accessToken;
55
			} else {
56
				throw new \Exception( 'No access token' );
57
			}
58
		} else {
59
			return $this->getAccessToken();
60
		}
61
	}
62
63
	public function setToken( $token )
64
	{
65
		$this->setAccessToken( $token );
66
	}
67
68
	public function check()
69
	{
70
		return !$this->isAccessTokenExpired();
71
	}
72
73
	public function isAccessTokenExpired()
74
	{
75
		$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...
76
		$this->setAccessToken($token);
77
78
		return parent::isAccessTokenExpired();
79
	}
80
81
	public function logout()
82
	{
83
		$this->revokeToken();
84
		$this->setAccessToken( 'null' );
85
	}
86
87
88
	private function refreshTokenIfNeeded()
89
	{
90
		if ( $this->isAccessTokenExpired() ) {
91
			$this->fetchAccessTokenWithRefreshToken( $this->getRefreshToken() );
92
			$token = $this->getAccessToken();
93
			$this->setAccessToken($token);
94
		}
95
	}
96
97
	/**
98
	 * @return \Google_Service_Gmail_Profile
99
	 */
100
	public function getProfile()
101
	{
102
		$service = new Google_Service_Gmail( $this );
103
104
		return $service->users->getProfile( 'me' );
105
	}
106
107
	public function setAccessToken($token)
108
	{
109
		parent::setAccessToken($token);
110
		$this->saveAccessToken($token);
111
	}
112
113
	/**
114
	 * Save the credentials in a file
115
	 *
116
	 * @param array $config
117
	 */
118
	public function saveAccessToken( array $config)
119
	{
120
		$fileName = $this->getFileName();
121
		$file = $file = storage_path( $fileName );
122
123
		File::delete($file);
124
125
		$config[ 'email' ] = $this->emailAddress;
126
		File::put( $file, json_encode( $config ) );
127
	}
128
129
	/**
130
	 * @return array
131
	 */
132
	public function getConfigs()
133
	{
134
		return [
135
			'client_secret' => $this->config->get('gmail.client_secret'),
136
			'client_id'     => $this->config->get('gmail.client_id'),
137
			'redirect_uri'  => url( $this->config->get('gmail.redirect_url', '/') ),
138
		];
139
	}
140
141
	public function config( $string = null, $email = null )
142
	{
143
		$email = $email ?: $this->emailAddress;
144
		$file = storage_path( "gmail-{$email}json" );
145
146
		if ( File::exists( $file ) ) {
147
			$config = json_decode(
148
				File::get( $file ),
149
				true
150
			);
151
152
			if($string) {
153
				if ( isset( $config[ $string ] ) ) {
154
					return $config[ $string ];
155
				}
156
			} else {
157
				return $config;
158
			}
159
160
		}
161
		return null;
162
	}
163
164
	private function getFileName()
165
	{
166
		//TODO Make the replacer function
167
		return $this->config->get('gmail.credentials_file_name');
168
	}
169
170
	private function getUserScopes()
171
	{
172
		return [
173
			Google_Service_Gmail::GMAIL_READONLY,
174
			Google_Service_Gmail::GMAIL_MODIFY,
175
		];
176
	}
177
178
}