Passed
Pull Request — master (#255)
by
unknown
11:49
created

Configurable::setConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Google_Service_Gmail;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Arr;
8
9
/**
10
 * Trait Configurable
11
 * @package Dacastro4\LaravelGmail\Traits
12
 */
13
trait Configurable
14
{
15
16
	protected $additionalScopes = [];
17
	private $_config;
18
	private $configObject;
19
20
	public function __construct($config)
21
	{
22
		$this->_config = $config;
23
	}
24
25
	public function setConfiguration($config)
26
	{
27
		$this->configObject = $config;
28
	}
29
30
	public function config($string = null)
31
	{
32
		$disk = Storage::disk('local');
0 ignored issues
show
Unused Code introduced by
The assignment to $disk is dead and can be removed.
Loading history...
33
		$fileName = $this->getFileName();
34
		$file = "gmail/tokens/$fileName.json";
0 ignored issues
show
Unused Code introduced by
The assignment to $file is dead and can be removed.
Loading history...
35
		$allowJsonEncrypt = $this->_config['gmail.allow_json_encrypt'];
0 ignored issues
show
Unused Code introduced by
The assignment to $allowJsonEncrypt is dead and can be removed.
Loading history...
36
		if ($this->configObject) {
37
			if ($string) {
38
				if (isset($this->configObject[$string])) {
39
					return $this->configObject[$string];
40
				}
41
			}
42
			return $this->configObject;
43
		}
44
		// if ($disk->exists($file)) {
45
		// 	if ($allowJsonEncrypt) {
46
		// 		$config = json_decode(decrypt($disk->get($file)), true);
47
		// 	} else {
48
		// 		$config = json_decode($disk->get($file), true);
49
		// 	}
50
51
		// 	if ($string) {
52
		// 		if (isset($config[$string])) {
53
		// 			return $config[$string];
54
		// 		}
55
		// 	} else {
56
		// 		return $config;
57
		// 	}
58
		// }
59
60
		return null;
61
	}
62
63
	private function getFileName()
64
	{
65
		if (property_exists(get_class($this), 'userId') && $this->userId) {
66
			$userId = $this->userId;
67
		} elseif (auth()->user()) {
68
			$userId = auth()->user()->id;
69
		}
70
71
		$credentialFilename = $this->_config['gmail.credentials_file_name'];
72
		$allowMultipleCredentials = $this->_config['gmail.allow_multiple_credentials'];
73
74
		if (isset($userId) && $allowMultipleCredentials) {
75
			return sprintf('%s-%s', $credentialFilename, $userId);
76
		}
77
78
		return $credentialFilename;
79
	}
80
81
	/**
82
	 * @return array
83
	 */
84
	public function getConfigs()
85
	{
86
		return [
87
			'client_secret' => $this->_config['gmail.client_secret'],
88
			'client_id' => $this->_config['gmail.client_id'],
89
			'redirect_uri' => url($this->_config['gmail.redirect_url']),
90
			'state' => isset($this->_config['gmail.state']) ? $this->_config['gmail.state'] : null,
91
		];
92
	}
93
94
	public function setAdditionalScopes(array $scopes)
95
	{
96
		$this->additionalScopes = $scopes;
97
98
		return $this;
99
	}
100
101
	private function configApi()
102
	{
103
		$type = $this->_config['gmail.access_type'];
104
		$approval_prompt = $this->_config['gmail.approval_prompt'];
105
		$prompt = $this->_config['gmail.prompt'];
106
107
		$this->setScopes($this->getUserScopes());
108
109
		$this->setAccessType($type);
110
111
		$this->setApprovalPrompt($approval_prompt);
112
		$this->setPrompt($prompt);
113
	}
114
115
	public abstract function setScopes($scopes);
116
117
	private function getUserScopes()
118
	{
119
		return $this->mapScopes();
120
	}
121
122
	private function mapScopes()
123
	{
124
		$scopes = array_merge($this->_config['gmail.scopes'] ?? [], $this->additionalScopes);
125
		$scopes = array_unique(array_filter($scopes));
126
		$mappedScopes = [];
127
128
		if (!empty($scopes)) {
129
			foreach ($scopes as $scope) {
130
				$mappedScopes[] = $this->scopeMap($scope);
131
			}
132
		}
133
134
		return array_merge($mappedScopes, $this->_config['gmail.additional_scopes'] ?? []);
135
	}
136
137
	private function scopeMap($scope)
138
	{
139
		$scopes = [
140
			'all' => Google_Service_Gmail::MAIL_GOOGLE_COM,
141
			'compose' => Google_Service_Gmail::GMAIL_COMPOSE,
142
			'insert' => Google_Service_Gmail::GMAIL_INSERT,
143
			'labels' => Google_Service_Gmail::GMAIL_LABELS,
144
			'metadata' => Google_Service_Gmail::GMAIL_METADATA,
145
			'modify' => Google_Service_Gmail::GMAIL_MODIFY,
146
			'readonly' => Google_Service_Gmail::GMAIL_READONLY,
147
			'send' => Google_Service_Gmail::GMAIL_SEND,
148
			'settings_basic' => Google_Service_Gmail::GMAIL_SETTINGS_BASIC,
149
			'settings_sharing' => Google_Service_Gmail::GMAIL_SETTINGS_SHARING,
150
		];
151
152
		return Arr::get($scopes, $scope);
153
	}
154
155
	public abstract function setAccessType($type);
156
157
	public abstract function setApprovalPrompt($approval);
158
	public abstract function setPrompt($prompt);
159
}
160