|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Dacastro4\LaravelGmail\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Google_Service_Gmail; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Trait Configurable |
|
10
|
|
|
* @package Dacastro4\LaravelGmail\Traits |
|
11
|
|
|
*/ |
|
12
|
|
|
trait Configurable |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
private $configuration; |
|
16
|
|
|
|
|
17
|
|
|
public function config( $string = null ) |
|
18
|
|
|
{ |
|
19
|
|
|
$fileName = $this->getFileName(); |
|
20
|
|
|
$file = "gmail/tokens/{$fileName}.json"; |
|
21
|
|
|
|
|
22
|
|
|
if ( Storage::exists( $file ) ) { |
|
23
|
|
|
$config = json_decode( |
|
24
|
|
|
Storage::get( $file ), |
|
|
|
|
|
|
25
|
|
|
true |
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
if ( $string ) { |
|
29
|
|
|
if ( isset( $config[ $string ] ) ) { |
|
30
|
|
|
return $config[ $string ]; |
|
31
|
|
|
} |
|
32
|
|
|
} else { |
|
33
|
|
|
return $config; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getConfigs() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
'client_secret' => $this->configuration[ 'gmail.client_secret' ], |
|
48
|
|
|
'client_id' => $this->configuration[ 'gmail.client_id' ], |
|
49
|
|
|
'redirect_uri' => url( $this->configuration[ 'gmail.redirect_url' ] ), |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
private function getUserScopes() |
|
54
|
|
|
{ |
|
55
|
|
|
dd( $this->mapScopes() ); |
|
56
|
|
|
|
|
57
|
|
|
return array_merge( |
|
58
|
|
|
[ |
|
59
|
|
|
Google_Service_Gmail::GMAIL_READONLY, |
|
60
|
|
|
], $this->mapScopes() ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function configApi() |
|
64
|
|
|
{ |
|
65
|
|
|
$type = $this->configuration[ 'gmail.access_type' ]; |
|
66
|
|
|
$approval_prompt = $this->configuration[ 'gmail.approval_prompt' ]; |
|
67
|
|
|
|
|
68
|
|
|
$this->setScopes( $this->getUserScopes() ); |
|
69
|
|
|
|
|
70
|
|
|
$this->setAccessType( $type ); |
|
71
|
|
|
|
|
72
|
|
|
$this->setApprovalPrompt( $approval_prompt ); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function getFileName() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->configuration[ 'gmail.credentials_file_name' ]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private function mapScopes() |
|
81
|
|
|
{ |
|
82
|
|
|
$scopes = $this->configuration[ 'gmail.scopes' ]; |
|
83
|
|
|
$mappedScopes = []; |
|
84
|
|
|
|
|
85
|
|
|
if ( ! empty( $scopes ) ) { |
|
86
|
|
|
foreach ( $scopes as $scope ) { |
|
87
|
|
|
$mappedScopes[] = $this->scopeMap( $scope ); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $mappedScopes; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
private function scopeMap( $scope ) |
|
95
|
|
|
{ |
|
96
|
|
|
$scopes = [ |
|
97
|
|
|
'all' => Google_Service_Gmail::MAIL_GOOGLE_COM, |
|
98
|
|
|
'compose' => Google_Service_Gmail::GMAIL_COMPOSE, |
|
99
|
|
|
'insert' => Google_Service_Gmail::GMAIL_INSERT, |
|
100
|
|
|
'labels' => Google_Service_Gmail::GMAIL_LABELS, |
|
101
|
|
|
'metadata' => Google_Service_Gmail::GMAIL_METADATA, |
|
102
|
|
|
'modify' => Google_Service_Gmail::GMAIL_MODIFY, |
|
103
|
|
|
'readonly' => Google_Service_Gmail::GMAIL_READONLY, |
|
104
|
|
|
'send' => Google_Service_Gmail::GMAIL_SEND, |
|
105
|
|
|
'settings_basic' => Google_Service_Gmail::GMAIL_SETTINGS_BASIC, |
|
106
|
|
|
'settings_sharing' => Google_Service_Gmail::GMAIL_SETTINGS_SHARING, |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
|
|
return array_get( $scopes, $scope ); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
abstract function setScopes( $scopes ); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
abstract function setAccessType( $type ); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
abstract function setApprovalPrompt( $approval ); |
|
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
} |