Skip to content

Commit 366e07e

Browse files
author
Neo
committed
Start Config API
1 parent cfa1883 commit 366e07e

File tree

9 files changed

+637
-0
lines changed

9 files changed

+637
-0
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
build
2+
docs
3+
vendor
4+
composer.lock
5+
_README.MD

composer.json

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "niltonmelox/gmail-api-easy",
3+
"description": "Laravel 5.* wrapper easy for the API Gmail",
4+
"keywords": [
5+
"laravel",
6+
"gmail"
7+
],
8+
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Martijn Wagena",
13+
"email": "[email protected]",
14+
"homepage": "http://www.beeproger.com"
15+
},
16+
{
17+
"name": "Nilton Melo",
18+
"email": "[email protected]",
19+
"homepage": "https://niltonmelox.github.io/"
20+
}
21+
],
22+
"require": {
23+
"illuminate/support": "5.0.*|5.1.*|5.2.*|5.3.*|5.4.*",
24+
"google/apiclient": "^2.0",
25+
"nesbot/carbon": "^1.22",
26+
"swiftmailer/swiftmailer": "^5.4"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"NiltonMeloX\\GmailApiEasy\\Gmail\\": "src"
31+
}
32+
},
33+
"require-dev": {
34+
"php": ">=5.5.9"
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
}
39+
}
40+
}

config/gmail.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
return [
3+
'app_name' => env('GMAIL_APP_NAME', ''),
4+
'client_id' => env('GMAIL_CLIENT_ID', ''),
5+
'client_secret' => env('GMAIL_SECRET', ''),
6+
'project_id' => env('GMAIL_PROJECT_ID', ''),
7+
'redirect_uri' => env('GMAIL_REDIRECT_URI', ''),
8+
];

src/Facades/Gmail.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace NiltonMeloX\GmailApiEasy\Gmail\Facades;
3+
4+
class Gmail extends \Illuminate\Support\Facades\Facade
5+
{
6+
/**
7+
* {@inheritdoc}
8+
*/
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'gmail';
12+
}
13+
}

src/Gmail/Gmail.php

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
namespace NiltonMeloX\GmailApiEasy\Gmail;
3+
4+
use Google_Client;
5+
use Google_Service_Gmail;
6+
7+
class Gmail {
8+
9+
protected $config;
10+
protected $scopes;
11+
protected $accessToken;
12+
protected $emailAddress;
13+
14+
public function __construct($config = [])
15+
{
16+
$this->config = $config;
17+
18+
$this->scopes = implode(' ', [
19+
Google_Service_Gmail::GMAIL_READONLY,
20+
Google_Service_Gmail::GMAIL_SEND
21+
]);
22+
23+
$this->accessToken = '';
24+
$this->refreshToken = '';
25+
26+
$this->client = new Google_Client();
27+
28+
$this->getClient();
29+
}
30+
31+
public static function create()
32+
{
33+
return new static;
34+
}
35+
36+
/**
37+
* @return Google_Client
38+
*/
39+
public function getClient() {
40+
$this->client->setApplicationName($this->config['app_name']);
41+
$this->client->setScopes($this->scopes);
42+
$this->client->setAuthConfig($this->getAuthConfig());
43+
$this->client->setAccessType('offline');
44+
$this->client->setRedirectUri($this->config['redirect_uri']);
45+
return $this->client;
46+
}
47+
48+
/**
49+
* @return array
50+
*/
51+
public function getAuthUrl() {
52+
53+
$this->getClient();
54+
$authUrl = $this->client->createAuthUrl();
55+
56+
return compact('authUrl');
57+
}
58+
59+
/**
60+
* @param $code
61+
* @return static
62+
*/
63+
public function makeAccessToken($code) {
64+
$this->getClient();
65+
66+
$accessToken = $this->client->fetchAccessTokenWithAuthCode($code);
67+
68+
$this->accessToken = $accessToken['access_token'];
69+
$this->refreshToken = $accessToken['refresh_token'];
70+
71+
$this->client->setAccessToken($this->formatAccessToken());
72+
73+
$me = $this->getProfile();
74+
if($me) {
75+
$this->emailAddress = $me->emailAddress;
76+
}
77+
78+
79+
return $this->saveAccessToken();
80+
}
81+
82+
/**
83+
* @param bool $update
84+
* @return static
85+
*/
86+
public function saveAccessToken() {
87+
$config = json_decode(
88+
File::get(
89+
$file = storage_path('gmail-' . $this->emailAddress . 'json')
90+
),
91+
true
92+
);
93+
94+
$config['access_token'] = $this->accessToken;
95+
$config['refresh_token'] = $this->refreshToken;
96+
$config['email'] = $this->emailAddress;
97+
98+
File::put($file, json_encode($config));
99+
}
100+
101+
/**
102+
*
103+
*/
104+
public function revokeAccess() {
105+
$this->client->revokeToken();
106+
}
107+
108+
/**
109+
* @return \Google_Service_Gmail_Profile
110+
*/
111+
private function getProfile() {
112+
$service = new Google_Service_Gmail($this->client);
113+
return $service->users->getProfile('me');
114+
}
115+
116+
/**
117+
*
118+
*/
119+
public function isAccessTokenExpired() {
120+
121+
if($this->client->isAccessTokenExpired()) {
122+
123+
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
124+
$accessToken = $this->client->getAccessToken();
125+
126+
$this->accessToken = $accessToken['access_token'];
127+
$this->refreshToken = $accessToken['refresh_token'];
128+
129+
$this->saveAccessToken();
130+
}
131+
}
132+
133+
/**
134+
* @param $accessToken
135+
* @param $refreshToken
136+
* @return $this
137+
*/
138+
public function setAccessToken($accessToken, $refreshToken) {
139+
$this->accessToken = $accessToken;
140+
$this->refreshToken = $refreshToken;
141+
$this->client->setAccessToken($this->formatAccessToken());
142+
143+
return $this;
144+
}
145+
146+
147+
/**
148+
* @return array
149+
*/
150+
private function formatAccessToken() {
151+
return [
152+
"access_token" => $this->accessToken,
153+
"token_type" => "Bearer",
154+
"expires_in" => 3600,
155+
"refresh_token" => $this->refreshToken,
156+
"created" => 1492614871
157+
];
158+
}
159+
160+
/**
161+
* @return array
162+
*/
163+
private function getAuthConfig()
164+
{
165+
return [
166+
'installed' => [
167+
'client_id' => $this->config['client_id'],
168+
'project_id' => $this->config['project_id'],
169+
'auth_uri' => 'https://accounts.google.com/o/oauth2/auth',
170+
'token_uri' => 'https://accounts.google.com/o/oauth2/token',
171+
'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs',
172+
'client_secret' => $this->config['secret'],
173+
'redirect_uris' => [
174+
"urn:ietf:wg:oauth:2.0:oob",
175+
"http://localhost",
176+
$this->config['redirect_uri'],
177+
]
178+
]
179+
];
180+
}
181+
182+
/**
183+
* @param $list
184+
* @param $property
185+
* @return string|null
186+
*/
187+
public function findProperty($list, $property) {
188+
189+
$find = $list->filter(function($e) use ($property) {
190+
return $e->name == $property;
191+
})->first();
192+
193+
if($find) {
194+
return $find->value;
195+
}
196+
return null;
197+
}
198+
199+
}
200+

0 commit comments

Comments
 (0)