Passed
Pull Request — master (#140)
by
unknown
04:07 queued 01:40
created

Configurable::getConfigs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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