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