Completed
Push — master ( 413e7c...13a6e1 )
by Sergi Tur
09:46
created

GoogleAppsService::getUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Bug introduced by
The variable $state does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
40
        ];
41
42
        if ($state != 'connected') $result['message'] = $message;
0 ignored issues
show
Bug introduced by
The variable $message does not seem to be defined for all execution paths leading up to this point.

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:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

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

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
88
            return $this->allUsers();
89
        });
90
91
        foreach ($users as $user) {
92
//            dd($user);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
}