Passed
Push — master ( ddf050...67acca )
by Daniel
01:53
created

Configurable::scopeMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 1
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dacastro4\LaravelGmail\Traits;
4
5
use Google_Service_Gmail;
6
use Illuminate\Support\Facades\Storage;
7
8
/**
9
 * Trait Configurable
10
 * @package Dacastro4\LaravelGmail\Traits
11
 */
12
trait Configurable
13
{
14
15
	private $configuration;
16
17
	public function config( $string = null )
18
	{
19
		$fileName = $this->getFileName();
20
		$file = "gmail/tokens/{$fileName}.json";
21
22
		if ( Storage::exists( $file ) ) {
23
			$config = json_decode(
24
				Storage::get( $file ),
0 ignored issues
show
Bug introduced by
Illuminate\Support\Facades\Storage::get($file) of type Illuminate\Contracts\Filesystem\Filesystem is incompatible with the type string expected by parameter $json of json_decode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

24
				/** @scrutinizer ignore-type */ Storage::get( $file ),
Loading history...
25
				true
26
			);
27
28
			if ( $string ) {
29
				if ( isset( $config[ $string ] ) ) {
30
					return $config[ $string ];
31
				}
32
			} else {
33
				return $config;
34
			}
35
36
		}
37
38
		return null;
39
	}
40
41
	/**
42
	 * @return array
43
	 */
44
	public function getConfigs()
45
	{
46
		return [
47
			'client_secret' => $this->configuration[ 'gmail.client_secret' ],
48
			'client_id'     => $this->configuration[ 'gmail.client_id' ],
49
			'redirect_uri'  => url( $this->configuration[ 'gmail.redirect_url' ] ),
50
		];
51
	}
52
53
	private function getUserScopes()
54
	{
55
		dd( $this->mapScopes() );
56
57
		return array_merge(
58
			[
59
				Google_Service_Gmail::GMAIL_READONLY,
60
			], $this->mapScopes() );
61
	}
62
63
	private function configApi()
64
	{
65
		$type = $this->configuration[ 'gmail.access_type' ];
66
		$approval_prompt = $this->configuration[ 'gmail.approval_prompt' ];
67
68
		$this->setScopes( $this->getUserScopes() );
69
70
		$this->setAccessType( $type );
71
72
		$this->setApprovalPrompt( $approval_prompt );
73
	}
74
75
	private function getFileName()
76
	{
77
		return $this->configuration[ 'gmail.credentials_file_name' ];
78
	}
79
80
	private function mapScopes()
81
	{
82
		$scopes = $this->configuration[ 'gmail.scopes' ];
83
		$mappedScopes = [];
84
85
		if ( ! empty( $scopes ) ) {
86
			foreach ( $scopes as $scope ) {
87
				$mappedScopes[] = $this->scopeMap( $scope );
88
			}
89
		}
90
91
		return $mappedScopes;
92
	}
93
94
	private function scopeMap( $scope )
95
	{
96
		$scopes = [
97
			'all'              => Google_Service_Gmail::MAIL_GOOGLE_COM,
98
			'compose'          => Google_Service_Gmail::GMAIL_COMPOSE,
99
			'insert'           => Google_Service_Gmail::GMAIL_INSERT,
100
			'labels'           => Google_Service_Gmail::GMAIL_LABELS,
101
			'metadata'         => Google_Service_Gmail::GMAIL_METADATA,
102
			'modify'           => Google_Service_Gmail::GMAIL_MODIFY,
103
			'readonly'         => Google_Service_Gmail::GMAIL_READONLY,
104
			'send'             => Google_Service_Gmail::GMAIL_SEND,
105
			'settings_basic'   => Google_Service_Gmail::GMAIL_SETTINGS_BASIC,
106
			'settings_sharing' => Google_Service_Gmail::GMAIL_SETTINGS_SHARING,
107
		];
108
109
		return array_get( $scopes, $scope );
110
	}
111
112
	abstract function setScopes( $scopes );
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
113
114
	abstract function setAccessType( $type );
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
116
	abstract function setApprovalPrompt( $approval );
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
118
}