Passed
Pull Request — master (#43)
by
unknown
03:52
created

Configurable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 additionalGoogleScopes() {
58
59
		$googleScopes[] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$googleScopes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $googleScopes = array(); before regardless.
Loading history...
60
		return $googleScopes;
61
	}
62
63
	private function getUserScopes()
64
	{
65
66
		return array_merge(
67
			[
68
				Google_Service_Gmail::GMAIL_READONLY,
69
			], $this->mapScopes(), $this->additionalGoogleScopes() );
70
	}
71
72
	private function configApi()
73
	{
74
		$type = $this->_config[ 'gmail.access_type' ];
75
		$approval_prompt = $this->_config[ 'gmail.approval_prompt' ];
76
77
		$this->setScopes( $this->getUserScopes() );
78
79
		$this->setAccessType( $type );
80
81
		$this->setApprovalPrompt( $approval_prompt );
82
	}
83
84
	private function getFileName()
85
	{
86
		return $this->_config[ 'gmail.credentials_file_name' ];
87
	}
88
89
	private function mapScopes()
90
	{
91
		$scopes = $this->_config[ 'gmail.scopes' ];
92
		$mappedScopes = [];
93
94
		if ( ! empty( $scopes ) ) {
95
			foreach ( $scopes as $scope ) {
96
				$mappedScopes[] = $this->scopeMap( $scope );
97
			}
98
		}
99
100
		return $mappedScopes;
101
	}
102
103
	private function scopeMap( $scope )
104
	{
105
		$scopes = [
106
			'all'              => Google_Service_Gmail::MAIL_GOOGLE_COM,
107
			'compose'          => Google_Service_Gmail::GMAIL_COMPOSE,
108
			'insert'           => Google_Service_Gmail::GMAIL_INSERT,
109
			'labels'           => Google_Service_Gmail::GMAIL_LABELS,
110
			'metadata'         => Google_Service_Gmail::GMAIL_METADATA,
111
			'modify'           => Google_Service_Gmail::GMAIL_MODIFY,
112
			'readonly'         => Google_Service_Gmail::GMAIL_READONLY,
113
			'send'             => Google_Service_Gmail::GMAIL_SEND,
114
			'settings_basic'   => Google_Service_Gmail::GMAIL_SETTINGS_BASIC,
115
			'settings_sharing' => Google_Service_Gmail::GMAIL_SETTINGS_SHARING,
116
		];
117
118
		return array_get( $scopes, $scope );
119
	}
120
121
	public abstract function setScopes( $scopes );
122
123
	public abstract function setAccessType( $type );
124
125
	public abstract function setApprovalPrompt( $approval );
126
127
}
128