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