1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Acacha\Users\Services\GoogleApps; |
4
|
|
|
|
5
|
|
|
use Acacha\Users\Models\GoogleApps\GoogleUser; |
6
|
|
|
use Cache; |
7
|
|
|
use Google; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class GoogleAppsService. |
11
|
|
|
* |
12
|
|
|
* @package Acacha\Users\Services\GoogleApps |
13
|
|
|
*/ |
14
|
|
|
class GoogleAppsService |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Get connection state. |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function getConnectionState() |
23
|
|
|
{ |
24
|
|
|
$directory = Google::make('directory'); |
25
|
|
|
|
26
|
|
|
try { |
27
|
|
|
$r = $directory->users->get($email = config('users.google_apps_user_email_to_check_connection')); |
28
|
|
|
if($r) { |
29
|
|
|
$state = 'connected'; |
30
|
|
|
} else { |
31
|
|
|
echo 'User does not exists: $email<br/>'; |
32
|
|
|
} |
33
|
|
|
} catch (\Exception $e) { |
34
|
|
|
$state = 'error'; |
35
|
|
|
$message = json_decode($e->getMessage()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$result = [ |
39
|
|
|
'state' => $state |
|
|
|
|
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
if ($state != 'connected') $result['message'] = $message; |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $result; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get total number of users. |
49
|
|
|
*/ |
50
|
|
|
public function totalNumberOfUsers() |
51
|
|
|
{ |
52
|
|
|
return count($this->allUsers()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get all users from google apps. |
57
|
|
|
* |
58
|
|
|
* @return array|mixed |
59
|
|
|
*/ |
60
|
|
|
protected function allUsers() |
61
|
|
|
{ |
62
|
|
|
$directory = Google::make('directory'); |
63
|
|
|
$pageToken = null; |
64
|
|
|
$users = []; |
65
|
|
View Code Duplication |
do { |
|
|
|
|
66
|
|
|
try { |
67
|
|
|
$r = $directory->users->listUsers([ |
68
|
|
|
'domain' => config('users.google_apps_domain'), |
69
|
|
|
'maxResults' => config('users.google_apps_users_maxResults'), |
70
|
|
|
'pageToken' => $pageToken |
71
|
|
|
]); |
72
|
|
|
$pageToken = $r->nextPageToken; |
73
|
|
|
$users = array_merge($users, $r->users); |
74
|
|
|
} catch (\Exception $e) { |
75
|
|
|
return json_decode($e->getMessage()); |
76
|
|
|
} |
77
|
|
|
} while ($pageToken); |
78
|
|
|
return $users; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Sync local database with remote Google apps info. |
83
|
|
|
*/ |
84
|
|
|
public function localSync() |
85
|
|
|
{ |
86
|
|
|
$users = Cache::rememberForever('google_app_users', function () { |
87
|
|
|
// return $this->getUsers(); |
|
|
|
|
88
|
|
|
return $this->allUsers(); |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
foreach ($users as $user) { |
92
|
|
|
// dd($user); |
|
|
|
|
93
|
|
|
$user = GoogleUser::firstOrCreate([ |
94
|
|
|
'customerId' => $user->customerId, |
95
|
|
|
'kind' => $user->kind, |
96
|
|
|
'google_id'=> $user->id, |
97
|
|
|
'etag'=> $user->etag, |
98
|
|
|
'primaryEmail'=> $user->primaryEmail, |
99
|
|
|
'givenName'=> $user->name->givenName, |
100
|
|
|
'familyName'=> $user->name->familyName, |
101
|
|
|
'fullName'=> $user->name->fullName, |
102
|
|
|
'orgUnitPath'=> $user->orgUnitPath, |
103
|
|
|
'organizations'=> json_decode($user->organizations), |
104
|
|
|
'isAdmin'=> $user->isAdmin, |
105
|
|
|
'isDelegatedAdmin'=> $user->isDelegatedAdmin, |
106
|
|
|
'lastLoginTime'=> $user->lastLoginTime, |
107
|
|
|
'creationTime'=> $user->creationTime, |
108
|
|
|
'deletionTime'=> $user->deletionTime, |
109
|
|
|
'agreedToTerms'=> $user->agreedToTerms, |
110
|
|
|
'password'=> $user->password, |
111
|
|
|
'hashFunction'=> $user->hashFunction, |
112
|
|
|
'suspended'=> $user->suspended, |
113
|
|
|
'suspensionReason'=> $user->suspensionReason, |
114
|
|
|
'changePasswordAtNextLogin'=> $user->changePasswordAtNextLogin, |
115
|
|
|
'emails'=> json_encode($user->emails), |
116
|
|
|
]); |
117
|
|
|
// dd($user); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get users. |
123
|
|
|
* |
124
|
|
|
* @param int $perPage |
125
|
|
|
* @return int|mixed |
126
|
|
|
*/ |
127
|
|
|
public function getUsers($perPage = 15) |
128
|
|
|
{ |
129
|
|
|
$directory = Google::make('directory'); |
130
|
|
|
try { |
131
|
|
|
$r = $directory->users->listUsers([ |
132
|
|
|
'domain' => config('users.google_apps_domain'), |
133
|
|
|
'maxResults' => $perPage |
134
|
|
|
]); |
135
|
|
|
} catch (\Exception $e) { |
136
|
|
|
return json_decode($e->getMessage()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $r->users; |
140
|
|
|
} |
141
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: